/
tank.go
133 lines (105 loc) · 3.44 KB
/
tank.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package tanklets
import (
"fmt"
"time"
"github.com/go-gl/mathgl/mgl32"
"github.com/jakecoffman/cp"
"github.com/jakecoffman/tanklets/pkt"
)
// default tank attributes (power-ups could change them!)
const (
TankWidth = 20
TankHeight = 32
TurretWidth = 4
TurretHeight = 15
TurnSpeed = 3
MaxSpeed = 60
ShotCooldown = 333 * time.Millisecond
)
type Tank struct {
// network
ID PlayerID
// physics
Turret
*cp.Body
*cp.Shape
ControlBody *cp.Body
width, height float64
Color mgl32.Vec3
Name string
Score int
LastShot time.Time
Destroyed bool
NextMove, LastMove pkt.Move
Aim float32
LastPkt time.Time
Ready bool
}
type Turret struct {
*cp.Body
*cp.Shape
width, height float64
}
func (g *Game) NewTank(id PlayerID, color mgl32.Vec3) *Tank {
tank := &Tank{
ID: id,
Name: fmt.Sprintf("Player %v", id),
Color: color,
}
tank.Body = g.Space.AddBody(cp.NewBody(1, cp.MomentForBox(1, TankWidth, TankHeight)))
tankShape := g.Space.AddShape(cp.NewBox(tank.Body, TankWidth, TankHeight, 0))
tankShape.SetElasticity(0)
tankShape.SetFriction(1)
tankShape.SetFilter(cp.NewShapeFilter(uint(id), PlayerMaskBit, PlayerMaskBit))
tankShape.UserData = tank
tank.ControlBody = g.Space.AddBody(cp.NewKinematicBody())
pivot := g.Space.AddConstraint(cp.NewPivotJoint2(tank.ControlBody, tank.Body, cp.Vector{}, cp.Vector{}))
pivot.SetMaxBias(0) // prevent joint from sucking the tank in
pivot.SetMaxForce(10000) // prevent tanks from spinning crazy
g.Space.AddConstraint(cp.NewGearJoint(tank.ControlBody, tank.Body, 0.0, 1.0))
//gear.SetErrorBias(0) // idk
//gear.SetMaxBias(1.2) // idk
//gear.SetMaxForce(50000) // don't set or tank will go through walls
tank.Turret.Body = g.Space.AddBody(cp.NewKinematicBody())
tank.Turret.Shape = g.Space.AddShape(cp.NewSegment(tank.Turret.Body, cp.Vector{0, 0}, cp.Vector{TurretHeight, 0}, TurretWidth))
tank.Turret.Shape.SetFilter(cp.NewShapeFilter(uint(id), ^cp.ALL_CATEGORIES, ^cp.ALL_CATEGORIES))
circlePart := g.Space.AddShape(cp.NewCircle(tank.Turret.Body, 10, cp.Vector{}))
circlePart.SetFilter(cp.NewShapeFilter(uint(id), ^cp.ALL_CATEGORIES, ^cp.ALL_CATEGORIES))
return tank
}
func (tank *Tank) Update(dt float64) {
}
func (tank *Tank) FixedUpdate() {
if tank.Destroyed {
// slowly stop, looks cool
tank.ControlBody.SetAngularVelocity(tank.ControlBody.AngularVelocity() * .99)
tank.ControlBody.SetVelocityVector(tank.ControlBody.Velocity().Mult(.99))
return
}
move := tank.NextMove
if !(move.Turn == 0 && tank.LastMove.Turn == 0) {
tank.ControlBody.SetAngularVelocity(float64(move.Turn) * TurnSpeed)
}
if !(move.Throttle == 0 && tank.LastMove.Throttle == 0) {
tank.ControlBody.SetVelocityVector(tank.Body.Rotation().Rotate(cp.Vector{Y: float64(move.Throttle) * MaxSpeed}))
}
tank.LastMove = tank.NextMove
tank.Turret.SetPosition(tank.Position())
tank.Turret.SetAngle(float64(tank.Aim))
}
var locationSequence uint64
// gather important data to transmit
func (tank *Tank) Location() pkt.Location {
locationSequence++
return pkt.Location{
ID: tank.ID,
Sequence: locationSequence,
X: float32(tank.Body.Position().X),
Y: float32(tank.Body.Position().Y),
Angle: float32(tank.Body.Angle()),
AngularVelocity: float32(tank.Body.AngularVelocity()),
Vx: float32(tank.Body.Velocity().X),
Vy: float32(tank.Body.Velocity().Y),
Turret: float32(tank.Turret.Angle()),
}
}