Skip to content

Commit

Permalink
Replace Spriter with Sprite
Browse files Browse the repository at this point in the history
  • Loading branch information
qlli committed Oct 13, 2024
1 parent 6bde31b commit 48bf577
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 39 deletions.
2 changes: 1 addition & 1 deletion camera.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (c *Camera) On(obj interface{}) {
obj = sp
case *SpriteImpl:
case nil:
case Spriter:
case Sprite:
obj = spriteOf(v)
case specialObj:
if v != Mouse {
Expand Down
51 changes: 23 additions & 28 deletions game.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ type Game struct {
sounds soundMgr
turtle turtleCanvas
typs map[string]reflect.Type // map: name => sprite type, for all sprites
sprs map[string]Spriter // map: name => sprite prototype, for loaded sprites
sprs map[string]Sprite // map: name => sprite prototype, for loaded sprites
items []Shape // shapes on stage (in Zorder), not only sprites

tickMgr tickMgr
Expand All @@ -106,12 +106,8 @@ type Game struct {
isRunned bool
}

type Spriter interface {
Shape
Main()
}
type Gamer interface {
initGame(sprites []Spriter) *Game
initGame(sprites []Sprite) *Game
}

func (p *Game) IsRunned() bool {
Expand All @@ -125,16 +121,16 @@ func (p *Game) getSharedImgs() *sharedImages {
return p.shared
}

func (p *Game) newSpriteAndLoad(name string, tySpr reflect.Type, g reflect.Value) Spriter {
spr := reflect.New(tySpr).Interface().(Spriter)
func (p *Game) newSpriteAndLoad(name string, tySpr reflect.Type, g reflect.Value) Sprite {
spr := reflect.New(tySpr).Interface().(Sprite)
if err := p.loadSprite(spr, name, g); err != nil {
panic(err)
}
// p.sprs[name] = spr (has been set by loadSprite)
return spr
}

func (p *Game) getSpriteProto(tySpr reflect.Type, g reflect.Value) Spriter {
func (p *Game) getSpriteProto(tySpr reflect.Type, g reflect.Value) Sprite {
name := tySpr.Name()
spr, ok := p.sprs[name]
if !ok {
Expand All @@ -143,7 +139,7 @@ func (p *Game) getSpriteProto(tySpr reflect.Type, g reflect.Value) Spriter {
return spr
}

func (p *Game) getSpriteProtoByName(name string, g reflect.Value) Spriter {
func (p *Game) getSpriteProtoByName(name string, g reflect.Value) Sprite {
spr, ok := p.sprs[name]
if !ok {
tySpr, ok := p.typs[name]
Expand All @@ -161,17 +157,17 @@ func (p *Game) reset() {
p.Stop(AllOtherScripts)
p.items = nil
p.isLoaded = false
p.sprs = make(map[string]Spriter)
p.sprs = make(map[string]Sprite)
}

func (p *Game) getGame() *Game {
return p
}

func (p *Game) initGame(sprites []Spriter) *Game {
func (p *Game) initGame(sprites []Sprite) *Game {
p.tickMgr.init()
p.eventSinks.init(&p.sinkMgr, p)
p.sprs = make(map[string]Spriter)
p.sprs = make(map[string]Sprite)
p.typs = make(map[string]reflect.Type)
for _, spr := range sprites {
tySpr := reflect.TypeOf(spr).Elem()
Expand All @@ -181,7 +177,7 @@ func (p *Game) initGame(sprites []Spriter) *Game {
}

// Gopt_Game_Main is required by Go+ compiler as the entry of a .gmx project.
func Gopt_Game_Main(game Gamer, sprites ...Spriter) {
func Gopt_Game_Main(game Gamer, sprites ...Sprite) {
g := game.initGame(sprites)
if me, ok := game.(interface{ MainEntry() }); ok {
me.MainEntry()
Expand Down Expand Up @@ -264,7 +260,7 @@ func Gopt_Game_Run(game Gamer, resource interface{}, gameConf ...*Config) {
}
*fld = media
}
case Spriter:
case Sprite:
if g.canBindSprite(name) {
if err := g.loadSprite(fld, name, v); err != nil {
panic(err)
Expand Down Expand Up @@ -308,7 +304,7 @@ func getFieldPtrOrAlloc(g *Game, v reflect.Value, i int) (name string, val inter
word := unsafe.Pointer(vFld.Addr().Pointer())
ret := reflect.NewAt(typ, word).Interface()

if vFld.Kind() == reflect.Ptr && typ.Implements(tySpriter) {
if vFld.Kind() == reflect.Ptr && typ.Implements(tySprite) {
obj := reflect.New(typ.Elem())
reflect.ValueOf(ret).Elem().Set(obj)
ret = obj.Interface()
Expand Down Expand Up @@ -380,7 +376,7 @@ func (p *Game) canBindSprite(name string) bool {
return true
}

func (p *Game) loadSprite(sprite Spriter, name string, gamer reflect.Value) error {
func (p *Game) loadSprite(sprite Sprite, name string, gamer reflect.Value) error {
if debugLoad {
log.Println("==> LoadSprite", name)
}
Expand All @@ -403,7 +399,7 @@ func (p *Game) loadSprite(sprite Spriter, name string, gamer reflect.Value) erro
return nil
}

func spriteOf(sprite Spriter) *SpriteImpl {
func spriteOf(sprite Sprite) *SpriteImpl {
vSpr := reflect.ValueOf(sprite)
if vSpr.Kind() != reflect.Ptr {
return nil
Expand Down Expand Up @@ -436,7 +432,7 @@ func (p *Game) loadIndex(g reflect.Value, proj *projConfig) (err error) {
p.world = ebiten.NewImage(p.worldWidth_, p.worldHeight_)
p.mapMode = toMapMode(proj.Map.Mode)

inits := make([]Spriter, 0, len(proj.Zorder))
inits := make([]Sprite, 0, len(proj.Zorder))
for _, v := range proj.Zorder {
if name, ok := v.(string); ok {
sp := p.getSpriteProtoByName(name, g)
Expand Down Expand Up @@ -495,7 +491,7 @@ func Gopt_Game_Reload(game Gamer, index interface{}) (err error) {
g.reset()
for i, n := 0, v.NumField(); i < n; i++ {
name, val := getFieldPtrOrAlloc(g, v, i)
if fld, ok := val.(Spriter); ok {
if fld, ok := val.(Sprite); ok {
if err := g.loadSprite(fld, name, v); err != nil {
panic(err)
}
Expand All @@ -512,7 +508,7 @@ func Gopt_Game_Reload(game Gamer, index interface{}) (err error) {

type specsp = map[string]interface{}

func (p *Game) addSpecialShape(g reflect.Value, v specsp, inits []Spriter) []Spriter {
func (p *Game) addSpecialShape(g reflect.Value, v specsp, inits []Sprite) []Sprite {
switch typ := v["type"].(string); typ {
case "stageMonitor", "monitor":
if sm, err := newMonitor(g, v); err == nil {
Expand All @@ -531,10 +527,10 @@ func (p *Game) addSpecialShape(g reflect.Value, v specsp, inits []Spriter) []Spr
return inits
}

func (p *Game) addStageSprite(g reflect.Value, v specsp, inits []Spriter) []Spriter {
func (p *Game) addStageSprite(g reflect.Value, v specsp, inits []Sprite) []Sprite {
target := v["target"].(string)
if val := findObjPtr(g, target, 0); val != nil {
if sp, ok := val.(Spriter); ok {
if sp, ok := val.(Sprite); ok {
dest := spriteOf(sp)
applySpriteProps(dest, v)
p.addShape(dest)
Expand All @@ -561,7 +557,7 @@ func (p *Game) addStageSprite(g reflect.Value, v specsp, inits []Spriter) []Spri
]
}
*/
func (p *Game) addStageSprites(g reflect.Value, v specsp, inits []Spriter) []Spriter {
func (p *Game) addStageSprites(g reflect.Value, v specsp, inits []Sprite) []Sprite {
target := v["target"].(string)
if val := findFieldPtr(g, target, 0); val != nil {
fldSlice := reflect.ValueOf(val).Elem()
Expand All @@ -575,7 +571,7 @@ func (p *Game) addStageSprites(g reflect.Value, v specsp, inits []Spriter) []Spr
} else {
typItemPtr = reflect.PtrTo(typItem)
}
if typItemPtr.Implements(tySpriter) {
if typItemPtr.Implements(tySprite) {
spr := p.getSpriteProto(typItem, g)
items := v["items"].([]interface{})
n := len(items)
Expand All @@ -599,8 +595,7 @@ func (p *Game) addStageSprites(g reflect.Value, v specsp, inits []Spriter) []Spr
}

var (
tySpriter = reflect.TypeOf((*Spriter)(nil)).Elem()
tySprite = reflect.TypeOf((*Sprite)(nil)).Elem()
tySprite = reflect.TypeOf((*Sprite)(nil)).Elem()
)

// -----------------------------------------------------------------------------
Expand Down Expand Up @@ -845,7 +840,7 @@ func (p *Game) objectPos(obj interface{}) (float64, float64) {
mx, my := rand.Intn(worldW), rand.Intn(worldH)
return float64(mx - (worldW >> 1)), float64((worldH >> 1) - my)
}
case Spriter:
case Sprite:
return spriteOf(v).getXY()
}
panic("objectPos: unexpected input")
Expand Down
20 changes: 10 additions & 10 deletions sprite.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,13 +388,13 @@ func applySpriteProps(dest *SpriteImpl, v specsp) {
dest.isCloned_ = false
}

func applySprite(out reflect.Value, sprite Spriter, v specsp) (*SpriteImpl, Spriter) {
func applySprite(out reflect.Value, sprite Sprite, v specsp) (*SpriteImpl, Sprite) {
in := reflect.ValueOf(sprite).Elem()
outPtr := out.Addr().Interface().(Spriter)
outPtr := out.Addr().Interface().(Sprite)
return cloneSprite(out, outPtr, in, v), outPtr
}

func cloneSprite(out reflect.Value, outPtr Spriter, in reflect.Value, v specsp) *SpriteImpl {
func cloneSprite(out reflect.Value, outPtr Sprite, in reflect.Value, v specsp) *SpriteImpl {
dest := spriteOf(outPtr)
func() {
out.Set(in)
Expand All @@ -417,37 +417,37 @@ func cloneSprite(out reflect.Value, outPtr Spriter, in reflect.Value, v specsp)
return dest
}

func Gopt_SpriteImpl_Clone__0(sprite Spriter) {
func Gopt_SpriteImpl_Clone__0(sprite Sprite) {
Gopt_Sprite_Clone__1(sprite, nil)
}

func Gopt_SpriteImpl_Clone__1(sprite Spriter, data interface{}) {
func Gopt_SpriteImpl_Clone__1(sprite Sprite, data interface{}) {
src := spriteOf(sprite)
if debugInstr {
log.Println("Clone", src.name)
}
in := reflect.ValueOf(sprite).Elem()
v := reflect.New(in.Type())
out, outPtr := v.Elem(), v.Interface().(Spriter)
out, outPtr := v.Elem(), v.Interface().(Sprite)
dest := cloneSprite(out, outPtr, in, nil)
src.g.addClonedShape(src, dest)
if dest.hasOnCloned {
dest.doWhenCloned(dest, data)
}
}

func Gopt_Sprite_Clone__0(sprite Spriter) {
func Gopt_Sprite_Clone__0(sprite Sprite) {
Gopt_Sprite_Clone__1(sprite, nil)
}

func Gopt_Sprite_Clone__1(sprite Spriter, data interface{}) {
func Gopt_Sprite_Clone__1(sprite Sprite, data interface{}) {
src := spriteOf(sprite)
if debugInstr {
log.Println("Clone", src.name)
}
in := reflect.ValueOf(sprite).Elem()
v := reflect.New(in.Type())
out, outPtr := v.Elem(), v.Interface().(Spriter)
out, outPtr := v.Elem(), v.Interface().(Sprite)
dest := cloneSprite(out, outPtr, in, nil)
src.g.addClonedShape(src, dest)
if dest.hasOnCloned {
Expand Down Expand Up @@ -1389,7 +1389,7 @@ func (p *SpriteImpl) Touching(obj interface{}) bool {
x, y := p.g.getMousePos()
return p.g.touchingPoint(p, x, y)
}
case Spriter:
case Sprite:
return touchingSprite(p, spriteOf(v))
}
panic("Touching: unexpected input")
Expand Down

0 comments on commit 48bf577

Please sign in to comment.