2using System.Runtime.Serialization;
17[JsonObject(IsReference =
true)]
20 #region static members
27 #endregion static members
34 protected internal int zOrder = 0;
39 protected internal bool visible;
44 protected internal Frame frame;
49 protected internal bool enableFog =
false;
54 protected internal Animator? animator;
67 private bool _adjustCollisionAreaByFrame;
68 private bool _hasAssignedFrame;
69 private bool _collisionAdjustExplicitlySet;
71 private bool _collisionTypeByFrame;
72 private bool _collisionTypeExplicitlySet;
73 private bool _collisionsEnabled;
74 private string? _collisionProfileName;
75 private long _sortKeyRenderPassId;
76 private TileSortKey _sortKey;
80 #region abstract properties
102 #endregion abstract properties
104 #region IDrawable members
110 public Guid
Id {
get;
internal set; } = Guid.NewGuid();
168 backbuffer.DrawTileFrame(
this, destRectScreen);
170 #endregion IDrawable members
192 bool usesDirtyRegionRendering =
193 SceneLayer.Scene?.UsesDirtyRegionRendering ??
true;
195 if (usesDirtyRegionRendering)
200 bool hasFrame = value.Tilesheet is not
null;
203 (_adjustCollisionAreaByFrame ||
204 (!_hasAssignedFrame && !_collisionAdjustExplicitlySet)))
206 SetCollisionAdjustFromFrame(value);
210 (_collisionTypeByFrame ||
211 (!_hasAssignedFrame && !_collisionTypeExplicitlySet)))
213 SetCollisionTypeFromFrame(value);
216 _hasAssignedFrame = hasFrame;
218 if (usesDirtyRegionRendering)
267 SceneLayer.CoordinateSystem.GetPolygonPts(
this,
false);
280 get => _adjustCollisionAreaByFrame;
283 _adjustCollisionAreaByFrame = value;
285 if (value && frame.Tilesheet is not
null)
286 SetCollisionAdjustFromFrame(frame);
296 get => _adjustCollisionArea;
299 _adjustCollisionArea = value;
300 _collisionAdjustExplicitlySet =
true;
311 get => _collisionType;
314 _collisionTypeExplicitlySet =
true;
315 SetCollisionTypeCore(value);
326 get => _collisionTypeByFrame;
329 _collisionTypeByFrame = value;
331 if (value && frame.Tilesheet is not
null)
332 SetCollisionTypeFromFrame(frame);
342 get => _collisionProfileName;
343 private set => _collisionProfileName = value;
352 get => _collisionsEnabled;
355 _collisionTypeExplicitlySet =
true;
365 SetCollisionTypeCore(enabledType);
388 if (
string.IsNullOrWhiteSpace(profileName))
389 throw new ArgumentException(
"Collision profile name cannot be empty.", nameof(profileName));
393 if (scene is not
null)
395 var profile = scene.CollisionProfiles.Get(profileName);
396 _ = profile.ResolveCollisionGroup(scene.CollisionGroups);
397 _ = profile.ResolveCollidesWith(scene.CollisionGroups);
401 ApplyCollisionProfileToCollider();
408 internal void RefreshCollisionProfile() => ApplyCollisionProfileToCollider();
416 ArgumentNullException.ThrowIfNull(source);
418 _adjustCollisionArea = source._adjustCollisionArea;
419 _adjustCollisionAreaByFrame = source._adjustCollisionAreaByFrame;
420 _hasAssignedFrame = frame.Tilesheet is not
null;
421 _collisionAdjustExplicitlySet = source._collisionAdjustExplicitlySet;
422 _collisionType = source._collisionType;
423 _collisionTypeByFrame = source._collisionTypeByFrame;
424 _collisionTypeExplicitlySet = source._collisionTypeExplicitlySet;
425 _collisionsEnabled = source._collisionsEnabled;
426 _collisionProfileName = source._collisionProfileName;
429 private void SetCollisionAdjustFromFrame(
Frame value)
436 private void SetCollisionTypeFromFrame(
Frame value)
445 _collisionType = collisionType;
446 ApplyCollisionTypeToCollider();
450 private void ApplyCollisionTypeToCollider()
455 switch (_collisionType)
466 private void SetCollisionsEnabledCore(
bool enabled)
468 _collisionsEnabled = enabled;
469 SynchronizeColliderRegistration();
472 private void SynchronizeColliderRegistration()
477 if (_collisionsEnabled)
483 private void ApplyCollisionProfileToCollider()
485 if (
_collider is
null ||
string.IsNullOrWhiteSpace(_collisionProfileName))
493 var profile = scene.CollisionProfiles.Get(_collisionProfileName);
494 _collider.CollisionGroup = profile.ResolveCollisionGroup(scene.CollisionGroups);
495 _collider.CollidesWith = profile.ResolveCollidesWith(scene.CollisionGroups);
518 ArgumentNullException.ThrowIfNull(collider);
524 ApplyCollisionProfileToCollider();
525 ApplyCollisionTypeToCollider();
526 SynchronizeColliderRegistration();
530 private void OnTileDeserialized(StreamingContext context)
532 _hasAssignedFrame = frame.Tilesheet is not
null;
534 if (_adjustCollisionAreaByFrame && _hasAssignedFrame)
535 SetCollisionAdjustFromFrame(frame);
537 if (_collisionTypeByFrame && _hasAssignedFrame)
538 SetCollisionTypeFromFrame(frame);
541 #region IComparable<Tile> Members
553 var thisKey = GetSortKeyForCompare();
554 var tileKey = tile.GetSortKeyForCompare();
556 if (thisKey.IsPositionFixed && !tileKey.IsPositionFixed)
559 if (!thisKey.IsPositionFixed && tileKey.IsPositionFixed)
562 return (thisKey.Location, thisKey.ZOrder, thisKey.SceneLayerX)
563 .CompareTo((tileKey.Location, tileKey.ZOrder, tileKey.SceneLayerX));
566 private TileSortKey GetSortKeyForCompare()
568 var renderContext = RenderContext.Current;
573 if (renderContext is
null)
574 return CaptureSortKey();
576 long renderPassId = renderContext.PassId;
578 if (_sortKeyRenderPassId != renderPassId)
580 _sortKey = CaptureSortKey();
581 _sortKeyRenderPassId = renderPassId;
587 private TileSortKey CaptureSortKey()
593 float location = isPositionFixed
594 ? drawLocationWorld.Top + overhang.Top
595 : drawLocationWorld.Bottom - overhang.Bottom - 1;
597 return new TileSortKey(
604 private readonly record
struct TileSortKey(
610 #endregion IComparable<Tile> Members
612 #region IDisposable Members
628 GC.SuppressFinalize(
this);
631 #endregion IDisposable Members
Manages animation playback for a Tile, controlling animation cycles, frame timing,...
Represents an abstract base class for drawable tiles in the Gondwana engine. Provides core functional...
bool CollisionsEnabled
Gets or sets whether this tile participates in collision detection.
virtual bool EnableFog
Gets or sets whether scene-layer fog applies to this tile.
virtual bool Visible
Gets or sets whether this drawable is visible.
virtual RectangleF GetCollisionAreaScreen(View view)
Transforms the collision bounds into screen pixels for the specified view.
virtual Frame CurrentFrame
Gets or sets the current frame. The initial frame supplies the tile's default collision adjustment an...
virtual RectangleF GetDrawLocationScreen(View view)
Transforms the drawable bounds into screen pixels for the specified view.
void AttachCollider(ICollider collider)
Attaches this tile's collider and applies any collision state that was set before the collider became...
virtual Rectangle CollisionArea
Gets the effective collision area in world coordinates.
SceneLayer SceneLayer
Gets the scene layer that owns this tile.
virtual void Draw(BackbufferBase backbuffer, RectangleF destRectScreen)
Draws the current content into the backbuffer at the supplied screen-pixel bounds.
virtual ? ICollider Collider
Gets the collision shape associated with this tile.
bool pauseAnimation
The pause animation.
void CopyCollisionSettingsFrom(Tile source)
Copies collision behavior, profile, and effective frame metadata from another tile.
string? Nickname
Gets or sets the optional lookup name.
virtual Point[] OutlinePointsWorld
Gets the tile outline vertices in world pixels.
virtual TileCollisionType CollisionType
Gets or sets the tile's collision behavior. This is initialized from the first assigned frame and may...
virtual void Dispose()
Releases the resources owned by this instance.
virtual bool AdjustCollisionAreaByFrame
Gets or sets whether frame changes replace the tile's collision adjustment with the newly selected fr...
virtual bool PauseAnimation
Gets or sets whether animation is paused.
virtual int ZOrder
Gets or sets the drawing order within the layer.
void SetCollisionProfile(string profileName)
Applies a named profile from this tile's parent scene. If the layer has not yet been attached to a sc...
TypedValueBag ValueBag
Gets the custom typed values associated with this object.
int CompareTo(Tile? tile)
Compares this tile with another tile using their render ordering.
Rectangle DrawLocationWorld
Gets the tile drawing bounds in world pixels.
virtual Spacing Overhang
Gets the current frame's visual overhang beyond its tile bounds.
void DetachCollider()
Detaches the current collider from its scene-layer registry without otherwise changing this tile's se...
string? CollisionProfileName
Gets the scene-level collision profile currently associated with this tile.
ICollider? _collider
The collider.
virtual Animator TileAnimator
Gets the animator associated with this tile.
static List< Tile > TilesAnimating
Gets the collection of all tiles that are currently animating in the scene.
virtual CollisionAdjust AdjustCollisionArea
Gets or sets the collision adjustment used to derive CollisionArea.
Guid Id
Gets the unique identifier.
virtual bool CollisionTypeByFrame
Gets or sets whether frame changes replace this tile's collision type with the newly selected frame's...
bool IsPositionFixed
Gets whether the tile occupies a fixed position in its layer.
PointF SceneLayerCoordinates
Gets the position in scene-layer grid coordinates.
Represents a base class for managing a graphical backbuffer, and is the in-memory surface where rende...
Represents a rendered view of a scene, combining a camera position with a viewport configuration to c...
A flexible, strongly-typed value container intended for runtime-attached metadata and ephemeral engin...
Represents an object that can be drawn on a visual surface, with properties for visibility and stacki...
World-space axis-aligned collider used by the collision system.
Represents an entity that participates in the collision system and has a defined collision area.
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.
CollisionResponseType
Defines how a collider responds to collisions with other colliders.
@ Trigger
Trigger collision response that reports overlaps without applying push-out or blocking movement.
TileCollisionType
Defines the default collision behavior associated with a tilesheet region, frame, or runtime tile.
@ None
Collision is disabled.
@ Blocking
Collision is enabled and overlapping solid colliders block movement.
@ Trigger
Collision is enabled and overlaps are reported without blocking movement.
Represents the source Tilesheet and its coordinates to render on a destination.
CollisionAdjust CollisionAdjust
Gets or sets the collision adjustment associated with this frame's region coordinates.
TileCollisionType CollisionType
Gets or sets the effective collision type associated with this frame's region coordinates.
Per-edge inset amounts applied to visual bounds to produce a collision rectangle.
static readonly CollisionAdjust None
Represents an adjustment with no pixel offsets.