-
Notifications
You must be signed in to change notification settings - Fork 1
/
entity.go
128 lines (110 loc) · 2.89 KB
/
entity.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
package main
import (
"math"
"math/rand"
"time"
)
var r *rand.Rand
func init() {
r = rand.New(rand.NewSource(time.Now().UnixNano()))
}
// RandomCoordinates returns a set of corrdinates binded by the factor provided.
func RandomCoordinates(factor float64) Coordinates {
return Coordinates{
X: r.Float64() * factor,
Y: r.Float64() * factor,
}
}
// RandomAngle returns a angle randomly.
func RandomAngle() float64 {
return r.Float64() * 2 * math.Pi
}
// BoundingBox describes a box that contains a top left and a top right point.
type BoundingBox struct {
Max, Min Coordinates
}
// Coordinates stores float64 x, y data.
type Coordinates struct {
X float64 `json:"x"`
Y float64 `json:"y"`
}
// Inside returns true if the coordinate is within the bounding box.
func (c *Coordinates) Inside(r BoundingBox) bool {
return c.X >= r.Min.X && c.X <= r.Max.X && c.Y >= r.Min.Y && c.Y <= r.Max.Y
}
// NewEntity creates a new entity that moves with a given thrust.
func NewEntity(thrust, posx, posy, velx, vely, angle float64) Entity {
return Entity{
Position: Coordinates{
X: posx,
Y: posy,
},
Velocity: Coordinates{
X: velx,
Y: vely,
},
Angle: angle,
thrust: thrust,
}
}
// CommandUpdate describes a change in the state for an Entity.
type CommandUpdate struct {
Forward bool `json:"f"`
Backward bool `json:"b"`
Left bool `json:"l"`
Right bool `json:"r"`
}
// Entity represents a component that moves around the screen
type Entity struct {
Position Coordinates `json:"p"`
Velocity Coordinates `json:"v"`
Angle float64 `json:"a"`
Updates CommandUpdate `json:"-"`
Updated CommandUpdate `json:"u"`
Size float64 `json:"-"`
thrust float64
}
// Process takes a command and flips the flag.
func (e *Entity) Process(command Command) {
switch command.Message {
case "left":
e.Updates.Left = true
case "right":
e.Updates.Right = true
case "up":
e.Updates.Forward = true
case "down":
e.Updates.Backward = true
}
}
// Simulate executes the simulation steps in order to move the entity to it's
// new location and apply any pending updates/changes.
func (e *Entity) Simulate() {
var angleNot = e.Angle
var velocityNot = e.Velocity
// mark that we did what they thought we did
e.Updated = e.Updates
// apply YAW
if e.Updates.Left != e.Updates.Right {
if e.Updates.Left {
e.Angle -= angleStep
} else {
e.Angle += angleStep
}
}
// apply THRUST
if e.Updates.Forward != e.Updates.Backward {
if e.Updates.Forward {
e.Velocity.X += e.thrust * math.Cos(angleNot)
e.Velocity.Y += e.thrust * math.Sin(angleNot)
} else {
e.Velocity.X -= e.thrust * math.Cos(angleNot)
e.Velocity.Y -= e.thrust * math.Sin(angleNot)
}
}
// reset the updates to be performed
e.Updates = CommandUpdate{}
// update POSITION
e.Position.X += halfTimestep * (velocityNot.X + e.Velocity.X)
e.Position.Y += halfTimestep * (velocityNot.Y + e.Velocity.Y)
}