-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
277 lines (237 loc) · 6.8 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
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
package main
import (
"math/rand/v2"
"slices"
"strconv"
rl "github.com/gen2brain/raylib-go/raylib"
)
type Player struct {
pos rl.Vector2
velocity rl.Vector2
size float32
speed float32
health int
maxHealth int
damage int
direction rl.Vector2
atackSize float32
}
type Enemy struct {
pos rl.Vector2
velocity rl.Vector2
size float32
speed float32
health int
damage int
}
func main() {
var screenWidth int32 = 640
var screenHeight int32 = 450
const title = "Shape Slayer"
isGamePaused := false
rl.InitWindow(screenWidth, screenHeight, title)
rl.SetWindowState(rl.FlagWindowResizable)
defer rl.CloseWindow()
rl.SetTargetFPS(0)
player := Player{
pos: rl.Vector2{},
velocity: rl.Vector2{},
size: 32,
speed: 32,
health: 100,
maxHealth: 100,
damage: 10,
atackSize: 64,
}
var camera = rl.Camera2D{
Offset: rl.Vector2{X: float32(screenWidth) / 2, Y: float32(screenHeight) / 2},
Target: player.pos,
Rotation: 0,
Zoom: 1,
}
defauldEnemy := Enemy{
size: 32,
speed: 24,
health: 20,
damage: 1,
}
var tickRate uint = 32
onTick := newTickLoop(tickRate)
enemies := []Enemy{}
enemiesPerSpawn := 1
var enemySpawnSecondsInterval float32 = 4
enemySpawnTicksInterval := int(enemySpawnSecondsInterval * float32(tickRate))
enemySpawnTimer := 0
for !rl.WindowShouldClose() {
if rl.IsWindowResized() {
screenWidth = int32(rl.GetScreenWidth())
screenHeight = int32(rl.GetScreenHeight())
camera.Offset = rl.Vector2{X: float32(screenWidth) / 2, Y: float32(screenHeight) / 2}
}
// update
if !isGamePaused {
player.direction = rl.Vector2Normalize(rl.Vector2Subtract(rl.GetMousePosition(), rl.Vector2{X: float32(screenWidth) / 2, Y: float32(screenHeight) / 2}))
readPlayerMovment(&player)
movePlayer(&player, rl.GetFrameTime())
camera.Target = player.pos
if wheel := rl.GetMouseWheelMove(); wheel != 0 {
updateCameraZoom(&camera, wheel, 0.05)
}
if rl.IsMouseButtonPressed(0) {
attack(player, &enemies)
}
onTick(func() {
enemySpawnTimer += 1
if enemySpawnTimer >= enemySpawnTicksInterval {
for range enemiesPerSpawn {
spawnEnemy(randPosOnEdge(player.pos, float32(screenWidth)*2, float32(screenHeight)*2, 2), defauldEnemy, &enemies)
}
enemySpawnTimer = 0
enemiesPerSpawn += 1
if enemySpawnSecondsInterval > 1 {
enemySpawnSecondsInterval *= 0.95
enemySpawnTicksInterval = int(enemySpawnSecondsInterval * float32(tickRate))
}
}
checkPlayerEnemiesCollision(&player, &enemies)
if player.health <= 0 {
rl.GlClose()
rl.CloseWindow()
}
enemies = slices.DeleteFunc(enemies, func(e Enemy) bool {
if e.health <= 0 {
return true
}
return false
})
determineEnemiesPos(&enemies, player)
})
moveEnemies(&enemies, rl.GetFrameTime())
}
if rl.IsKeyPressed('P') {
isGamePaused = !isGamePaused
}
// render
rl.BeginDrawing()
rl.ClearBackground(rl.Black)
rl.BeginMode2D(camera)
rl.PushMatrix()
rl.Translatef(0, 16*100, 0)
rl.Rotatef(90, 1, 0, 0)
rl.DrawGrid(100, 100)
rl.PopMatrix()
if rl.IsMouseButtonPressed(0) {
rl.DrawCircle(int32(player.pos.X+(player.direction.X*(player.size+player.atackSize/2))), int32(player.pos.Y+(player.direction.Y*(player.size+player.atackSize/2))), player.atackSize, rl.DarkGray)
}
rl.DrawCircle(int32(player.pos.X), int32(player.pos.Y), player.size, rl.White)
rl.DrawCircle(int32(player.pos.X+player.direction.X*10), int32(player.pos.Y+player.direction.Y*10), player.size/3, rl.Black)
for _, enemy := range enemies {
rl.DrawCircle(int32(enemy.pos.X), int32(enemy.pos.Y), enemy.size, rl.Red)
rl.DrawCircle(int32(enemy.pos.X), int32(enemy.pos.Y), enemy.size-8, rl.Black)
}
rl.EndMode2D()
rl.DrawFPS(0, 0)
rl.DrawRectangle(32, 32, 512, 32, rl.Gray)
rl.DrawRectangle(32, 32, int32(512*player.health/player.maxHealth), 32, rl.Green)
rl.DrawText(strconv.Itoa(player.health)+"/"+strconv.Itoa(player.maxHealth), 36, 36, 24, rl.Black)
rl.EndDrawing()
}
}
func movePlayer(p *Player, delta float32) {
p.pos = rl.Vector2Add(p.pos, rl.Vector2Scale(p.velocity, delta*20))
p.velocity = rl.Vector2{}
}
func readPlayerMovment(p *Player) {
if rl.IsKeyDown('D') {
p.velocity.Y = -1
} else if rl.IsKeyDown('T') {
p.velocity.Y = 1
}
if rl.IsKeyDown('R') {
p.velocity.X = -1
} else if rl.IsKeyDown('S') {
p.velocity.X = 1
}
p.velocity = rl.Vector2Normalize(p.velocity)
p.velocity.X *= p.speed
p.velocity.Y *= p.speed
}
func updateCameraZoom(c *rl.Camera2D, wheel float32, step float32) {
c.Zoom += wheel * step
if c.Zoom < 0.5 {
c.Zoom = 0.5
}
if c.Zoom > 2 {
c.Zoom = 2
}
}
func newTickLoop(tickRate uint) func(func()) {
var tickTime float32 = 1 / float32(tickRate)
var tickTimer float32 = 0
return func(f func()) {
tickTimer += rl.GetFrameTime()
for tickTimer >= tickTime {
f()
tickTimer -= tickTime
}
}
}
func spawnEnemy(pos rl.Vector2, enemy Enemy, enemies *[]Enemy) {
enemy.pos = pos
*enemies = append(*enemies, enemy)
}
func moveEnemies(enemies *[]Enemy, delta float32) {
for i, e := range *enemies {
(*enemies)[i].pos = rl.Vector2Add(e.pos, rl.Vector2Scale((*enemies)[i].velocity, delta*10))
}
}
func checkPlayerEnemiesCollision(p *Player, enemies *[]Enemy) {
for _, e := range *enemies {
if rl.CheckCollisionCircles(p.pos, p.size, e.pos, e.size) {
p.health -= e.damage
}
}
}
func randPosOnEdge(center rl.Vector2, width, height, border float32) rl.Vector2 {
xShift := width/2 + rand.Float32()*border
yShift := height/2 + rand.Float32()*border
isXaxis := rand.Int32N(2) == 0
isNegative := rand.Int32N(2) == 0
if isXaxis {
xShift = (rand.Float32() - 0.5) * (width + border)
if isNegative {
yShift *= -1
}
} else {
yShift = (rand.Float32() - 0.5) * (height + border)
if isNegative {
xShift *= -1
}
}
return rl.Vector2{
X: center.X + xShift,
Y: center.Y + yShift,
}
}
func attack(p Player, enemeis *[]Enemy) {
atackPos := rl.Vector2{
X: p.pos.X + p.direction.X*(p.size+p.atackSize/2),
Y: p.pos.Y + p.direction.Y*(p.size+p.atackSize/2),
}
for i, e := range *enemeis {
if rl.CheckCollisionCircles(e.pos, e.size, atackPos, p.atackSize) {
(*enemeis)[i].health = e.health - p.damage
}
}
}
func determineEnemiesPos(enemies *[]Enemy, p Player) {
for e1Index, e1 := range *enemies {
(*enemies)[e1Index].velocity = rl.Vector2Scale(rl.Vector2Normalize(rl.Vector2Subtract(p.pos, e1.pos)), e1.speed)
for e2Index, e2 := range *enemies {
if e1Index != e2Index && rl.CheckCollisionCircles(e1.pos, e1.size, e2.pos, e2.size) {
(*enemies)[e1Index].pos = rl.Vector2Add(e1.pos, rl.Vector2Scale(rl.Vector2Normalize(rl.Vector2Subtract(e1.pos, e2.pos)), 1))
(*enemies)[e2Index].pos = rl.Vector2Add(e2.pos, rl.Vector2Scale(rl.Vector2Normalize(rl.Vector2Subtract(e2.pos, e1.pos)), 1))
}
}
}
}