Gondwana Game Engine Development (master) - 2.6.1
Gondwana is a cross-platform 2.5D game and rendering engine written in C#/.NET 8. It provides fine-grained control over rendering, timing, and scene composition, with built-in support for parallax, z-ordering, pixel overhang, collision detection, and particle effects. Gondwana targets desktop and web platforms using SkiaSharp for graphics and pluggable audio backends for desktop and browser targets.
Loading...
Searching...
No Matches
SceneLayerTile.cs
1using System.Drawing;
6using Newtonsoft.Json;
7
8namespace Gondwana.Scenes;
9
13[JsonObject(IsReference = true)]
14public class SceneLayerTile : Tile
15{
16 #region private / internal fields
17
18 [JsonIgnore]
19 internal SceneLayer parentSceneLayer;
20
25 [JsonProperty]
26 internal Point sceneLayerCoordinates;
27
28 #endregion private / internal fields
29
30 #region constructors / finalizer
31
32 [JsonConstructor]
33 private SceneLayerTile()
34 {
35 zOrder = 0;
36 visible = true;
37
38 // Parent ownership is reconstructed by SceneLayer.OnDeserialized().
39 // Use the null-object layer while Json.NET populates inherited Tile
40 // properties whose setters may invalidate the parent layer.
41 parentSceneLayer = SceneLayer.Empty;
42 }
43
44 internal SceneLayerTile(SceneLayer sceneLayer)
45 {
46 zOrder = 0;
47 visible = true;
48 parentSceneLayer = sceneLayer;
49 }
50
55 internal void EnsureCollider()
56 {
57 if (_collider is null)
58 {
60 this,
61 collisionGroup: CollisionMasks.None,
62 collidesWith: CollisionMasks.None));
63 }
64 }
65
75 ~SceneLayerTile()
76 {
77 Dispose();
78 }
79
80 #endregion constructors / finalizer
81
83 public override bool EnableFog
84 {
85 get => base.EnableFog;
86 set
87 {
88 bool changed = enableFog != value;
89 base.EnableFog = value;
90
91 if (changed && !ReferenceEquals(parentSceneLayer, SceneLayer.Empty))
92 parentSceneLayer.InvalidateFixedGridRenderEligibility();
93 }
94 }
95
96 private TileTransform _transform;
97
99 [JsonProperty]
101 {
102 get => _transform;
103 set
104 {
105 if (!Enum.IsDefined(value)) throw new ArgumentOutOfRangeException(nameof(value));
106 if (_transform == value) return;
107 SceneLayer.RefreshQueue.AddWorldRect(DrawLocationWorld);
108 _transform = value;
109 if (!ReferenceEquals(SceneLayer, SceneLayer.Empty))
110 {
111 if (value == TileTransform.Identity) SceneLayer.TransformedTiles.Remove(this);
112 else SceneLayer.TransformedTiles.Add(this);
113 }
114 SceneLayer.RefreshQueue.AddWorldRect(DrawLocationWorld);
115 }
116 }
117
119 [JsonIgnore]
120 public Size EffectiveTileSize => TileTransformGeometry.TransformSize(CurrentFrame.TileSize, Transform);
121
123 [JsonIgnore]
124 public override Spacing Overhang => TileTransformGeometry.TransformSpacing(CurrentFrame.Overhang, Transform);
125
127 [JsonIgnore]
128 public Spacing EffectiveTilePadding => TileTransformGeometry.TransformSpacing(CurrentFrame.TilePadding, Transform);
129
131 [JsonIgnore]
132 public CollisionAdjust EffectiveCollisionAdjust => TileTransformGeometry.TransformCollisionAdjust(AdjustCollisionArea, Transform);
133
135 [JsonIgnore]
136 public override Rectangle CollisionArea => EffectiveCollisionAdjust.ApplyTo(DrawLocationWorld);
137
139 public override void Draw(Gondwana.Rendering.Backbuffers.BackbufferBase backbuffer, RectangleF destRectScreen)
140 {
141 if (Transform == TileTransform.Identity)
142 {
143 base.Draw(backbuffer, destRectScreen);
144 return;
145 }
146
147 var origin = TileTransformGeometry.MapToDestination(new(0, 0), destRectScreen, Transform);
148 var x = TileTransformGeometry.MapToDestination(new(1, 0), destRectScreen, Transform);
149 var y = TileTransformGeometry.MapToDestination(new(0, 1), destRectScreen, Transform);
150 var matrix = new global::SkiaSharp.SKMatrix
151 {
152 ScaleX = x.X - origin.X,
153 SkewX = y.X - origin.X,
154 TransX = origin.X,
155 SkewY = x.Y - origin.Y,
156 ScaleY = y.Y - origin.Y,
157 TransY = origin.Y,
158 Persp2 = 1
159 };
160 var canvas = backbuffer.Canvas;
161 canvas.Save();
162 try
163 {
164 canvas.Concat(ref matrix);
165 base.Draw(backbuffer, new RectangleF(0, 0, 1, 1));
166 }
167 finally { canvas.Restore(); }
168 }
169
171 public override void Dispose()
172 {
173 parentSceneLayer?.TransformedTiles.Remove(this);
174 base.Dispose();
175 GC.SuppressFinalize(this);
176 }
177
178 #region public properties
179
192 [JsonIgnore]
193 public override int ZOrder
194 {
195 get => base.ZOrder;
196 set
197 {
198 bool changed = zOrder != value;
199 base.ZOrder = value;
200
201 if (changed && !ReferenceEquals(parentSceneLayer, SceneLayer.Empty))
202 parentSceneLayer.InvalidateFixedGridRenderEligibility();
203 }
204 }
205
226 [JsonIgnore]
227 public override Rectangle DrawLocationWorld => parentSceneLayer.CoordinateSystem.GetPixelRangeForTile(this, true);
228
248 [JsonIgnore]
249 public override bool IsPositionFixed => true;
250
269 [JsonIgnore]
270 public override PointF SceneLayerCoordinates => (PointF)sceneLayerCoordinates;
271
290 [JsonIgnore]
291 public Point GridCoordinatesAbs => sceneLayerCoordinates;
292
310 [JsonIgnore]
311 public override SceneLayer SceneLayer => parentSceneLayer;
312
335 [JsonProperty]
336 public bool EnableAnimator
337 {
338 get { return (animator != null); }
339 set
340 {
341 if (value)
342 {
343 if (animator == null)
344 animator = new Animator(this);
345
346 return;
347 }
348
349 animator?.Dispose();
350 animator = null;
351 }
352 }
353
354 #endregion public properties
355}
Manages animation playback for a Tile, controlling animation cycles, frame timing,...
Definition Animator.cs:9
Provides collision detection capabilities for a Tile by implementing the ICollider interface.
Represents an abstract base class for drawable tiles in the Gondwana engine. Provides core functional...
Definition Tile.cs:19
virtual Frame CurrentFrame
Gets or sets the current frame. The initial frame supplies the tile's default collision adjustment an...
Definition Tile.cs:176
void AttachCollider(ICollider collider)
Attaches this tile's collider and applies any collision state that was set before the collider became...
Definition Tile.cs:504
ICollider? _collider
The collider.
Definition Tile.cs:58
virtual CollisionAdjust AdjustCollisionArea
Gets or sets the collision adjustment used to derive CollisionArea.
Definition Tile.cs:285
Represents a base class for managing a graphical backbuffer, and is the in-memory surface where rende...
Point GridCoordinatesAbs
Gets the absolute grid coordinates of this tile within its parent scene layer as integer values.
Spacing EffectiveTilePadding
Gets oriented atlas padding for inspection, never for source slicing.
override void Draw(Gondwana.Rendering.Backbuffers.BackbufferBase backbuffer, RectangleF destRectScreen)
Draws the current content into the backbuffer at the supplied screen-pixel bounds.
override SceneLayer SceneLayer
Gets the parent scene layer that contains this tile.
override int ZOrder
Gets the z-order (rendering depth) of this tile within its layer.
override bool IsPositionFixed
Gets a value indicating whether this tile's position is fixed within its grid.
bool EnableAnimator
Gets or sets a value indicating whether the tile's animator is enabled.
TileTransform Transform
Gets or sets this placement's orthogonal orientation without changing its source frame.
override Spacing Overhang
Gets oriented overhang. The source value remains on CurrentFrame.
CollisionAdjust EffectiveCollisionAdjust
Gets the oriented collision insets, including the current frame-derived adjustment.
override Rectangle CollisionArea
Gets the collision area of this entity in world pixel coordinates.
override PointF SceneLayerCoordinates
Gets the grid coordinates of this tile within its parent scene layer as a floating-point value.
override void Dispose()
Releases the resources owned by this instance.
Size EffectiveTileSize
Gets source frame dimensions after placement orientation.
override Rectangle DrawLocationWorld
Gets the world-space pixel rectangle where this tile should be drawn.
TileTransform
The eight orthogonal orientations of artwork placed in a fixed scene cell.
record struct Spacing([property:JsonProperty("left")] int Left, [property:JsonProperty("top")] int Top, [property:JsonProperty("right")] int Right, [property:JsonProperty("bottom")] int Bottom)
Represents dimensions in pixels when spacing Tilesheets.
Definition Spacing.cs:12
Per-edge inset amounts applied to visual bounds to produce a collision rectangle.