forked from atolVerderben/tentsuyu
/
gameobject.go
320 lines (261 loc) · 7.07 KB
/
gameobject.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package tentsuyu
import (
"math"
"github.com/rs/xid"
"github.com/hajimehoshi/ebiten/v2"
)
//GameObject is any renderable object
type GameObject interface {
GetPosition() (float64, float64)
SetPosition(float64, float64)
GetWidth() int
GetHeight() int
//Update()
Draw(*ebiten.Image) error
Contains(float64, float64) bool
GetHealth() float64
GetAngle() float64
GetVelocity() (float64, float64)
}
//BasicObject is the bare implementation of a GameObject
type BasicObject struct {
Angle, Speed, PrevX, PrevY float64
VX, VY float64
NotCentered bool
Width, Height int
WidthF, HeightF float64
ID xid.ID
isCentered bool
Velocity *Vector2d
Position *Vector2d
}
//SetCollision2D will allow the object to use more advanced collision functions
func (obj *BasicObject) SetCollision2D(isCircle bool) {
x, y := obj.GetPosition()
if obj.NotCentered {
x += float64(obj.Width) / 2
y += float64(obj.Height) / 2
}
}
//========================================================
//Implement GameObject
//GetSpeed returns the BasicObject speed
func (obj *BasicObject) GetSpeed() float64 {
return obj.Speed
}
//GetVelocity returns Vx and Vy of the BasicObject
func (obj *BasicObject) GetVelocity() (float64, float64) {
//vecX, vecY := math.Cos(obj.Angle)*obj.Speed, math.Sin(obj.Angle)*obj.Speed
return obj.VX, obj.VY //vecX, vecY
}
//GetPosition returns the x,y coords
func (obj BasicObject) GetPosition() (float64, float64) {
return obj.Position.X, obj.Position.Y
}
//GetY returns the y coords
func (obj BasicObject) GetY() float64 {
return obj.Position.Y
}
//GetX returns the x coords
func (obj BasicObject) GetX() float64 {
return obj.Position.X
}
//GetWidth returns width
func (obj BasicObject) GetWidth() int {
return obj.Width
}
//GetHeight returns height
func (obj BasicObject) GetHeight() int {
return obj.Height
}
//GetWidthF returns width as a float64
func (obj BasicObject) GetWidthF() float64 {
return float64(obj.Width)
}
//GetHeightF returns height as a float64
func (obj BasicObject) GetHeightF() float64 {
return float64(obj.Height)
}
//AddPosition increases X and Y position by vX and vY respectively
func (obj *BasicObject) AddPosition(vX, vY float64) {
obj.Position.X += vX
obj.Position.Y += vY
}
//SetPosition of x,y
func (obj *BasicObject) SetPosition(x, y float64) {
obj.Position.X = x
obj.Position.Y = y
}
//SetSize with width , height
func (obj *BasicObject) SetSize(width, height int) {
obj.Width = width
obj.Height = height
obj.WidthF = float64(width)
obj.HeightF = float64(height)
}
//=========================================================
//SetX of the position
func (obj *BasicObject) SetX(x float64) {
obj.Position.X = x
}
//SetY of the position
func (obj *BasicObject) SetY(y float64) {
obj.Position.Y = y
}
//GetAngle of BasicObject
func (obj BasicObject) GetAngle() float64 {
return obj.Angle
}
//SetAngle of BasicObject
func (obj *BasicObject) SetAngle(angle float64) {
obj.Angle = angle
}
//AddAngle adds the provided increment to the angle RADIAN
func (obj *BasicObject) AddAngle(inc float64) {
obj.Angle += inc
if obj.Angle > 2*math.Pi {
obj.Angle = 0
}
if obj.Angle < 0 {
obj.Angle = (2 * math.Pi) + obj.Angle
}
}
//GetSize returns width and height
func (obj BasicObject) GetSize() (int, int) {
return obj.GetWidth(), obj.GetHeight()
}
//AddX adds to the x value
func (obj *BasicObject) AddX(vX float64) {
obj.Position.X += vX
}
//AddY adds to the y value
func (obj *BasicObject) AddY(vY float64) {
obj.Position.Y += vY
}
//NewBasicObject returns a new oject
func NewBasicObject(x, y float64, w, h int) *BasicObject {
obj := &BasicObject{
ID: xid.New(),
Width: w,
Height: h,
WidthF: float64(w),
HeightF: float64(h),
isCentered: true,
Velocity: &Vector2d{},
Position: &Vector2d{X: x, Y: y},
}
return obj
}
//GetID returns the guid of the Basic Object
func (obj BasicObject) GetID() xid.ID {
return obj.ID
}
//GetIDasString returns the string representation of the guid
func (obj BasicObject) GetIDasString() string {
return obj.ID.String()
}
//SetID sets the BasicObject's ID to a new guid
func (obj *BasicObject) SetID() {
obj.ID = xid.New()
}
//Left edge of the rectangle
func (obj *BasicObject) Left() float64 {
x, _ := obj.GetPosition()
if obj.NotCentered {
return x
}
return x - float64(obj.GetWidth()/2)
}
//Right edge of the rectangle
func (obj *BasicObject) Right() float64 {
x, _ := obj.GetPosition()
if obj.NotCentered {
return x + float64(obj.GetWidth())
}
return x + float64(obj.GetWidth()/2)
}
//Top edge of the rectangle
func (obj *BasicObject) Top() float64 {
_, y := obj.GetPosition()
if obj.NotCentered {
return y
}
return y - float64(obj.GetHeight()/2)
}
//Bottom edge of the rectangle
func (obj *BasicObject) Bottom() float64 {
_, y := obj.GetPosition()
if obj.NotCentered {
return y + float64(obj.GetHeight())
}
return y + float64(obj.GetHeight()/2)
}
//Contains returns true if the given point is withing the rectangle of the object
func (obj *BasicObject) Contains(srcX, srcY float64) bool {
if obj.NotCentered == true {
return obj.ContainsNoCenter(srcX, srcY)
}
xIn, yIn := false, false
if srcX <= obj.Right() && srcX >= obj.Left() {
xIn = true
}
if srcY <= obj.Bottom() && srcY >= obj.Top() {
yIn = true
}
return xIn && yIn
}
//LeftNoCenter edge of the rectangle
func (obj *BasicObject) LeftNoCenter() float64 {
x, _ := obj.GetPosition()
return x
}
//RightNoCenter edge of the rectangle
func (obj *BasicObject) RightNoCenter() float64 {
x, _ := obj.GetPosition()
return x + float64(obj.GetWidth())
}
//TopNoCenter edge of the rectangle
func (obj *BasicObject) TopNoCenter() float64 {
_, y := obj.GetPosition()
return y
}
//BottomNoCenter edge of the rectangle
func (obj *BasicObject) BottomNoCenter() float64 {
_, y := obj.GetPosition()
return y + float64(obj.GetHeight())
}
//ContainsNoCenter returns true if the given point is withing the rectangle of the object
func (obj *BasicObject) ContainsNoCenter(srcX, srcY float64) bool {
xIn, yIn := false, false
if srcX <= obj.RightNoCenter() && srcX >= obj.LeftNoCenter() {
xIn = true
}
if srcY <= obj.BottomNoCenter() && srcY >= obj.TopNoCenter() {
yIn = true
}
return xIn && yIn
}
//GetHealth should return BasicObject Health
func (obj *BasicObject) GetHealth() float64 {
return 1.0
}
//Update for GameObject
//This should be overriden by user
func (obj *BasicObject) Update() {
}
//Draw for GameObject
//This should be overriden by user
func (obj *BasicObject) Draw(screen *ebiten.Image) error {
return nil
}
//ReturnVectorPosition returns the X,Y position as a vector2d
//This is useful for vector math
func (obj BasicObject) ReturnVectorPosition() Vector2d {
return *obj.Position //Vector2d{X: obj.X, Y: obj.Y}
}
//SetCentered of the object. If true the object coords refer to the center of the object.
//If false the object coords refer to the top left of the object.
func (obj *BasicObject) SetCentered(isCentered bool) {
obj.NotCentered = !isCentered
obj.isCentered = isCentered
}