add derelicts
This commit is contained in:
parent
abe3f3d688
commit
5bc4fa00bb
Binary file not shown.
After Width: | Height: | Size: 314 B |
|
@ -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
|
||||
|
|
32
gfx/gfx.go
32
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")
|
||||
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: %w", err)
|
||||
return nil, fmt.Errorf("load picture %q: %w", file, err)
|
||||
}
|
||||
|
||||
ob, err := loadPicture("rock.png")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("load picture: %w", err)
|
||||
*field = p
|
||||
}
|
||||
|
||||
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)
|
||||
|
|
Loading…
Reference in New Issue