-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameObject.go
59 lines (49 loc) · 1.16 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
package main
import (
"github.com/hajimehoshi/ebiten/v2"
"image/color"
)
type GameObject struct {
image *ebiten.Image
size Vector
position Vector
speed float64
bounds Bounds
}
type Bounds struct {
left float64
right float64
top float64
bottom float64
}
func MakeGameObject(width, height int, x, y, speed float64, color color.RGBA) GameObject {
gameObject := GameObject{
image: ebiten.NewImage(width, height),
size: Vector{float64(width), float64(height)},
speed: speed,
}
gameObject.SetPosition(Vector{x, y})
gameObject.image.Fill(color)
return gameObject
}
func (o *GameObject) SetPosition(v Vector) {
o.position = v
o.bounds = Bounds{
left: v.x - float64(o.size.x)/2,
right: v.x + float64(o.size.x)/2,
top: v.y - float64(o.size.y)/2,
bottom: v.y + float64(o.size.y)/2,
}
}
func (o *GameObject) GetScreenPosition() (float64, float64) {
return o.bounds.left, o.bounds.top
}
func (o *GameObject) Hit(other GameObject) bool {
if o.bounds.right >= other.bounds.left &&
o.bounds.left <= other.bounds.right &&
o.bounds.top <= other.bounds.bottom &&
o.bounds.bottom >= other.bounds.top {
return true
}
return false
}