Open nighca opened 3 weeks ago
@nighca In game development, interfaces from the physics engine are typically used to implement the above business requirements e.g., Physics2D.Raycast, Physics2D.OverlapBox ...
The following are some of Unity's API interfaces:
public static RaycastHit2D Raycast(Vector2 origin, Vector2 direction, float distance)
public static RaycastHit2D[] RaycastAll(Vector2 origin,Vector2 direction,float distance,int layerMask)
public static Collider2D OverlapBox(Vector2 point, Vector2 size, float angle)
public static Collider2D[] OverlapBoxAll(Vector2 point,Vector2 size,float angle,int layerMask,float minDepth)
interfaces from the physics engine are typically used to implement the above business requirements e.g., Physics2D.Raycast, Physics2D.OverlapBox ...
@JiepengTan These seem to be more basic definitions, like Shape
in spx.
What I propose to add is a type definition which just describes a sprite. All public methods that a sprite has, e.g., Die
/Say
/Step
/..., will be declared on this type definition.
Consider a modified version of the game AircraftWar where instead of firing bullets, the aircraft uses lasers to destroy enemies. The laser immediately eliminates the nearest enemy at the same
xpos
when fired.Implementing this using the
touching
andonTouched
API is not feasible because:laser
sprite is created and the "touching" event is utilized between the laser and enemies, it becomes challenging to identify "the nearest enemy" since each enemy cannot determine its proximity.In this scenario, game developers may need to manage a global sprite array, which simplifies the logic:
However, there’s a complication:
spx
lacks an appropriate type definition forSprite
.The
Sprite
type currently refers to astruct
, while any enemy in the game is an instance ofEnemy
:So it is inappropriate to use
[]Sprite
to store enemy instances.Using
[]Enemy
for the enemies array is inappropriate either because:[]*Enemy
to store the correct data, while we want to avoid pointer usage by game developers.[]*SmallEnemy
or[]*BigEnemy
?I propose renaming the existing
type Sprite struct{}
toSpriteImpl
and defining a new typeSprite
:With this structure, the sprite array logic will function as intended.
Please share your ideas here, @xushiwei @JiepengTan.