add derelicts

This commit is contained in:
Luke Meyers 2020-02-08 23:11:26 -08:00
parent abe3f3d688
commit 5bc4fa00bb
3 changed files with 23 additions and 18 deletions

BIN
derelict.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 314 B

View File

@ -8,6 +8,7 @@ type State struct {
Teams []Team Teams []Team
SpawnPoints map[int]SpawnPoint // keys are racer IDs SpawnPoints map[int]SpawnPoint // keys are racer IDs
Obstacles []Obstacle Obstacles []Obstacle
Derelicts []Obstacle
GameOver bool GameOver bool
} }
@ -129,8 +130,8 @@ func updateTeam(s State, t Team) State {
} }
func destroyRacer(s State, r Racer) State { func destroyRacer(s State, r Racer) State {
// insert obstacle where racer was // insert derelict where racer was
s.Obstacles = append(s.Obstacles, Obstacle{Position: r.Position}) s.Derelicts = append(s.Derelicts, Obstacle{Position: r.Position})
// spawn racer back at starting position // spawn racer back at starting position
r.Position = s.SpawnPoints[r.ID].Pos r.Position = s.SpawnPoints[r.ID].Pos

View File

@ -64,23 +64,23 @@ type context struct {
type SpriteBank struct { type SpriteBank struct {
racer pixel.Picture racer pixel.Picture
obstacle pixel.Picture obstacle pixel.Picture
derelict pixel.Picture
} }
func NewSpriteBank() (*SpriteBank, error) { func NewSpriteBank() (*SpriteBank, error) {
racer, err := loadPicture("shuttle.png") 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 { if err != nil {
return nil, fmt.Errorf("load picture: %w", err) 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 &sb, nil
return &SpriteBank{
racer: racer,
obstacle: ob,
}, nil
} }
func loadPicture(path string) (pixel.Picture, error) { 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) renderBackground(w, bgBatch)
oBatch := pixel.NewBatch(new(pixel.TrianglesData), sb.obstacle) oBatch := pixel.NewBatch(new(pixel.TrianglesData), sb.obstacle)
renderObstacles(sNew, w, oBatch, sb.obstacle) renderObstacles(sNew.Obstacles, w, oBatch, sb.obstacle)
oBatch.Draw(w) 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) sBatch := pixel.NewBatch(new(pixel.TrianglesData), nil)
renderSpawnPoints(sBatch, sNew.SpawnPoints, w.Bounds()) renderSpawnPoints(sBatch, sNew.SpawnPoints, w.Bounds())
sBatch.Draw(w) 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) 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() b := w.Bounds()
im := imdraw.New(nil) im := imdraw.New(nil)
for _, o := range s.Obstacles { for _, o := range os {
pos := lanePos(o.Position.Pos, o.Position.Lane, racerWidth, b) pos := lanePos(o.Position.Pos, o.Position.Lane, racerWidth, b)
im.Push(pos) im.Push(pos)