Gondwana Game Engine Development (master) - 2.7.0
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] internal SceneLayer parentSceneLayer;
19
24 [JsonProperty] internal Point sceneLayerCoordinates;
25
26 #endregion private / internal fields
27
28 #region constructors / finalizer
29
30 [JsonConstructor]
31 private SceneLayerTile()
32 {
33 zOrder = 0;
34 visible = true;
35
36 // Parent ownership is reconstructed by SceneLayer.OnDeserialized().
37 // Use the null-object layer while Json.NET populates inherited Tile
38 // properties whose setters may invalidate the parent layer.
39 parentSceneLayer = SceneLayer.Empty;
40 }
41
42 internal SceneLayerTile(SceneLayer sceneLayer)
43 {
44 zOrder = 0;
45 visible = true;
46 parentSceneLayer = sceneLayer;
47 }
48
53 internal void EnsureCollider()
54 {
55 if (_collider is null)
56 {
58 new TileCollider(
59 this,
60 collisionGroup: CollisionMasks.None,
61 collidesWith: CollisionMasks.None));
62 }
63 }
64
74 ~SceneLayerTile()
75 {
76 Dispose();
77 }
78
79 #endregion constructors / finalizer
80
82 public override bool EnableFog
83 {
84 get => base.EnableFog;
85 set
86 {
87 bool changed = enableFog != value;
88 base.EnableFog = value;
89
90 if (changed && !ReferenceEquals(parentSceneLayer, SceneLayer.Empty))
91 parentSceneLayer.InvalidateFixedGridRenderEligibility();
92 }
93 }
94
95 private TileTransform _transform;
96
98 [JsonProperty]
100 {
101 get => _transform;
102 set
103 {
104 if (!Enum.IsDefined(value))
105 throw new ArgumentOutOfRangeException(nameof(value));
106
107 if (_transform == value)
108 return;
109
110 SceneLayer.RefreshQueue.AddWorldRect(DrawLocationWorld);
111 _transform = value;
112
113 if (!ReferenceEquals(SceneLayer, SceneLayer.Empty))
114 {
115 if (value == TileTransform.Identity)
116 SceneLayer.TransformedTiles.Remove(this);
117 else
118 SceneLayer.TransformedTiles.Add(this);
119 }
120
121 SceneLayer.RefreshQueue.AddWorldRect(DrawLocationWorld);
122 }
123 }
124
126 [JsonIgnore]
127 public Size EffectiveTileSize => TileTransformGeometry.TransformSize(CurrentFrame.TileSize, Transform);
128
130 [JsonIgnore]
131 public override Spacing Overhang =>
132 TileTransformGeometry.TransformSpacing(
133 TileTransformGeometry.ScaleOverhang(
134 CurrentFrame.TileSize,
135 new Size(SceneLayer.TileWidth, SceneLayer.TileHeight),
136 CurrentFrame.Overhang),
137 Transform);
138
140 [JsonIgnore]
141 public Spacing EffectiveTilePadding => TileTransformGeometry.TransformSpacing(CurrentFrame.TilePadding, Transform);
142
144 [JsonIgnore]
146 TileTransformGeometry.TransformCollisionAdjust(AdjustCollisionArea, Transform);
147
149 [JsonIgnore]
150 public override Rectangle CollisionArea => EffectiveCollisionAdjust.ApplyTo(DrawLocationWorld);
151
153 public override void Draw(Gondwana.Rendering.Backbuffers.BackbufferBase backbuffer, RectangleF destRectScreen)
154 {
155 if (Transform == TileTransform.Identity)
156 {
157 base.Draw(backbuffer, destRectScreen);
158 return;
159 }
160
161 var origin = TileTransformGeometry.MapToDestination(new(0, 0), destRectScreen, Transform);
162 var x = TileTransformGeometry.MapToDestination(new(1, 0), destRectScreen, Transform);
163 var y = TileTransformGeometry.MapToDestination(new(0, 1), destRectScreen, Transform);
164
165 var matrix = new global::SkiaSharp.SKMatrix
166 {
167 ScaleX = x.X - origin.X,
168 SkewX = y.X - origin.X,
169 TransX = origin.X,
170 SkewY = x.Y - origin.Y,
171 ScaleY = y.Y - origin.Y,
172 TransY = origin.Y,
173 Persp2 = 1
174 };
175
176 var canvas = backbuffer.Canvas;
177 canvas.Save();
178
179 try
180 {
181 canvas.Concat(ref matrix);
182
183 base.Draw(
184 backbuffer,
185 new RectangleF(
186 0,
187 0,
188 1,
189 1));
190 }
191 finally
192 {
193 canvas.Restore();
194 }
195 }
196
198 public override void Dispose()
199 {
200 parentSceneLayer?.TransformedTiles.Remove(this);
201 base.Dispose();
202 GC.SuppressFinalize(this);
203 }
204
205 #region public properties
206
219 [JsonIgnore]
220 public override int ZOrder
221 {
222 get => base.ZOrder;
223 set
224 {
225 bool changed = zOrder != value;
226 base.ZOrder = value;
227
228 if (changed && !ReferenceEquals(parentSceneLayer, SceneLayer.Empty))
229 parentSceneLayer.InvalidateFixedGridRenderEligibility();
230 }
231 }
232
253 [JsonIgnore]
254 public override Rectangle DrawLocationWorld => parentSceneLayer.CoordinateSystem.GetPixelRangeForTile(this, true);
255
275 [JsonIgnore]
276 public override bool IsPositionFixed => true;
277
296 [JsonIgnore]
297 public override PointF SceneLayerCoordinates => (PointF)sceneLayerCoordinates;
298
317 [JsonIgnore]
318 public Point GridCoordinatesAbs => sceneLayerCoordinates;
319
337 [JsonIgnore]
338 public override SceneLayer SceneLayer => parentSceneLayer;
339
362 [JsonProperty]
363 public bool EnableAnimator
364 {
365 get { return (animator != null); }
366 set
367 {
368 if (value)
369 {
370 if (animator == null)
371 animator = new Animator(this);
372
373 return;
374 }
375
376 animator?.Dispose();
377 animator = null;
378 }
379 }
380
381 #endregion public properties
382}
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:185
void AttachCollider(ICollider collider)
Attaches this tile's collider and applies any collision state that was set before the collider became...
Definition Tile.cs:516
ICollider? _collider
The collider.
Definition Tile.cs:64
virtual CollisionAdjust AdjustCollisionArea
Gets or sets the collision adjustment used to derive CollisionArea.
Definition Tile.cs:295
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 scaled, oriented world-pixel 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.