diff --git a/camera.go b/camera.go index 2571530b..7f49ec1b 100644 --- a/camera.go +++ b/camera.go @@ -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 { diff --git a/game.go b/game.go index 56f082eb..bc613f8a 100644 --- a/game.go +++ b/game.go @@ -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 @@ -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 { @@ -125,8 +121,8 @@ 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) } @@ -134,7 +130,7 @@ func (p *Game) newSpriteAndLoad(name string, tySpr reflect.Type, g reflect.Value 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 { @@ -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] @@ -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() @@ -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() @@ -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) @@ -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() @@ -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) } @@ -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 @@ -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) @@ -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) } @@ -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 { @@ -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) @@ -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() @@ -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) @@ -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() ) // ----------------------------------------------------------------------------- @@ -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") diff --git a/sprite.go b/sprite.go index 9b053e82..d614c625 100644 --- a/sprite.go +++ b/sprite.go @@ -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) @@ -417,18 +417,18 @@ 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 { @@ -436,18 +436,18 @@ func Gopt_SpriteImpl_Clone__1(sprite Spriter, data interface{}) { } } -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 { @@ -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")