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
|
package main
import (
"log"
"image"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
)
const (
SPRING_FORCE = 8
)
type Direction int
const (
NONE Direction = iota
LEFT
RIGHT
UP
DOWN
)
type GameObject struct {
game *Game
startx, starty float32
x, y float32
vx, vy float32
offsetX, offsetY int
friction float32
resistance float32
image *ebiten.Image
onGround bool
onCollideUp func(this, other *GameObject) bool
onCollideDown func(this, other *GameObject) bool
onCollideLeft func(this, other *GameObject) bool
onCollideRight func(this, other *GameObject) bool
}
type Player struct {
GameObject
}
func (o * GameObject) Update(tilemap Tilemap, others []*GameObject) {
var direction Direction
o.vy += gravity
o.vx *= o.resistance
o.vy *= o.resistance
o.x += o.vx
if o.vx > 0 {
direction = RIGHT
} else {
direction = LEFT
}
if o.HasCollision(tilemap, others, direction) {
o.x -= o.vx
o.vx = 0
}
o.y += o.vy
if o.vy > 0 {
direction = UP
} else {
direction = DOWN
}
if o.HasCollision(tilemap, others, direction) {
o.onGround = true;
o.vx *= o.friction
o.y -= o.vy
o.vy = 0
} else {
o.onGround = false;
}
}
func (o * GameObject) HasCollision(tilemap Tilemap, others []*GameObject, dir Direction) bool {
if tilemap.CollideObject(o) {
return true
}
for _, obj := range others {
if obj.Collide(o) {
var f func(this, other *GameObject) bool
switch dir {
case UP:
f = obj.onCollideUp
case DOWN:
f = obj.onCollideDown
case LEFT:
f = obj.onCollideLeft
case RIGHT:
f = obj.onCollideRight
}
if f == nil{
return true
}
return f(obj, o)
}
}
return false
}
func (o * GameObject) Draw(screen *ebiten.Image, tilemap Tilemap) {
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(float64(o.x), float64(o.y))
screen.DrawImage(o.image, op)
}
func (object * GameObject) Collide(other *GameObject) bool {
maxX1 := object.x + float32(object.image.Bounds().Dx())
maxY1 := object.y + float32(object.image.Bounds().Dy())
minX1 := object.x
minY1 := object.y
maxX2 := other.x + float32(other.image.Bounds().Dx())
maxY2 := other.y + float32(other.image.Bounds().Dy())
minX2 := other.x
minY2 := other.y
if (minX1 == minX2 && maxX1 == maxX2 && minY1 == minY2 && maxY1 == maxY2) {
return false
}
return ! ( minX2 >= maxX1 || maxX2 <= minX1 || minY2 >= maxY1 || maxY2 <= minY1)
}
func (object * GameObject) ToRect() image.Rectangle {
width := object.image.Bounds().Dx()
height := object.image.Bounds().Dy()
x := int(object.x)
y := int(object.y)
return image.Rect(x, y, x+width, y+height)
}
func OnCollideGeneric(this, other *GameObject) bool {
return true
}
func NewObject(game *Game, x, y float32) *GameObject{
obj := &GameObject{
game: game,
startx: x,
starty: y,
}
obj.friction = friction
obj.resistance = airResistance
obj.x = obj.startx
obj.y = obj.starty
return obj
}
func NewPlayer(game *Game, x, y float32) *GameObject{
player := NewObject(game, x, y)
playerImage, _, err := ebitenutil.NewImageFromFile("assets/character.png")
if err != nil {
log.Fatal(err)
}
player.image = playerImage.SubImage(image.Rect(4, 8, 27, 32)).(*ebiten.Image)
return player
}
func NewExit(game *Game, x, y float32) *GameObject{
exit := NewObject(game, x, y)
exit.image = tilesImage.SubImage(image.Rect(0, 16, 32, 48)).(*ebiten.Image)
return exit
}
func NewBox(game *Game, x, y float32) *GameObject{
box := NewObject(game, x, y)
box.image = tilesImage.SubImage(image.Rect(160, 0, 176, 16)).(*ebiten.Image)
return box
}
func NewSpring(game *Game, x, y float32) *GameObject{
spring := NewObject(game, x, y)
spring.image = tilesImage.SubImage(image.Rect(176, 0, 192, 16)).(*ebiten.Image)
spring.onCollideUp = OnCollideSpring
return spring
}
func NewSpike(game *Game, x, y float32) *GameObject{
spike := NewObject(game, x, y)
spike.offsetY = 3
spike.image = tilesImage.SubImage(image.Rect(192, 3, 208, 16)).(*ebiten.Image)
spike.onCollideUp = OnCollideSpike
return spike
}
func NewLeftSpike(game *Game, x, y float32) *GameObject{
spike := NewObject(game, x, y)
spike.offsetX = 3
spike.image = tilesImage.SubImage(image.Rect(195, 16, 208, 32)).(*ebiten.Image)
spike.onCollideLeft = OnCollideSpike
return spike
}
func OnCollideSpring(this, other *GameObject) bool {
other.vy -= SPRING_FORCE
other.onGround = true
return false
}
func OnCollideSpike(this, other *GameObject) bool {
if other == this.game.player {
other.game.ResetAll()
}
return true
}
|