/
entity.go
182 lines (157 loc) · 4.21 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package main
import (
"math"
"github.com/go-gl/gl"
glfw "github.com/go-gl/glfw3"
)
type Entity struct {
Shape Polygon
PosX float64
PosY float64
Angle float64
TurnRate float64
VelocityX float64
VelocityY float64
AccelerationRate float64
MaxVelocity float64
rotateLeft bool
rotateRight bool
accelerate bool
decelerate bool
isAlive bool
createdTime float64
lastUpdatedTime float64
}
func NewEntity(shape Polygon, x, y, angle, turnrate, vX, vY, accel, maxvel float64) *Entity {
return &Entity{
Shape: shape,
PosX: x,
PosY: y,
Angle: angle,
TurnRate: turnrate,
VelocityX: vX,
VelocityY: vY,
AccelerationRate: accel,
MaxVelocity: maxvel,
rotateLeft: false,
rotateRight: false,
accelerate: false,
decelerate: false,
isAlive: true,
createdTime: glfw.GetTime(),
lastUpdatedTime: glfw.GetTime(),
}
}
func (ent *Entity) Draw(invertColors bool) {
if ent.IsAlive() {
//gl.LoadIdentity()
gl.Begin(gl.POLYGON)
for v := range ent.Shape.Vectors {
if invertColors {
ent.Color3d(ent.Shape.Colors[v])
} else {
gl.Color3d(ent.Shape.Colors[v].R, ent.Shape.Colors[v].G, ent.Shape.Colors[v].B)
}
ent.GlVertex2d(ent.Shape.Vectors[v])
}
gl.End()
}
}
func (ent *Entity) Color3d(c Color) {
gl.Color3d(Colorize(c.R), Colorize(c.G), Colorize(c.B))
}
func (ent *Entity) GlVertex2d(v Vector) {
x, y := v.Rotate(ent.Angle)
gl.Vertex2d(ent.PosX+x, ent.PosY+y)
}
func (ent *Entity) RotateLeft(flag bool) {
ent.rotateLeft = flag
}
func (ent *Entity) RotateRight(flag bool) {
ent.rotateRight = flag
}
func (ent *Entity) Accelerate(flag bool) {
ent.accelerate = flag
}
func (ent *Entity) Decelerate(flag bool) {
//ent.decelerate = flag
}
func (ent *Entity) Update() {
if paused {
ent.lastUpdatedTime = glfw.GetTime()
} else {
timediff := (glfw.GetTime() - ent.lastUpdatedTime) * 500
ent.lastUpdatedTime = glfw.GetTime()
var rad float64 = ((ent.Angle) * math.Pi) / 180
// rotation
if ent.rotateLeft {
ent.Angle = ent.Angle - (ent.TurnRate * timediff)
if ent.Angle < 0 {
ent.Angle += 360
}
} else if ent.rotateRight {
ent.Angle = ent.Angle + (ent.TurnRate * timediff)
if ent.Angle > 360 {
ent.Angle -= 360
}
}
/*
0° Sin(0), Cos(1)
90° Sin(1), Cos(0)
180° Sin(0), Cos(-1)
270° Sin(-1), Cos(0)
*/
if ent.accelerate {
ent.VelocityX = ent.VelocityX + (ent.AccelerationRate * math.Sin(rad))
ent.VelocityY = ent.VelocityY + (ent.AccelerationRate * math.Cos(rad))
} else if ent.decelerate {
ent.VelocityX = ent.VelocityX - (ent.AccelerationRate * math.Sin(rad))
ent.VelocityY = ent.VelocityY - (ent.AccelerationRate * math.Cos(rad))
}
// max velocity
totalVelocity := math.Sqrt(ent.VelocityX*ent.VelocityX + ent.VelocityY*ent.VelocityY)
if totalVelocity > ent.MaxVelocity {
ent.VelocityX = ent.VelocityX / totalVelocity
ent.VelocityY = ent.VelocityY / totalVelocity
ent.VelocityX = ent.VelocityX * ent.MaxVelocity
ent.VelocityY = ent.VelocityY * ent.MaxVelocity
}
// move
ent.PosX = ent.VelocityX*timediff + ent.PosX
ent.PosY = ent.VelocityY*timediff + ent.PosY
// crude zone clipping
// TODO: for now it works, but needs to be updated for seamless clipping..
if ent.PosX > gameWidth {
ent.PosX -= gameWidth
} else if ent.PosX < 0 {
ent.PosX += gameWidth
}
if ent.PosY > gameHeight {
ent.PosY -= gameHeight
} else if ent.PosY < 0 {
ent.PosY += gameHeight
}
}
}
func (ent *Entity) AddFrictionToVelocity(friction float64) {
frict := (friction / 100)
newX := ent.VelocityX * (1 - frict)
if math.Abs(newX) < frict/10 {
newX = 0
}
ent.VelocityX = newX
newY := ent.VelocityY * (1 - frict)
if math.Abs(newY) < frict/10 {
newY = 0
}
ent.VelocityY = newY
}
func (ent *Entity) IsAlive() bool {
return ent.isAlive
}
func (ent *Entity) Destroy() {
ent.isAlive = false
}