From 5bc4fa00bba501de95bbd7dd0086442edf16dab5 Mon Sep 17 00:00:00 2001 From: Luke Meyers Date: Sat, 8 Feb 2020 23:11:26 -0800 Subject: [PATCH] add derelicts --- derelict.png | Bin 0 -> 314 bytes game/game.go | 5 +++-- gfx/gfx.go | 36 ++++++++++++++++++++---------------- 3 files changed, 23 insertions(+), 18 deletions(-) create mode 100644 derelict.png diff --git a/derelict.png b/derelict.png new file mode 100644 index 0000000000000000000000000000000000000000..96db6cfefa985ba739aa5e3af16679dfedcac7fc GIT binary patch literal 314 zcmV-A0mc4_P)Px#^hrcPR9J=WmQfCaAP__c)63XPG#*C|1jjMGL_JRX1C-z{tU+i$n8Xj)$UKl8 z1cruaz1`JZ6Zc@%x(f&k;2|QA{s1vZD+ra`Uulw#EdyA!#&g@MoFB^qYm5lwMipQC zUJc+r8>}&YRY?$X1zLz(kqO^cyL6L9h5!y76}%<@xX*LA;uq6F&eXWN_wf+L9H3?* zsg>Y7geV7U#X$hVAZn>D0J+Y}0$5`XFH37BbC)$rA!)y>a)5(M^Wf(}L`HFL*Gd#m zqI4#Z0Io-?Bt4A&?l=x0)J~TUdPbrgXk`vCdmu4xW}Z{G-}0fM4;t5OU9yYtm;e9( M07*qoM6N<$f;6jn-v9sr literal 0 HcmV?d00001 diff --git a/game/game.go b/game/game.go index 8e392b9..6b3304f 100644 --- a/game/game.go +++ b/game/game.go @@ -8,6 +8,7 @@ type State struct { Teams []Team SpawnPoints map[int]SpawnPoint // keys are racer IDs Obstacles []Obstacle + Derelicts []Obstacle GameOver bool } @@ -129,8 +130,8 @@ func updateTeam(s State, t Team) State { } func destroyRacer(s State, r Racer) State { - // insert obstacle where racer was - s.Obstacles = append(s.Obstacles, Obstacle{Position: r.Position}) + // insert derelict where racer was + s.Derelicts = append(s.Derelicts, Obstacle{Position: r.Position}) // spawn racer back at starting position r.Position = s.SpawnPoints[r.ID].Pos diff --git a/gfx/gfx.go b/gfx/gfx.go index 740370e..d9d8ef8 100644 --- a/gfx/gfx.go +++ b/gfx/gfx.go @@ -64,23 +64,23 @@ type context struct { type SpriteBank struct { racer pixel.Picture obstacle pixel.Picture + derelict pixel.Picture } func NewSpriteBank() (*SpriteBank, error) { - racer, err := loadPicture("shuttle.png") - if err != nil { - return nil, fmt.Errorf("load picture: %w", err) + var sb SpriteBank + for file, field := range map[string]*pixel.Picture{ + "shuttle.png": &sb.racer, + "rock.png": &sb.obstacle, + "derelict.png": &sb.derelict, + } { + p, err := loadPicture(file) + if err != nil { + return nil, fmt.Errorf("load picture %q: %w", file, err) + } + *field = p } - - ob, err := loadPicture("rock.png") - if err != nil { - return nil, fmt.Errorf("load picture: %w", err) - } - - return &SpriteBank{ - racer: racer, - obstacle: ob, - }, nil + return &sb, nil } func loadPicture(path string) (pixel.Picture, error) { @@ -101,9 +101,13 @@ func Render(rs renderState, sOld, sNew game.State, w *pixelgl.Window, sb SpriteB renderBackground(w, bgBatch) oBatch := pixel.NewBatch(new(pixel.TrianglesData), sb.obstacle) - renderObstacles(sNew, w, oBatch, sb.obstacle) + renderObstacles(sNew.Obstacles, w, oBatch, sb.obstacle) oBatch.Draw(w) + dBatch := pixel.NewBatch(new(pixel.TrianglesData), sb.derelict) + renderObstacles(sNew.Derelicts, w, dBatch, sb.derelict) + dBatch.Draw(w) + sBatch := pixel.NewBatch(new(pixel.TrianglesData), nil) renderSpawnPoints(sBatch, sNew.SpawnPoints, w.Bounds()) sBatch.Draw(w) @@ -263,11 +267,11 @@ func lanePos(pos, lane int, width float64, bounds pixel.Rect) pixel.Vec { bounds.Min.Y+float64(lane+1)*vOffset) } -func renderObstacles(s game.State, w *pixelgl.Window, batch *pixel.Batch, pic pixel.Picture) { +func renderObstacles(os []game.Obstacle, w *pixelgl.Window, batch *pixel.Batch, pic pixel.Picture) { b := w.Bounds() im := imdraw.New(nil) - for _, o := range s.Obstacles { + for _, o := range os { pos := lanePos(o.Position.Pos, o.Position.Lane, racerWidth, b) im.Push(pos)