obstacle support
This commit is contained in:
parent
de68ec2e7b
commit
14f6c65e86
8
game.go
8
game.go
@ -22,6 +22,7 @@ type Boardstate struct {
|
|||||||
OtherRobots []OtherRobot `json:"robots"`
|
OtherRobots []OtherRobot `json:"robots"`
|
||||||
Projectiles []Projectile `json:"projectiles"`
|
Projectiles []Projectile `json:"projectiles"`
|
||||||
Splosions []Splosion `json:"splosions"`
|
Splosions []Splosion `json:"splosions"`
|
||||||
|
Obstacles []Obstacle `json:"obj"`
|
||||||
Reset bool `json:"reset"`
|
Reset bool `json:"reset"`
|
||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
Turn int `json:"turn"`
|
Turn int `json:"turn"`
|
||||||
@ -85,6 +86,7 @@ type game struct {
|
|||||||
players map[*player]bool
|
players map[*player]bool
|
||||||
projectiles map[*Projectile]bool
|
projectiles map[*Projectile]bool
|
||||||
splosions map[*Splosion]bool
|
splosions map[*Splosion]bool
|
||||||
|
obstacles []Obstacle
|
||||||
register chan *player
|
register chan *player
|
||||||
unregister chan *player
|
unregister chan *player
|
||||||
turn int
|
turn int
|
||||||
@ -102,6 +104,7 @@ func NewGame(id string, width, height float32) *game {
|
|||||||
unregister: make(chan *player, maxPlayer),
|
unregister: make(chan *player, maxPlayer),
|
||||||
projectiles: make(map[*Projectile]bool),
|
projectiles: make(map[*Projectile]bool),
|
||||||
splosions: make(map[*Splosion]bool),
|
splosions: make(map[*Splosion]bool),
|
||||||
|
obstacles: GenerateObstacles(5, width, height),
|
||||||
players: make(map[*player]bool),
|
players: make(map[*player]bool),
|
||||||
turn: 0,
|
turn: 0,
|
||||||
width: width,
|
width: width,
|
||||||
@ -117,6 +120,8 @@ func NewGame(id string, width, height float32) *game {
|
|||||||
func (g *game) tick(payload *Boardstate) int {
|
func (g *game) tick(payload *Boardstate) int {
|
||||||
robots_remaining := 0
|
robots_remaining := 0
|
||||||
|
|
||||||
|
payload.Obstacles = g.obstacles
|
||||||
|
|
||||||
// Update Players
|
// Update Players
|
||||||
for p := range g.players {
|
for p := range g.players {
|
||||||
if p.Robot.Health > 0 {
|
if p.Robot.Health > 0 {
|
||||||
@ -163,6 +168,7 @@ func (g *game) send_update(payload *Boardstate) {
|
|||||||
player_payload := NewBoardstate()
|
player_payload := NewBoardstate()
|
||||||
player_payload.Projectiles = payload.Projectiles
|
player_payload.Projectiles = payload.Projectiles
|
||||||
player_payload.Splosions = payload.Splosions
|
player_payload.Splosions = payload.Splosions
|
||||||
|
player_payload.Obstacles = payload.Obstacles
|
||||||
player_payload.AllBots = payload.AllBots
|
player_payload.AllBots = payload.AllBots
|
||||||
player_payload.Turn = payload.Turn
|
player_payload.Turn = payload.Turn
|
||||||
player_payload.Reset = payload.Reset
|
player_payload.Reset = payload.Reset
|
||||||
@ -242,7 +248,7 @@ func (g *game) run() {
|
|||||||
log.Printf("Robot %v Wins", p.Robot.Id)
|
log.Printf("Robot %v Wins", p.Robot.Id)
|
||||||
log.Printf("game %s: game over", g.id)
|
log.Printf("game %s: game over", g.id)
|
||||||
}
|
}
|
||||||
p.reset()
|
p.reset(g)
|
||||||
}
|
}
|
||||||
payload.Reset = true
|
payload.Reset = true
|
||||||
} else {
|
} else {
|
||||||
|
29
obstacle.go
Normal file
29
obstacle.go
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
v "bitbucket.org/hackerbots/vector"
|
||||||
|
"math/rand"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Obstacle struct {
|
||||||
|
Bounds v.Rect2d `json:"bounds"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func GenerateObstacles(count int, width, height float32) []Obstacle {
|
||||||
|
out := []Obstacle{}
|
||||||
|
for i := 0; i < count; i++ {
|
||||||
|
x := rand.Float32() * width
|
||||||
|
y := rand.Float32() * height
|
||||||
|
w := rand.Float32() * width / 10
|
||||||
|
h := rand.Float32() * height / 10
|
||||||
|
out = append(
|
||||||
|
out,
|
||||||
|
Obstacle{
|
||||||
|
Bounds: v.Rect2d{
|
||||||
|
A: v.Point2d{X: x, Y: y},
|
||||||
|
B: v.Point2d{X: 20 + x + w, Y: 20 + y + h},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
33
player.go
33
player.go
@ -83,10 +83,20 @@ func (p *player) checkCollisions(g *game, move_vector v.Vector2d) (bool, v.Point
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check Obstacles
|
||||||
|
for _, obj := range g.obstacles {
|
||||||
|
collision, _, pos := v.RectIntersection(obj.Bounds, p.Robot.Position, move_vector)
|
||||||
|
if collision {
|
||||||
|
log.Printf("Object Collision %v hit %v, rect:%v", p.Robot.Position, move_vector, obj.Bounds)
|
||||||
|
return collision, pos, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return collision, intersection_point, nil
|
return collision, intersection_point, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *player) Tick(g *game) {
|
func (p *player) Tick(g *game) {
|
||||||
|
p.Robot.Collision = false
|
||||||
p.scan(g.players)
|
p.scan(g.players)
|
||||||
|
|
||||||
// Adjust Speed
|
// Adjust Speed
|
||||||
@ -129,15 +139,16 @@ func (p *player) Tick(g *game) {
|
|||||||
move_vector := new_heading.Scale(p.Robot.Speed * delta)
|
move_vector := new_heading.Scale(p.Robot.Speed * delta)
|
||||||
collision, _, hit_player := p.checkCollisions(g, move_vector)
|
collision, _, hit_player := p.checkCollisions(g, move_vector)
|
||||||
if collision {
|
if collision {
|
||||||
|
p.Robot.Collision = true
|
||||||
if hit_player != nil {
|
if hit_player != nil {
|
||||||
hit_player.Robot.Health -= int(p.Robot.Speed / 10.0)
|
hit_player.Robot.Health -= int(p.Robot.Speed / 10.0)
|
||||||
hit_player.Robot.Speed = (hit_player.Robot.Speed * 0.5)
|
hit_player.Robot.Speed = (hit_player.Robot.Speed * 0.1)
|
||||||
hit_player.Robot.Heading = p.Robot.Heading
|
hit_player.Robot.Heading = p.Robot.Heading
|
||||||
}
|
}
|
||||||
// p.Robot.Position = intersection_point
|
//p.Robot.Position = intersection_point
|
||||||
p.Robot.Health -= int(p.Robot.Speed / 10.0)
|
p.Robot.Health -= int(p.Robot.Speed / 10.0)
|
||||||
p.Robot.MoveTo = &p.Robot.Position
|
p.Robot.MoveTo = &p.Robot.Position
|
||||||
p.Robot.Speed = (p.Robot.Speed * 0.5)
|
p.Robot.Speed = (p.Robot.Speed * 0.1)
|
||||||
p.Robot.Heading = p.Robot.Heading.Scale(-1.0)
|
p.Robot.Heading = p.Robot.Heading.Scale(-1.0)
|
||||||
} else {
|
} else {
|
||||||
p.Robot.Position = p.Robot.Position.Add(move_vector)
|
p.Robot.Position = p.Robot.Position.Add(move_vector)
|
||||||
@ -197,7 +208,8 @@ func (p *player) fire(projectiles map[*Projectile]bool, turn int) *Projectile {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *player) reset() {
|
func (p *player) reset(g *game) {
|
||||||
|
for {
|
||||||
start_pos := v.Point2d{
|
start_pos := v.Point2d{
|
||||||
X: rand.Float32() * float32(*width),
|
X: rand.Float32() * float32(*width),
|
||||||
Y: rand.Float32() * float32(*height),
|
Y: rand.Float32() * float32(*height),
|
||||||
@ -205,4 +217,17 @@ func (p *player) reset() {
|
|||||||
p.Robot.MoveTo = &start_pos
|
p.Robot.MoveTo = &start_pos
|
||||||
p.Robot.Position = start_pos
|
p.Robot.Position = start_pos
|
||||||
p.Robot.Health = p.Robot.Stats.Hp
|
p.Robot.Health = p.Robot.Stats.Hp
|
||||||
|
|
||||||
|
// Check Obstacles
|
||||||
|
retry := false
|
||||||
|
for _, obj := range g.obstacles {
|
||||||
|
_, inside, _ := v.RectIntersection(obj.Bounds, p.Robot.Position, v.Vector2d{0, 0})
|
||||||
|
if inside {
|
||||||
|
retry = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !retry {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -35,6 +35,16 @@ func (p *Projectile) Tick(g *game) {
|
|||||||
|
|
||||||
travel := newPos.Sub(p.Position)
|
travel := newPos.Sub(p.Position)
|
||||||
arrived, _, _ := v.RectIntersection(r_dest, p.Position, travel)
|
arrived, _, _ := v.RectIntersection(r_dest, p.Position, travel)
|
||||||
|
|
||||||
|
if !arrived {
|
||||||
|
for _, obj := range g.obstacles {
|
||||||
|
collision, _, _ := v.RectIntersection(obj.Bounds, p.Position, travel)
|
||||||
|
if collision {
|
||||||
|
arrived = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if arrived || hit_player {
|
if arrived || hit_player {
|
||||||
delete(g.projectiles, p)
|
delete(g.projectiles, p)
|
||||||
|
|
||||||
|
@ -173,7 +173,7 @@ func addPlayer(ws *websocket.Conn) {
|
|||||||
send: make(chan *Boardstate),
|
send: make(chan *Boardstate),
|
||||||
ws: ws,
|
ws: ws,
|
||||||
}
|
}
|
||||||
p.reset()
|
p.reset(game)
|
||||||
game.register <- p
|
game.register <- p
|
||||||
defer func() {
|
defer func() {
|
||||||
game.unregister <- p
|
game.unregister <- p
|
||||||
|
1
robot.go
1
robot.go
@ -17,6 +17,7 @@ type Robot struct {
|
|||||||
FireAt *v.Point2d `json:"-"`
|
FireAt *v.Point2d `json:"-"`
|
||||||
Scanners []Scanner `json:"scanners"`
|
Scanners []Scanner `json:"scanners"`
|
||||||
LastFired int `json:"-"`
|
LastFired int `json:"-"`
|
||||||
|
Collision bool `json:"collision"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// This is the subset of data we send to players about robots
|
// This is the subset of data we send to players about robots
|
||||||
|
Loading…
Reference in New Issue
Block a user