Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type definition for sprite #316

Closed
nighca opened this issue Aug 20, 2024 · 3 comments · Fixed by #338
Closed

Type definition for sprite #316

nighca opened this issue Aug 20, 2024 · 3 comments · Fixed by #338

Comments

@nighca
Copy link
Collaborator

nighca commented Aug 20, 2024

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 and onTouched API is not feasible because:

  • There is no "touching" event triggered.
  • Even if a 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:

// main.spx

var (
	enemies []Sprite
)

func onPlayerAttack() {
	// Locate the nearest enemy with the same xpos as the player
	// Iterate through `enemies` and use `enemy.xpos()` / `enemy.ypos()`
}
// Enemy.spx

onCloned => {
	enemies = append(enemies, this)
}

However, there’s a complication: spx lacks an appropriate type definition for Sprite.

The Sprite type currently refers to a struct, while any enemy in the game is an instance of Enemy:

type Enemy struct {
	spx.Sprite
	*Game
}

So it is inappropriate to use []Sprite to store enemy instances.

Using []Enemy for the enemies array is inappropriate either because:

  • We need []*Enemy to store the correct data, while we want to avoid pointer usage by game developers.
  • If multiple enemy types exist (with different appearances and behaviors), representing them could lead to ambiguity—should we use []*SmallEnemy or []*BigEnemy?

I propose renaming the existing type Sprite struct{} to SpriteImpl and defining a new type Sprite:

type Sprite interface {
	Visible() bool
	Xpos() float64
	Ypos() float64
	// Any methods intended for game developers will be defined in this interface
}

type SpriteImpl struct {
	// Additionally, `*SpriteImpl` will implement `Sprite`, as will sprite classes like `*Enemy`
}

With this structure, the sprite array logic will function as intended.

Please share your ideas here, @xushiwei @JiepengTan.

@JiepengTan
Copy link
Collaborator

@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)

@nighca
Copy link
Collaborator Author

nighca commented Aug 30, 2024

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.

@nighca
Copy link
Collaborator Author

nighca commented Sep 20, 2024

And, it is expected to trigger auto-binding with code like this:

var (
	Enemy Sprite
)

spx will try to load sprite with name Enemy and bind the instance value to variable Enemy.

@nighca nighca mentioned this issue Sep 25, 2024
@nighca nighca linked a pull request Sep 25, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants