/
main.go
195 lines (167 loc) · 4.01 KB
/
main.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
// Package main 👍
package main
import (
"errors"
"image/color"
"log"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
collider "github.com/melonfunction/ebiten-collider"
)
// vars
var (
WindowWidth = 640 * 2
WindowHeight = 480 * 2
player *Player
wall *collider.RectangleShape
wall2 *collider.RectangleShape
obs *collider.CircleShape
obs2 *collider.CircleShape
hash *collider.SpatialHash
cursor *collider.PointShape
ErrNormalExit = errors.New("Normal exit")
)
// Player is the moveable shape
type Player struct {
Bounds *collider.CircleShape
// Bounds *collider.RectangleShape
Speed float64
}
// Game implements ebiten.Game interface.
type Game struct{}
// Update proceeds the game state.
// Update is called every tick (1/60 [s] by default).
func (g *Game) Update() error {
if ebiten.IsKeyPressed(ebiten.KeyEscape) {
return ErrNormalExit
}
if ebiten.IsKeyPressed(ebiten.KeyLeft) || ebiten.IsKeyPressed(ebiten.KeyH) {
player.Bounds.Move(-player.Speed, 0)
}
if ebiten.IsKeyPressed(ebiten.KeyRight) || ebiten.IsKeyPressed(ebiten.KeyN) {
player.Bounds.Move(player.Speed, 0)
}
if ebiten.IsKeyPressed(ebiten.KeyUp) || ebiten.IsKeyPressed(ebiten.KeyC) {
player.Bounds.Move(0, -player.Speed)
}
if ebiten.IsKeyPressed(ebiten.KeyDown) || ebiten.IsKeyPressed(ebiten.KeyT) {
player.Bounds.Move(0, player.Speed)
}
cx, cy := ebiten.CursorPosition()
cursor.MoveTo(float64(cx), float64(cy))
collisions := hash.CheckCollisions(player.Bounds)
for _, collision := range collisions {
sep := collision.SeparatingVector
player.Bounds.Move(sep.X, sep.Y)
log.Println(collision.Other.GetParent())
// player.Bounds.Move(sep.X/2, sep.Y/2)
// collision.Other.Move(-sep.X/2, -sep.Y/2)
}
return nil
}
// Draw draws the game screen.
// Draw is called every frame (typically 1/60[s] for 60Hz display).
func (g *Game) Draw(screen *ebiten.Image) {
hash.Draw(screen)
red := color.RGBA{255, 0, 0, 128}
green := color.RGBA{0, 255, 0, 128}
_ = green
ebitenutil.DrawCircle(
screen,
player.Bounds.Pos.X,
player.Bounds.Pos.Y,
player.Bounds.Radius,
red)
// ebitenutil.DrawRect(
// screen,
// player.Bounds.Pos.X-player.Bounds.Width/2,
// player.Bounds.Pos.Y-player.Bounds.Height/2,
// player.Bounds.Width,
// player.Bounds.Height,
// red)
ebitenutil.DrawCircle(
screen,
obs.Pos.X,
obs.Pos.Y,
obs.Radius,
red)
ebitenutil.DrawCircle(
screen,
obs2.Pos.X,
obs2.Pos.Y,
obs2.Radius,
red)
ebitenutil.DrawRect(
screen,
wall.Pos.X-wall.Width/2,
wall.Pos.Y-wall.Height/2,
wall.Width,
wall.Height,
red)
ebitenutil.DrawRect(
screen,
wall2.Pos.X-wall2.Width/2,
wall2.Pos.Y-wall2.Height/2,
wall2.Width,
wall2.Height,
red)
ebitenutil.DrawCircle(
screen,
cursor.Pos.X,
cursor.Pos.Y,
5,
red)
}
// Layout sets window size
func (g *Game) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
if WindowWidth != outsideWidth || WindowHeight != outsideHeight {
log.Println("resize", outsideWidth, outsideHeight)
WindowWidth = outsideWidth
WindowHeight = outsideHeight
}
return outsideWidth, outsideHeight
}
func main() {
game := &Game{}
ebiten.SetWindowSize(WindowWidth, WindowHeight)
ebiten.SetWindowTitle("Collisions example")
ebiten.SetWindowResizable(true)
x := float64(WindowWidth)/2 - 64/2
y := float64(WindowHeight)/2 - 64/2
hash = collider.NewSpatialHash(128)
player = &Player{
Bounds: hash.NewCircleShape(x, y, 32),
// Bounds: hash.NewRectangleShape(x, y, 64, 64),
Speed: 1,
}
player.Bounds.SetParent("I'm the player")
wall = hash.NewRectangleShape(
x,
y-16,
128,
128,
)
wall.SetParent("Wall 1")
wall2 = hash.NewRectangleShape(
x+128,
y-16,
128,
128*2,
)
wall2.SetParent("Wall 2")
obs = hash.NewCircleShape(
x+96,
y+256+64,
32)
obs.SetParent("Circle 1")
obs2 = hash.NewCircleShape(
x+128,
y+256,
64)
obs2.SetParent("Circle 2")
cursor = hash.NewPointShape(0, 0)
cursor.SetParent("Cursor")
if err := ebiten.RunGame(game); err != nil {
log.Fatal(err)
}
}