-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
517 lines (459 loc) · 12.1 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
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
/*
This file is part of Murinus.
Murinus is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Murinus is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Murinus. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"fmt"
"math/rand"
"runtime"
"strconv"
"time"
"github.com/veandco/go-sdl2/sdl"
)
const (
Arcade bool = false
sizeMult int32 = 4
sizeDiv int32 = 5
timeExitHasToBeHeldToQuit int = 60 * 5
timeExitHasToBeHeldToExit int = 90
)
const (
screenWidthD int32 = (1280 * sizeMult) / sizeDiv
screenHeightD int32 = (800 * sizeMult) / sizeDiv
blockSizeD int32 = (48 * sizeMult) / sizeDiv
blockSizeBigBoardD int32 = (24 * sizeMult) / sizeDiv
gSize int32 = 12
)
var screenWidth, screenHeight, blockSize, blockSizeBigBoard int32
var quit, lostLife bool
var (
window *sdl.Window
renderer *sdl.Renderer
input *Input
menus []*Menu
stage *Stage
highscores Highscores
defaultName string
)
func main() {
Init()
defer CleanUp()
for !quit {
difficulty = -1
menuChoice := -1
menuLoop:
for difficulty == -1 && !quit {
menuChoice, _, _ = menus[0].Run(renderer, input)
switch menuChoice {
case -1:
if !quit {
quit = Arcade
}
break menuLoop
case 0:
fallthrough
case 1:
StartGameSession(menuChoice)
case 2:
StartTrainingSession()
case 3:
highscores.Display(-1, false, renderer, input)
case 4:
ShowStats()
case 5:
DoSettings(menus[3], renderer, input)
case 6:
ShowCredits()
case 7:
quit = true
default:
panic("Unknown menu option")
}
}
}
fmt.Println("Quit")
}
func Init() {
screenWidth, screenHeight, blockSize, blockSizeBigBoard =
screenWidthD, screenHeightD, blockSizeD, blockSizeBigBoardD
newScreenWidth, newScreenHeight = screenWidth, screenHeight
runtime.LockOSThread()
err := sdl.Init(sdl.INIT_EVERYTHING)
PanicOnError(err)
fmt.Println("Init SDL")
window, err = sdl.CreateWindow("Murinus", sdl.WINDOWPOS_UNDEFINED,
sdl.WINDOWPOS_UNDEFINED, int(screenWidth), int(screenHeight),
sdl.WINDOW_SHOWN|sdl.WINDOW_RESIZABLE|sdl.RENDERER_PRESENTVSYNC)
PanicOnError(err)
fmt.Println("Created window")
renderer, err = sdl.CreateRenderer(window, -1,
sdl.RENDERER_ACCELERATED)
PanicOnError(err)
renderer.Clear()
fmt.Println("Created renderer")
InitText(renderer)
fmt.Println("Initiated text")
InitNumbers(renderer)
fmt.Println("Initiated numbers")
input = GetInput()
fmt.Println("Got inputs")
ReadOptions("options.xml", input)
fmt.Println("Created options")
stage = LoadTextures(renderer, input)
fmt.Println("Loaded stage-basis")
menus = GetMenus(renderer)
fmt.Println("Created menus")
highscores = Read("singleplayer.hs", "multiplayer.hs")
fmt.Println("Loaded Highscores")
stats.Load("stats.info")
fmt.Println("Loaded Stats")
defaultName = "\\\\\\\\\\"
rand.Seed(time.Now().Unix())
if Arcade {
sdl.ShowCursor(sdl.DISABLE)
}
}
func CleanUp() {
highscores.Write("singleplayer.hs", "multiplayer.hs")
stats.Save("stats.info")
if !Arcade {
SaveOptions("options.xml", input)
}
numbers.Free()
for i := 0; i < len(menus); i++ {
menus[i].Free()
}
stage.Free()
renderer.Destroy()
window.Destroy()
}
func StartGameSession(menuChoice int) {
difficulty, _, _ = menus[1].Run(renderer, input)
stage.ID = -1
for !quit && difficulty != -1 {
if menuChoice != 0 {
stats.Multiplayer[difficulty]++
} else {
stats.Singleplayer[difficulty]++
}
levelsCleared := 0
score := -ScoreMult(500)
won := RunGame(menuChoice, &levelsCleared, &score)
fmt.Printf("Game Over. Final score %d\n", score)
stage.lostOnce = true
input.exit.timeHeld = 0
if !GameOverMenu(levelsCleared, score, menuChoice != 0, won) {
break
}
}
}
func StartTrainingSession() {
stats.TimesTrained++
difficulty = -1
for play, ups, downs := menus[4].Run(renderer, input); !quit &&
play != -1; play, _, _ = menus[4].Run(renderer, input) {
ID := stage.levels[2][int(menus[4].NVal(0))][0]
difficulty := int(menus[4].NVal(1))
players := menus[4].NVal(3) - 1
engine := stage.LoadSingleLevel(ID, difficulty,
ups > 8, downs > 8, true, 0, players)
for i := menus[4].NVal(2); i > 0; i-- {
PlayStage(engine, window, renderer, i)
if quit || input.exit.active || !lostLife {
break
}
engine = stage.LoadSingleLevel(ID, difficulty,
ups > 8, downs > 8, false, engine.Score, players)
}
}
}
func ShowCredits() {
strings := []string{"Made by ITR",
"Source available on github.com/ITR13/murinus", " ",
"Other contributers:", "byllgrim"}
textures := make([]*sdl.Texture, len(strings))
src := make([]*sdl.Rect, len(strings))
dst := make([]*sdl.Rect, len(strings))
h := int32(0)
for i := 0; i < len(textures); i++ {
col := rand.Int()%26 + 1
red := uint8((col % 3) * 255 / 2)
col /= 3
green := uint8((col % 3) * 255 / 2)
col /= 3
blue := uint8((col % 3) * 255 / 2)
textures[i], src[i], dst[i] = GetText(strings[i],
sdl.Color{red, green, blue, 255},
newScreenWidth/2, newScreenHeight/2, renderer)
defer textures[i].Destroy()
h += dst[i].H * sizeMult / sizeDiv
dst[i].X -= dst[i].W * sizeMult / sizeDiv
dst[i].W *= 2 * sizeMult / sizeDiv
dst[i].H *= 2 * sizeMult / sizeDiv
}
input.Clear()
for i := 0; i < len(textures); i++ {
dst[i].Y = newScreenHeight/2 - h
h -= dst[i].H
}
renderer.SetRenderTarget(nil)
renderer.SetDrawColor(0, 0, 0, 255)
renderer.Clear()
for !input.mono.a.Down() && !input.mono.b.Down() && !quit {
renderer.SetRenderTarget(nil)
renderer.SetDrawColor(0, 0, 0, 255)
renderer.Clear()
for i := 0; i < len(textures); i++ {
PanicOnError(renderer.Copy(textures[i], src[i], dst[i]))
}
renderer.Present()
input.Poll()
}
}
func RunGame(menuChoice int, levelsCleared *int, score *int64) bool {
lostLife = false
lives := 3
wonInARow := -2
extraLives := 0
extraLivesCounter := int64(25000)
for !quit && (lives != 1 || !lostLife) {
var engine *Engine
if lostLife {
wonInARow = -1
lostLife = false
lives--
if lives < extraLives {
extraLives = lives
}
if lives == 0 {
panic("Should not reach this statement")
}
engine = stage.Load(stage.ID, false, *score, menuChoice)
window.SetTitle("Score: " + strconv.Itoa(int(*score)) +
" Lives: " + strconv.Itoa(lives))
} else {
*levelsCleared++
wonInARow++
if wonInARow == 3 {
if lives-extraLives < 4 {
wonInARow = 0
lives++
}
}
fmt.Printf("Won in a row counter: %d\n", wonInARow)
engine = stage.Load(stage.ID+1, true,
*score+ScoreMult(500), menuChoice)
}
fmt.Printf("Lives: %d\n", lives)
if engine == nil {
fmt.Println("Engine nil, game was won")
return true
}
PlayStage(engine, window, renderer, int32(lives))
*score = engine.Score
if engine.Input.exit.active {
fmt.Println("Game was quit with exit key")
return false
}
for *score > extraLivesCounter &&
extraLivesCounter*2 > extraLivesCounter {
extraLivesCounter *= 2
extraLives++
lives++
stats.ExtraLives++
}
fmt.Printf("Score: %d\n", *score)
}
return false
}
func PlayStage(engine *Engine, window *sdl.Window, renderer *sdl.Renderer,
lives int32) {
p1C, p2C := engine.GetPlayerSpriteID()
quit = false
lostLife = false
score := int32(0)
engine.Stage.scores.score, engine.Stage.scores.lives = engine.Score, lives
for i := 0; i < 30 && !quit; i++ {
engine.Stage.Render(p1C, p2C, renderer, false)
engine.Input.Poll()
if engine.Input.exit.active {
fmt.Println("Round was quit with exit key")
return
}
}
for noKeysTouched >= 25 && !quit {
engine.Stage.Render(p1C, p2C, renderer, false)
engine.Input.Poll()
}
fmt.Println("Finished starting animation")
engine.Stage.tiles.renderedOnce = false
for !quit {
engine.Input.Poll()
if engine.Input.exit.active {
fmt.Println("Round was quit with exit key")
return
}
engine.Advance()
window.SetTitle("Murinus (score: " +
strconv.Itoa(int(score)) +
", left " + strconv.Itoa(engine.Stage.pointsLeft) + ")")
engine.Stage.scores.score = engine.Score
engine.Stage.Render(p1C, p2C, renderer, true)
if engine.Stage.pointsLeft <= 0 || lostLife {
break
}
}
fmt.Println("Exited play loop")
for i := 0; i < len(engine.snakes); i++ {
snake := engine.snakes[i]
snake.head.display = true
for j := 0; j < len(snake.body); j++ {
snake.body[j].display = true
}
snake.tail.display = true
}
if lostLife {
for i := 0; i < 90 && !quit; i++ {
engine.Stage.tiles.renderedOnce = false
engine.Stage.scores.lives = lives - int32(i/15%2)
p1C, p2C := engine.GetPlayerSpriteID()
if engine.p1 != nil {
engine.p1.entity.display = (i / 15 % 2) == 0
}
if engine.p2 != nil {
engine.p2.entity.display = (i / 15 % 2) == 0
}
engine.Stage.Render(p1C, p2C, renderer, false)
engine.Input.Poll()
if engine.Input.exit.active {
fmt.Println("Round was quit with exit key")
return
}
}
} else {
for i := 0; i < 30 && !quit; i++ {
engine.Stage.Render(p1C, p2C, renderer, false)
engine.Input.Poll()
}
}
fmt.Println("Finished exit animation")
}
func DoSettings(menu *Menu, renderer *sdl.Renderer, input *Input) {
for v, _, _ := menu.Run(renderer, input); v != -1 &&
!quit; v, _, _ = menu.Run(renderer, input) {
ReadOptions("", input)
menu.menuItems[0].SetNumber(int32(options.CharacterP1), renderer)
menu.menuItems[1].SetNumber(int32(options.CharacterP2), renderer)
menu.menuItems[2].SetNumber(int32(options.UseTap), renderer)
menu.menuItems[3].SetNumber(int32(options.EdgeSlip), renderer)
menu.menuItems[4].SetNumber(int32(options.BetterSlip), renderer)
menu.menuItems[5].SetNumber(int32(options.ShowDivert), renderer)
}
if quit {
return
}
options.CharacterP1 = uint8(menu.NVal(0))
options.CharacterP2 = uint8(menu.NVal(1))
options.UseTap = uint8(menu.NVal(2))
options.useTap = options.UseTap != 0
options.EdgeSlip = int(menu.NVal(3))
options.BetterSlip = menu.NVal(4)
options.ShowDivert = uint8(menu.NVal(5))
options.showDivert = options.ShowDivert != 0
redrawTextures = true
}
func GameOverMenu(levelsCleared int, score int64, multiplayer,
won bool) (resume bool) {
menuChoice := -1
var scoreData *ScoreData
if !won {
menus[2].selectedElement = 0
for !quit && menuChoice < 2 {
menuChoice, _, _ = menus[2].Run(renderer, input)
switch menuChoice {
case 0: // Set name
name := GetName(defaultName, renderer, input)
if name != "" {
defaultName = name
if scoreData == nil {
scoreData = &ScoreData{score, name,
levelsCleared, difficulty,
time.Now()}
highscores.Add(scoreData,
multiplayer, true)
} else {
scoreData.Name = name
}
}
case 1: // Highscores
highscores.Display(difficulty, multiplayer,
renderer, input)
case -1:
//menuChoice = 4
}
}
} else {
menus[5].selectedElement = 0
for !quit && menuChoice < 2 {
menuChoice, _, _ = menus[5].Run(renderer, input)
switch menuChoice {
case 0: // Set name
name := GetName(defaultName, renderer, input)
if name != "" {
defaultName = name
if scoreData == nil {
scoreData = &ScoreData{score, name,
levelsCleared, difficulty,
time.Now()}
highscores.Add(scoreData,
menuChoice != 0, true)
} else {
scoreData.Name = name
}
}
case 1: // Highscores
highscores.Display(difficulty, menuChoice != 0,
renderer, input)
case -1:
//menuChoice = 3
}
}
menuChoice++
}
if quit {
return false
}
resume = true
switch menuChoice {
case 2: // Continue
stage.ID--
case 3: // Restart
stage.ID = -1
case 4: // Exit to menu
resume = false
default:
panic("Unknown menu option")
}
return
}
func PanicOnError(err error) {
if err != nil {
panic(err)
}
}
func LogOnError(err error) bool {
if err != nil {
fmt.Println(err)
}
return err != nil
}