cameraaaaa
This commit is contained in:
parent
47680971e5
commit
1bf7920d2f
5 changed files with 50 additions and 11 deletions
32
engine/camera.go
Normal file
32
engine/camera.go
Normal file
|
@ -0,0 +1,32 @@
|
|||
package engine
|
||||
|
||||
import (
|
||||
"image"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
)
|
||||
|
||||
type Camera struct {
|
||||
ID
|
||||
Scene *Scene
|
||||
Transform GeoMDef
|
||||
|
||||
game *Game
|
||||
}
|
||||
|
||||
func (c *Camera) Draw(screen *ebiten.Image, geom ebiten.GeoM) {
|
||||
geom.Concat(*c.Transform.GeoM())
|
||||
c.Scene.Draw(screen, geom)
|
||||
}
|
||||
|
||||
func (c *Camera) Update() error { return c.Scene.Update() }
|
||||
|
||||
func (c *Camera) Scan() []interface{} { return []interface{}{c.Scene} }
|
||||
|
||||
func (c *Camera) Prepare(game *Game) { c.game = game }
|
||||
|
||||
func (c *Camera) Centre(p image.Point) {
|
||||
t := c.Transform.GeoM()
|
||||
t.Reset()
|
||||
t.Translate(float64(c.game.ScreenWidth/2-p.X), float64(c.game.ScreenHeight/2-p.Y))
|
||||
}
|
|
@ -18,7 +18,6 @@ type Scene struct {
|
|||
Disabled bool
|
||||
Hidden bool
|
||||
ID
|
||||
Transform GeoMDef
|
||||
ZPos
|
||||
}
|
||||
|
||||
|
@ -27,8 +26,6 @@ func (s *Scene) Draw(screen *ebiten.Image, geom ebiten.GeoM) {
|
|||
if s.Hidden {
|
||||
return
|
||||
}
|
||||
geom.Concat(*s.Transform.GeoM())
|
||||
|
||||
for _, i := range s.Components {
|
||||
if d, ok := i.(Drawer); ok {
|
||||
d.Draw(screen, geom)
|
||||
|
|
|
@ -18,11 +18,10 @@ type Tilemap struct {
|
|||
Disabled bool
|
||||
Hidden bool
|
||||
ID
|
||||
Map [][]Tile
|
||||
Ersatz bool // "fake wall"
|
||||
Src ImageRef
|
||||
TileSize int
|
||||
Transform GeoMDef
|
||||
Map [][]Tile
|
||||
Ersatz bool // "fake wall"
|
||||
Src ImageRef
|
||||
TileSize int
|
||||
ZPos
|
||||
}
|
||||
|
||||
|
@ -59,7 +58,6 @@ func (t *Tilemap) Draw(screen *ebiten.Image, geom ebiten.GeoM) {
|
|||
}
|
||||
src := t.Src.Image()
|
||||
w, _ := src.Size()
|
||||
geom.Concat(*t.Transform.GeoM())
|
||||
for j, row := range t.Map {
|
||||
for i, tile := range row {
|
||||
if tile == nil {
|
||||
|
|
10
game/aw.go
10
game/aw.go
|
@ -16,6 +16,9 @@ func init() {
|
|||
type Awakeman struct {
|
||||
engine.Sprite
|
||||
|
||||
CameraID string
|
||||
|
||||
camera *engine.Camera
|
||||
vx, vy float64
|
||||
facingLeft bool
|
||||
coyoteTimer int
|
||||
|
@ -65,10 +68,15 @@ func (aw *Awakeman) Update() error {
|
|||
}
|
||||
aw.MoveX(aw.vx, func() { aw.vx = -aw.vx * bounceDampen })
|
||||
aw.MoveY(aw.vy, func() { aw.vy = -aw.vy * bounceDampen })
|
||||
|
||||
aw.camera.Centre(aw.Pos.Add(aw.Size.Div(2)))
|
||||
|
||||
return aw.Sprite.Update()
|
||||
}
|
||||
|
||||
func (aw *Awakeman) Prepare(*engine.Game) {
|
||||
func (aw *Awakeman) Prepare(game *engine.Game) {
|
||||
aw.camera = game.Component(aw.CameraID).(*engine.Camera)
|
||||
|
||||
aw.animIdleLeft = &engine.Anim{Def: engine.AnimDefs["aw_idle_left"]}
|
||||
aw.animIdleRight = &engine.Anim{Def: engine.AnimDefs["aw_idle_right"]}
|
||||
aw.animRunLeft = &engine.Anim{Def: engine.AnimDefs["aw_run_left"]}
|
||||
|
|
6
main.go
6
main.go
|
@ -68,6 +68,7 @@ func main() {
|
|||
Rect: image.Rect(320, 0, 321, 240),
|
||||
},
|
||||
&game.Awakeman{
|
||||
CameraID: "level_1_camera",
|
||||
Sprite: engine.Sprite{
|
||||
ID: "awakeman",
|
||||
Actor: engine.Actor{
|
||||
|
@ -93,7 +94,10 @@ func main() {
|
|||
&engine.GobDumper{
|
||||
KeyCombo: []ebiten.Key{ebiten.KeyControl, ebiten.KeyD},
|
||||
},
|
||||
level1,
|
||||
&engine.Camera{
|
||||
ID: "level_1_camera",
|
||||
Scene: level1,
|
||||
},
|
||||
engine.PerfDisplay{},
|
||||
},
|
||||
},
|
||||
|
|
Loading…
Reference in a new issue