Browse Source

game types, deathmatch and melee

master
Fraser Graham 6 years ago
parent
commit
9583de2ffa
4 changed files with 95 additions and 28 deletions
  1. +42
    -0
      deathmatch.go
  2. +13
    -27
      game.go
  3. +39
    -0
      melee.go
  4. +1
    -1
      robot.go

+ 42
- 0
deathmatch.go View File

@ -0,0 +1,42 @@
package main
import (
"log"
)
type deathmatch struct {
}
func (g *deathmatch) setup(gg *game) {
}
func (g *deathmatch) gameOver(gg *game) (bool, *GameOver) {
over := false
var stats *GameOver
if gg.players_remaining <= 1 && len(gg.players) > 1 {
gg.obstacles = GenerateObstacles(gg.obstacle_count, gg.width, gg.height)
log.Printf("game %s: game over", gg.id)
stats = NewGameOver()
for p := range gg.players {
for _, r := range p.Robots {
if r.Health > 0 {
log.Printf("Robot %v Survived", r.Id)
gg.winners.Lock()
gg.winners.m[r.Id] += 1
gg.winners.Unlock()
stats.Winners = append(stats.Winners, r.Id)
}
r.reset(gg)
}
}
over = true
}
return over, stats
}
func (g *deathmatch) tick(gg *game, payload *Boardstate) {
}

+ 13
- 27
game.go View File

@ -66,6 +66,13 @@ type game struct {
repair_rate float32
tick_duration int
winners WinnerMap
mode GameMode
}
type GameMode interface {
setup(g *game)
tick(gg *game, payload *Boardstate)
gameOver(gg *game) (bool, *GameOver)
}
func NewGame(id string, width, height float32, obstacles, tick, maxPoints int) *game {
@ -91,7 +98,11 @@ func NewGame(id string, width, height float32, obstacles, tick, maxPoints int) *
tick_duration: tick,
players_remaining: 2,
winners: WinnerMap{m: make(map[string]int)},
mode: &melee{respawn: make(map[*Robot]float64)},
}
g.mode.setup(g)
log.Printf("NewGame: %+v", g)
return g
}
@ -285,11 +296,12 @@ func (g *game) run() {
}
// UPDATE GAME STATE
if end, data := g.gameOver(); end {
if end, data := g.mode.gameOver(g); end {
g.sendGameOver(data)
}
g.tick(payload)
g.mode.tick(g, payload)
t1 = time.Now()
if *verbose {
@ -307,32 +319,6 @@ func (g *game) run() {
}
}
func (g *game) gameOver() (bool, *GameOver) {
over := false
var stats *GameOver
if g.players_remaining <= 1 && len(g.players) > 1 {
g.obstacles = GenerateObstacles(g.obstacle_count, g.width, g.height)
log.Printf("game %s: game over", g.id)
stats = NewGameOver()
for p := range g.players {
for _, r := range p.Robots {
if r.Health > 0 {
log.Printf("Robot %v Survived", r.Id)
g.winners.Lock()
g.winners.m[r.Id] += 1
g.winners.Unlock()
stats.Winners = append(stats.Winners, r.Id)
}
r.reset(g)
}
}
over = true
}
return over, stats
}
func (g *game) sendGameOver(eg *GameOver) {
log.Printf("sending out game over message: %+v", eg)
for p := range g.players {

+ 39
- 0
melee.go View File

@ -0,0 +1,39 @@
package main
import (
"log"
)
type melee struct {
respawn map[*Robot]float64
respawn_timer float64
}
func (g *melee) setup(gg *game) {
g.respawn_timer = 5000
}
func (g *melee) gameOver(gg *game) (bool, *GameOver) {
return false, &GameOver{}
}
func (g *melee) tick(gg *game, payload *Boardstate) {
for p := range gg.players {
for _, r := range p.Robots {
_, ok := g.respawn[r]
if r.Health <= 0 && !ok {
g.respawn[r] = g.respawn_timer
log.Printf("%v Died, Respawn in %v", r.Name, g.respawn_timer)
}
}
}
for r, _ := range g.respawn {
g.respawn[r] -= float64(gg.tick_duration)
if g.respawn[r] <= 0 {
log.Printf("%v Respawned", r.Name)
r.reset(gg)
delete(g.respawn, r)
}
}
}

+ 1
- 1
robot.go View File

@ -187,7 +187,7 @@ func (r *Robot) checkCollisions(g *game, move_vector v.Vector2d) (bool, v.Point2
collision, move_collision, translation := v.PolyPolyIntersection(
bot_polygon, move_vector, player_rect)
if collision || move_collision {
return true, r.Position.Add(move_vector).Add(translation.Scale(1.2)), bot
return true, r.Position.Add(move_vector).Add(translation.Scale(1.1)), bot
}
}
}

Loading…
Cancel
Save