cameraaaaa

This commit is contained in:
Josh Deprez 2021-08-08 22:07:55 +10:00
parent 47680971e5
commit 1bf7920d2f
5 changed files with 50 additions and 11 deletions

32
engine/camera.go Normal file
View 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))
}

View file

@ -18,7 +18,6 @@ type Scene struct {
Disabled bool Disabled bool
Hidden bool Hidden bool
ID ID
Transform GeoMDef
ZPos ZPos
} }
@ -27,8 +26,6 @@ func (s *Scene) Draw(screen *ebiten.Image, geom ebiten.GeoM) {
if s.Hidden { if s.Hidden {
return return
} }
geom.Concat(*s.Transform.GeoM())
for _, i := range s.Components { for _, i := range s.Components {
if d, ok := i.(Drawer); ok { if d, ok := i.(Drawer); ok {
d.Draw(screen, geom) d.Draw(screen, geom)

View file

@ -22,7 +22,6 @@ type Tilemap struct {
Ersatz bool // "fake wall" Ersatz bool // "fake wall"
Src ImageRef Src ImageRef
TileSize int TileSize int
Transform GeoMDef
ZPos ZPos
} }
@ -59,7 +58,6 @@ func (t *Tilemap) Draw(screen *ebiten.Image, geom ebiten.GeoM) {
} }
src := t.Src.Image() src := t.Src.Image()
w, _ := src.Size() w, _ := src.Size()
geom.Concat(*t.Transform.GeoM())
for j, row := range t.Map { for j, row := range t.Map {
for i, tile := range row { for i, tile := range row {
if tile == nil { if tile == nil {

View file

@ -16,6 +16,9 @@ func init() {
type Awakeman struct { type Awakeman struct {
engine.Sprite engine.Sprite
CameraID string
camera *engine.Camera
vx, vy float64 vx, vy float64
facingLeft bool facingLeft bool
coyoteTimer int coyoteTimer int
@ -65,10 +68,15 @@ func (aw *Awakeman) Update() error {
} }
aw.MoveX(aw.vx, func() { aw.vx = -aw.vx * bounceDampen }) aw.MoveX(aw.vx, func() { aw.vx = -aw.vx * bounceDampen })
aw.MoveY(aw.vy, func() { aw.vy = -aw.vy * 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() 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.animIdleLeft = &engine.Anim{Def: engine.AnimDefs["aw_idle_left"]}
aw.animIdleRight = &engine.Anim{Def: engine.AnimDefs["aw_idle_right"]} aw.animIdleRight = &engine.Anim{Def: engine.AnimDefs["aw_idle_right"]}
aw.animRunLeft = &engine.Anim{Def: engine.AnimDefs["aw_run_left"]} aw.animRunLeft = &engine.Anim{Def: engine.AnimDefs["aw_run_left"]}

View file

@ -68,6 +68,7 @@ func main() {
Rect: image.Rect(320, 0, 321, 240), Rect: image.Rect(320, 0, 321, 240),
}, },
&game.Awakeman{ &game.Awakeman{
CameraID: "level_1_camera",
Sprite: engine.Sprite{ Sprite: engine.Sprite{
ID: "awakeman", ID: "awakeman",
Actor: engine.Actor{ Actor: engine.Actor{
@ -93,7 +94,10 @@ func main() {
&engine.GobDumper{ &engine.GobDumper{
KeyCombo: []ebiten.Key{ebiten.KeyControl, ebiten.KeyD}, KeyCombo: []ebiten.Key{ebiten.KeyControl, ebiten.KeyD},
}, },
level1, &engine.Camera{
ID: "level_1_camera",
Scene: level1,
},
engine.PerfDisplay{}, engine.PerfDisplay{},
}, },
}, },