-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
1833 lines (1505 loc) · 50.7 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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"encoding/json"
"image"
"image/color"
_ "image/png"
"io"
"io/ioutil"
"log"
"math"
"math/rand"
"os"
"strings"
"sync"
"time"
alpacolor "github.com/aragajaga/alpa/util/color"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/audio"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
"github.com/hajimehoshi/ebiten/v2/inpututil"
)
type TextGrid struct {
rows [][]rune
}
const (
screenWidth = 640
screenHeight = 480
)
const (
tileSize = 16
tileXNum = 8
)
const (
GUIFRAMETILE_NW uint8 = 0
GUIFRAMETILE_TOP uint8 = 1
GUIFRAMETILE_NE uint8 = 2
GUIFRAMETILE_LEFT uint8 = 3
GUIFRAMETILE_RIGHT uint8 = 4
GUIFRAMETILE_SW uint8 = 5
GUIFRAMETILE_BOTTOM uint8 = 6
GUIFRAMETILE_SE uint8 = 7
)
const (
GUIFRAMETILE_SIZE int = 5
)
const (
FONTTILEMAP_ROW_LATIN = 0
FONTTILEMAP_ROW_CYRILLIC = 6
)
const (
UNICODE_LATIN_FIRST = '\u0020'
UNICODE_LATIN_LAST = '\u007F'
UNICODE_CYRILLIC_FIRST = '\u0400'
UNICODE_CYRILLIC_LAST = '\u045F'
)
const (
GLYPH_HORIZONTAL_SPACING = 1
GLYPH_VERTICAL_SPACING = 2
)
var (
tilesImage *ebiten.Image
charSprite *ebiten.Image
morgenSprite *ebiten.Image
flanSprite *ebiten.Image
monobearSprite *ebiten.Image
explosionSprite *ebiten.Image
guiFrameHerb *ebiten.Image
guiFrameTest *ebiten.Image
guiButton *ebiten.Image
tileCursor *ebiten.Image
skillMonobearExplosion *ebiten.Image
seeYaTileSet *ebiten.Image
michaelSprite *ebiten.Image
worldBorderImage *ebiten.Image
tickCounter int
)
type KeyBind uint8
type KeyBindMap map[KeyBind]ebiten.Key
const (
kbPlayerMoveRight KeyBind = 1
kbPlayerMoveLeft KeyBind = 2
kbPlayerMoveUp KeyBind = 3
kbPlayerMoveDown KeyBind = 4
kbToggleEditMode KeyBind = 5
kbToggleEntityFocusRotation KeyBind = 6
kbEditorNextLayer KeyBind = 7
kbEditorPrevLayer KeyBind = 8
kbEditorNextBrush KeyBind = 9
kbEditorPrevBrush KeyBind = 10
kbEditorPlace KeyBind = 11
kbEditorDelete KeyBind = 12
kbEditorSwitchMode KeyBind = 13
kbShowDebugInfo KeyBind = 14
kbWorldZoomIn KeyBind = 15
kbWorldZoomOut KeyBind = 16
)
var keyBinds KeyBindMap
var r *rand.Rand
type LookDirection int64
const (
LooksRight LookDirection = 0
LooksLeft LookDirection = 1
LooksUp LookDirection = 2
LooksDown LookDirection = 3
)
type View struct {
guiScale float64
}
type Tile uint8
type TileLayer []Tile
type Level struct {
tileLayers []TileLayer
width int
height int
fileName string
}
func (lv *Level) ReplaceAll(from, to Tile) {
for i, layer := range lv.tileLayers {
for j, tile := range layer {
if tile == from {
lv.tileLayers[i][j] = to
}
}
}
}
func I18n(stringID, fallbackText string) string {
translationString, has := langData[stringID]
if !has {
return fallbackText
}
return translationString
}
type IResource interface{}
type Rect struct {
X1 int
Y1 int
X2 int
Y2 int
}
type XPRunWindow struct {
XPWindow
}
func NewXPRunWindow(xps *WinXPScreen) *XPRunWindow {
s := new(XPRunWindow)
s.hWnd = xps.hwndCounter
xps.hwndCounter++
s.xpScreen = xps
s.game = xps.game
return s
}
type XPStartButtonWidget struct {
XPWindow
}
func (wnd *XPStartButtonWidget) Draw(screen *ebiten.Image) {
wnd.game.DrawStatedNineGrid(screen, wnd.xpScreen.startButtonImage, 0, 3, 1.0, NineGridInfo{Left: 6, Top: 13, Right: 52, Bottom: 14}, wnd.posX, wnd.posY, wnd.width, wnd.height)
}
func NewXPStartButtonWidget(xps *WinXPScreen) *XPStartButtonWidget {
s := new(XPStartButtonWidget)
InitXPWindow(&s.XPWindow, xps, "_CiceronStart", 0, 0, 32, 32)
return s
}
type ScreenManager struct {
currentScreen IScreen
}
func (bs *ScreenManager) SetScreen(screen IScreen) {
bs.currentScreen = screen
}
func CreateTextGrid() *TextGrid {
tg := new(TextGrid)
return tg
}
func (tg *TextGrid) Row(i int) []rune {
return tg.rows[i]
}
func (tg *TextGrid) SetRow(i int, row []rune) {
tg.rows[i] = row
}
type TileCursor struct {
x int
y int
}
func (c *TileCursor) GetWorldPos() Vec2f {
return Vec2f{float64(c.x), float64(c.y)}.Scale(tileSize)
}
type TileDesc struct {
JSONName string
Walkable bool
}
type TileDescStorage map[Tile]TileDesc
func (tds *TileDescStorage) RegisterTile(tileID Tile, jsonName string, walkable bool) {
// loadingLog := lazyAppend(loadingLog, fmt.Sprintf("Registering tile \"%s\"", jsonName))
(*tds)[tileID] = TileDesc{
JSONName: jsonName,
Walkable: walkable,
}
}
var tileDescStorage TileDescStorage
var tileNameMap map[Tile]string
var brandingImage *ebiten.Image
func init() {
r = rand.New(rand.NewSource(time.Now().UnixNano()))
tileNameMap = make(map[Tile]string)
tileDescStorage = make(TileDescStorage)
tileDescStorage.RegisterTile(tileIDEmpty, "tile_id_empty", true)
tileDescStorage.RegisterTile(tileIDGrass, "tile_id_grass", true)
tileDescStorage.RegisterTile(tileIDSand, "tile_id_sand", true)
tileDescStorage.RegisterTile(tileIDWater, "tile_id_water", true)
tileDescStorage.RegisterTile(tileIDVoid, "tile_id_void", true)
tileDescStorage.RegisterTile(tileIDHouseWall, "tile_id_house_wall", false)
tileDescStorage.RegisterTile(tileIDCarvedStone, "tile_id_carved_stone", false)
tileDescStorage.RegisterTile(tileIDBricks, "tile_id_bricks", false)
tileDescStorage.RegisterTile(tileIDRock, "tile_id_rock", false)
tileDescStorage.RegisterTile(tileIDDoor, "tile_id_door", false)
tileDescStorage.RegisterTile(tileIDWallCornerL, "tile_id_wall_corner_l", true)
tileDescStorage.RegisterTile(tileIDWallCornerR, "tile_id_wall_corner_r", true)
tileDescStorage.RegisterTile(tileIDWindow, "tile_id_window", true)
tileDescStorage.RegisterTile(tileIDBarrier, "tile_id_barrier", false)
tileDescStorage.RegisterTile(tileIDSwitch, "tile_id_switch", true)
tileDescStorage.RegisterTile(tileIDSwitchActive, "tile_id_switch_active", true)
tileDescStorage.RegisterTile(tileIDRoofNW, "tile_id_roof_nw", false)
tileDescStorage.RegisterTile(tileIDRoofTop, "tile_id_roof_top", false)
tileDescStorage.RegisterTile(tileIDRoofNE, "tile_id_roof_ne", false)
tileDescStorage.RegisterTile(tileIDRoofLeft, "tile_id_roof_left", false)
tileDescStorage.RegisterTile(tileIDRoof, "tile_id_roof", false)
tileDescStorage.RegisterTile(tileIDRoofRight, "tile_id_roof_right", false)
tileDescStorage.RegisterTile(tileIDRoofSW, "tile_id_roof_sw", false)
tileDescStorage.RegisterTile(tileIDRoofBottom, "tile_id_roof_bottom", false)
tileDescStorage.RegisterTile(tileIDRoofSE, "tile_id_roof_se", false)
tileDescStorage.RegisterTile(tileIDThorns, "tile_id_thorns", true)
tileDescStorage.RegisterTile(tileIDThornsActive, "tile_id_thorns_active", true)
tileDescStorage.RegisterTile(tileIDButton, "tile_id_button", true)
tileDescStorage.RegisterTile(tileIDButtonPushed, "tile_id_button_pushed", true)
tileDescStorage.RegisterTile(tileIDBlenderNW, "tile_id_blender_nw", true)
tileDescStorage.RegisterTile(tileIDBlenderTop, "tile_id_blender_top", true)
tileDescStorage.RegisterTile(tileIDBlenderNE, "tile_id_blender_ne", true)
tileDescStorage.RegisterTile(tileIDBlenderLeft, "tile_id_blender_left", true)
tileDescStorage.RegisterTile(tileIDBlenderRight, "tile_id_blender_right", true)
tileDescStorage.RegisterTile(tileIDBlenderSW, "tile_id_blender_sw", true)
tileDescStorage.RegisterTile(tileIDBlenderBottom, "tile_id_blender_bottom", true)
tileDescStorage.RegisterTile(tileIDBlenderSE, "tile_id_blender_se", true)
tileDescStorage.RegisterTile(tileIDTableBottom, "tile_id_table_bottom", false)
tileDescStorage.RegisterTile(tileIDTableTop, "tile_id_table_top", false)
tileDescStorage.RegisterTile(tileIDBedTop, "tile_id_bed_top", false)
tileDescStorage.RegisterTile(tileIDBedBottom, "tile_id_bed_bottom", false)
tileDescStorage.RegisterTile(tileIDLaptop, "tile_id_laptop", true)
tileDescStorage.RegisterTile(tileIDHouseFloor, "tile_id_house_floor", true)
tileDescStorage.RegisterTile(tileIDHouseWall, "tile_id_house_inner_wall", false)
tileDescStorage.RegisterTile(tileIDMine, "tile_id_mine", true)
tileDescStorage.RegisterTile(tileIDMineFlag, "tile_id_mine_flag", false)
tileDescStorage.RegisterTile(tileIDBloodDigit1, "tile_id_blood_digit_1", true)
tileDescStorage.RegisterTile(tileIDBloodDigit2, "tile_id_blood_digit_2", true)
tileDescStorage.RegisterTile(tileIDBloodDigit3, "tile_id_blood_digit_3", true)
tileDescStorage.RegisterTile(tileIDBloodDigit4, "tile_id_blood_digit_4", true)
tileDescStorage.RegisterTile(tileIDBloodDigit5, "tile_id_blood_digit_5", true)
tileDescStorage.RegisterTile(tileIDBloodDigit6, "tile_id_blood_digit_6", true)
tileDescStorage.RegisterTile(tileIDBloodDigit7, "tile_id_blood_digit_7", true)
tileDescStorage.RegisterTile(tileIDBloodDigit8, "tile_id_blood_digit_8", true)
tileDescStorage.RegisterTile(tileIDBloodDigit9, "tile_id_blood_digit_9", true)
tileDescStorage.RegisterTile(tileIDBloodDigit0, "tile_id_blood_digit_0", true)
tileDescStorage.RegisterTile(tileIDSokobanBox, "tile_id_sokoban_box", false)
tileDescStorage.RegisterTile(tileIDPoppingBarrierPushed, "tile_id_popping_barrier_pushed", true)
tileDescStorage.RegisterTile(tileIDPoppingBarrier, "tile_id_popping_barrier", true)
tileDescStorage.RegisterTile(tileIDPoppingBarrierActive, "tile_id_popping_barrier_active", false)
tileDescStorage.RegisterTile(tileIDWeigth, "tile_id_weight", false)
tileDescStorage.RegisterTile(tileIDArrowRight, "tile_id_arrow_right", true)
tileDescStorage.RegisterTile(tileIDBerryBush, "tile_id_berry_bush", false)
tileDescStorage.RegisterTile(tileIDBall, "tile_id_ball", false)
tileDescStorage.RegisterTile(tileIDHoneyBall, "tile_id_honey_ball", true)
tileDescStorage.RegisterTile(tileIDElevator, "tile_id_elevator", true)
tileDescStorage.RegisterTile(tileIDTarget, "tile_id_target", true)
tileDescStorage.RegisterTile(tileIDFenceSinleTop, "tile_id_fence_single_top", false)
tileDescStorage.RegisterTile(tileIDPavedRoad, "tile_id_paved_road", true)
tileDescStorage.RegisterTile(tileIDPit, "tile_id_pit", true)
tileDescStorage.RegisterTile(tileIDTree1, "tile_id_tree_1", true)
tileDescStorage.RegisterTile(tileIDBush, "tile_id_bush", false)
tileDescStorage.RegisterTile(tileIDWell, "tile_id_well", false)
tileDescStorage.RegisterTile(tileIDFenceNW, "tile_id_fence_nw", false)
tileDescStorage.RegisterTile(tileIDPavedRoad2, "tile_id_paved_road_2", true)
tileDescStorage.RegisterTile(tileIDCastleTower, "tile_id_castle_tower", false)
tileDescStorage.RegisterTile(tileIDTree2, "tile_id_tree_2", true)
tileDescStorage.RegisterTile(tileIDTree3, "tile_id_tree_3", true)
tileDescStorage.RegisterTile(tileIDFlower1, "tile_id_flower_1", true)
tileDescStorage.RegisterTile(tileIDSlab, "tile_id_slab", false)
tileDescStorage.RegisterTile(tileIDArrowLeft, "tile_id_arrow_left", true)
tileDescStorage.RegisterTile(tileIDAid, "tile_id_aid", true)
tileDescStorage.RegisterTile(tileIDBlueRose, "tile_id_blue_rose", true)
tileDescStorage.RegisterTile(tileIDColorDigit1, "tile_id_color_digit_1", true)
tileDescStorage.RegisterTile(tileIDColorDigit2, "tile_id_color_digit_2", true)
tileDescStorage.RegisterTile(tileIDColorDigit3, "tile_id_color_digit_3", true)
tileDescStorage.RegisterTile(tileIDPoop, "tile_id_poop", true)
tileNameMap = map[Tile]string{
tileIDEmpty: "Empty",
tileIDGrass: "Grass",
tileIDSand: "Sand",
tileIDWater: "Water",
tileIDVoid: "Void",
tileIDHouseWall: "House Wall",
tileIDCarvedStone: "Carved Stone",
tileIDBricks: "Bricks",
tileIDRock: "Rock",
tileIDDoor: "Door",
tileIDWallCornerL: "Corner Bricks (Left)",
tileIDWallCornerR: "Corner Bricks (Right)",
tileIDWindow: "Window",
tileIDBarrier: "Barrier",
tileIDSwitch: "Switch",
tileIDSwitchActive: "Pushed Switch",
tileIDRoofNW: "Roof (Top-Left)",
tileIDRoofTop: "Roof (Top)",
tileIDRoofNE: "Roof (Top-Right)",
tileIDRoofLeft: "Roof (Left)",
tileIDRoof: "Roof (Center)",
tileIDRoofRight: "Roof (Right)",
tileIDRoofSW: "Roof (Bottom-Left)",
tileIDRoofBottom: "Roof (Bottom)",
tileIDRoofSE: "Roof (Bottom-Right)",
tileIDThorns: "Thorns",
tileIDThornsActive: "Activated Thorns",
tileIDButton: "Button",
tileIDButtonPushed: "Pushed Button",
tileIDBlenderNW: "Blender (Top-Left)",
tileIDBlenderTop: "Blender (Top)",
tileIDBlenderNE: "Blender (Top-Right)",
tileIDBlenderLeft: "Blender (Left)",
tileIDBlenderRight: "Blender (Right)",
tileIDBlenderSW: "Blender (Bottom-Left)",
tileIDBlenderBottom: "Blender (Bottom)",
tileIDBlenderSE: "Blender (Bottom-Right)",
tileIDTableBottom: "Table (Bottom)",
tileIDTableTop: "Table (Top)",
tileIDBedTop: "Bed (Top)",
tileIDBedBottom: "Bed (Bottom)",
tileIDLaptop: "Laptop",
tileIDHouseFloor: "Floor",
tileIDHouseInnerWall: "House Wall (Inner)",
tileIDMine: "Mine",
tileIDMineFlag: "Mine Flag",
tileIDBloodDigit1: "Blood Digit 1",
tileIDBloodDigit2: "Blood Digit 2",
tileIDBloodDigit3: "Blood Digit 3",
tileIDBloodDigit4: "Blood Digit 4",
tileIDBloodDigit5: "Blood Ditit 5",
tileIDBloodDigit6: "Blood Digit 6",
tileIDBloodDigit7: "Blood Digit 7",
tileIDBloodDigit8: "Blood Digit 8",
tileIDBloodDigit9: "Blood Digit 9",
tileIDBloodDigit0: "Blood Digit 0",
tileIDSokobanBox: "Sokoban Box",
tileIDPoppingBarrierPushed: "Popping Barrier (Pushed)",
tileIDPoppingBarrier: "Popping Barrier",
tileIDPoppingBarrierActive: "Popping Barrier (Active)",
tileIDWeigth: "Weight",
tileIDArrowRight: "Arrow Right",
tileIDBerryBush: "Berried Bush",
tileIDBall: "Ball",
tileIDHoneyBall: "Honey Ball",
tileIDElevator: "Elevator",
tileIDTarget: "Target",
tileIDFenceSinleTop: "Fence Single (Top)",
tileIDPavedRoad: "Paved Road",
tileIDPit: "Pit",
tileIDTree1: "Tree 1",
tileIDBush: "Bush",
tileIDWell: "Well",
tileIDFenceNW: "Fence (NW)",
tileIDPavedRoad2: "Paved Road 2",
tileIDCastleTower: "Castle Tower",
tileIDTree2: "Tree 2",
tileIDTree3: "Tree 3",
tileIDFlower1: "Red Flower",
tileIDSlab: "Slab",
tileIDArrowLeft: "Arrow Left",
tileIDAid: "Aid",
tileIDBlueRose: "Blue Rose",
tileIDColorDigit1: "Color Digit 1",
tileIDColorDigit2: "Color Digit 2",
tileIDColorDigit3: "Color Digit 3",
tileIDPoop: "Poop",
}
}
func (g *Game) ToggleDebugInfoShow() {
g.showDebugInfo = !g.showDebugInfo
}
func (g *Game) Update() error {
if g.currentScreen != nil {
g.currentScreen.Update()
}
g.appTicker++
return nil
}
func GetTileSprite(tileSet *ebiten.Image, tileSetWidth int, tileSize int, tile Tile) *ebiten.Image {
sx := (int(tile) % tileSetWidth) * tileSize
sy := (int(tile) / tileSetWidth) * tileSize
return tileSet.SubImage(image.Rect(sx, sy, sx+tileSize, sy+tileSize)).(*ebiten.Image)
}
type IRunningMode interface {
ProcessKeyEvents()
Draw(*ebiten.Image)
}
type TextFormat struct {
scale float64
textColor color.Color
shadow bool
}
type IGameplayMode interface {
Draw(*ebiten.Image)
ProcessKeyEvents() bool
Update()
}
type GameplayMode struct {
gameplayScreen *GameplayScreen
}
func (*GameplayMode) Draw(screen *ebiten.Image) {
}
func (*GameplayMode) Update() {
}
func (*GameplayMode) ProcessKeyEvents() bool {
return true
}
type GameplayModeDefault struct {
GameplayMode
}
func (mode *GameplayModeDefault) ProcessKeyEvents() bool {
player := mode.gameplayScreen.game.char
if inpututil.IsKeyJustReleased(keyBinds[kbPlayerMoveRight]) ||
inpututil.IsKeyJustReleased(keyBinds[kbPlayerMoveLeft]) ||
inpututil.IsKeyJustReleased(keyBinds[kbPlayerMoveUp]) ||
inpututil.IsKeyJustReleased(keyBinds[kbPlayerMoveDown]) {
player.EndWalk()
return false
}
if inpututil.IsKeyJustPressed(keyBinds[kbPlayerMoveRight]) {
player.StartWalk(LooksRight)
return false
}
if inpututil.IsKeyJustPressed(keyBinds[kbPlayerMoveLeft]) {
player.StartWalk(LooksLeft)
return false
}
if inpututil.IsKeyJustPressed(keyBinds[kbPlayerMoveUp]) {
player.StartWalk(LooksUp)
return false
}
if inpututil.IsKeyJustPressed(keyBinds[kbPlayerMoveDown]) {
player.StartWalk(LooksDown)
return false
}
return true
}
func NewGameplayModeDefault(s *GameplayScreen) *GameplayModeDefault {
mode := new(GameplayModeDefault)
mode.gameplayScreen = s
game := mode.gameplayScreen.game
game.camera.TargetEntity(game.char)
return mode
}
type GameplayModeEntityFocusRotation struct {
GameplayMode
}
func (mode *GameplayModeEntityFocusRotation) Draw(screen *ebiten.Image) {
game := mode.gameplayScreen.game
game.DrawModeTitle(screen, I18n("string_entity_focus_rotation", "Entity Focus Rotation"))
game.DrawEntityInfo(screen, game.camera.targetEntity)
}
func (mode *GameplayModeEntityFocusRotation) Update() {
if tickCounter%16 == 0 {
game := mode.gameplayScreen.game
if len(game.entities) == 1 {
game.camera.TargetEntity(mode.gameplayScreen.game.entities[0])
} else if len(game.entities) > 0 {
game.camera.TargetEntity(game.entities[r.Intn(len(game.entities)-1)])
}
}
}
func (mode *GameplayModeEntityFocusRotation) ProcessKeyEvents() bool {
if inpututil.IsKeyJustPressed(keyBinds[kbToggleEntityFocusRotation]) {
mode.gameplayScreen.SetGameplayMode(NewGameplayModeDefault(mode.gameplayScreen))
return false
}
return true
}
func NewGameplayModeEntityFocusRotation(s *GameplayScreen) *GameplayModeEntityFocusRotation {
mode := new(GameplayModeEntityFocusRotation)
mode.gameplayScreen = s
return mode
}
// Delay the time before next loading log entry will append
func lazyAppend(target []string, el string) []string {
target = append(target, el)
// time.Sleep(time.Second)
return target
}
func LoadImage(path string) *ebiten.Image {
loadingLog = lazyAppend(loadingLog, "Loading image \""+path+"\"")
image, _, _ := ebitenutil.NewImageFromFile(path)
return image
}
var splash *ebiten.Image
// The main class that maintains global game state
//
// Implements ebiten.Game
type Game struct {
level *Level
char ICharacter
entities []ILivingEntity
entityListMutex sync.RWMutex
view View
gameOver bool
fontRenderer *FontRenderer
systemFontRenderer *FontRenderer
debugScreen *DebugScreen
showDebugInfo bool
camera Camera
ready bool
appTicker int
currentScreen IScreen
audioManager *AudioManager
volumeMusic float64
}
func (g *Game) WorldPosToTilePos(worldX float64, worldY float64) (int, error) {
x := int(worldX)
y := int(worldY)
return y/tileSize*g.level.width + x/tileSize, nil
}
func (g *Game) GetUnderlyingTilesAt(worldX float64, worldY float64) ([]Tile, error) {
var tiles []Tile
tilePos, err := g.WorldPosToTilePos(worldX, worldY)
if err != nil {
return nil, err
}
for _, layer := range g.level.tileLayers {
if layer[tilePos] != tileIDEmpty {
tiles = append(tiles, layer[tilePos])
}
}
return tiles, err
}
func (g *Game) IsTileSolidAt(worldX float64, worldY float64) bool {
tiles, _ := g.GetUnderlyingTilesAt(worldX, worldY)
for _, underlyingTile := range tiles {
if !tileDescStorage[underlyingTile].Walkable {
return true
}
}
return false
}
func (g *Game) GameOver() {
g.gameOver = true
}
func (g *Game) Reset() {
g.char = CreateCharacter(g)
g.gameOver = false
}
func (g *Game) ProcessTileEffects(e ILivingEntity, tile Tile) {
switch id := tile; id {
case tileIDVoid:
e.GetLivingEntity().health -= 0.1
case tileIDWater:
e.GetLivingEntity().SetSpeedModifier(0.25)
case tileIDSwitch:
tilePos, _ := e.GetTilePos()
for _, layer := range g.level.tileLayers {
if layer[tilePos] == tileIDSwitch {
layer[tilePos] = tileIDSwitchActive
for j, layer := range g.level.tileLayers {
for k, tile := range layer {
if tile == tileIDThornsActive {
g.level.tileLayers[j][k] = tileIDThorns
}
}
}
}
}
case tileIDButton:
tilePos, _ := e.GetTilePos()
for _, layer := range g.level.tileLayers {
if layer[tilePos] == tileIDButton {
layer[tilePos] = tileIDButtonPushed
}
}
case tileIDThornsActive:
e.GetLivingEntity().health -= 10.0
case tileIDLaptop:
switch e.GetLivingEntity().etype.(type) {
case Character:
g.SetScreen(NewComputerScreen(g, func(g *Game) IScreen { return NewGameplayScreen(g) }))
}
}
}
var langData map[string]string
func (g *Game) Load() {
loadingLog = append(loadingLog, "Loading Keybinds")
keyBinds = KeyBindMap{
kbPlayerMoveRight: ebiten.KeyArrowRight,
kbPlayerMoveLeft: ebiten.KeyArrowLeft,
kbPlayerMoveUp: ebiten.KeyArrowUp,
kbPlayerMoveDown: ebiten.KeyArrowDown,
kbToggleEntityFocusRotation: ebiten.KeyF7,
kbToggleEditMode: ebiten.KeyF8,
kbEditorNextLayer: ebiten.KeyX,
kbEditorPrevLayer: ebiten.KeyZ,
kbEditorNextBrush: ebiten.KeyNumpadAdd,
kbEditorPrevBrush: ebiten.KeyNumpadSubtract,
kbEditorPlace: ebiten.KeyEnter,
kbEditorDelete: ebiten.KeyDelete,
kbEditorSwitchMode: ebiten.KeyS,
kbShowDebugInfo: ebiten.KeyF3,
kbWorldZoomOut: ebiten.KeyO,
kbWorldZoomIn: ebiten.KeyP,
}
langFile, _ := ioutil.ReadFile("lang/ru_ru.json")
langData = make(map[string]string)
_ = json.Unmarshal([]byte(langFile), &langData)
g.audioManager = NewAudioManager(g)
g.volumeMusic = 0.5
g.audioManager.Load("bgm/stage_prepare", "sound/prepare.ogg")
g.audioManager.Load("bgm/main_menu", "sound/bgm_main_menu.ogg")
g.audioManager.Load("bgm/level0", "sound/bgm_level0.ogg")
g.audioManager.Load("bgm/computer", "sound/computer.ogg")
g.audioManager.Load("winxp/critical_stop", "sound/critical_stop.ogg")
am := AssetManager_GetInstance()
am.Load("game/tile_map", "assets/tilemap2.png")
am.Load("game/character/player", "assets/char2.png")
am.Load("game/character/morgen", "assets/morgen.png")
am.Load("game/character/michael", "assets/michael.png")
am.Load("game/character/flan", "assets/flan.png")
am.Load("game/character/monobear", "assets/monobear.png")
am.Load("gui/frame", "assets/gui_frame.png")
am.Load("gui/frame_herb", "assets/gui_frame_herb.png")
am.Load("gui/button", "assets/gui_button.png")
am.Load("winxp/boot_logo", "assets/boot.png")
tilesImage = LoadImage("assets/tilemap2.png")
charSprite = LoadImage("assets/char2.png")
morgenSprite = LoadImage("assets/morgen.png")
michaelSprite = LoadImage("assets/michael.png")
flanSprite = LoadImage("assets/flan.png")
monobearSprite = LoadImage("assets/monobear.png")
guiFrameTest = LoadImage("assets/gui_frame_test.png")
guiFrameHerb = LoadImage("assets/gui_frame_herb.png")
guiButton = LoadImage("assets/gui_button.png")
tileCursor = LoadImage("assets/tile_selector.png")
explosionSprite = LoadImage("assets/explosion.png")
skillMonobearExplosion = LoadImage("assets/spell_monobear_explosion.png")
seeYaTileSet = LoadImage("assets/seeya.png")
worldBorderImage = LoadImage("assets/world_border.png")
xpCaption = LoadImage("assets/computer/frame_caption.png")
xpLeftFrame = LoadImage("assets/computer/frame_left.png")
xpRightFrame = LoadImage("assets/computer/frame_right.png")
xpBottomFrame = LoadImage("assets/computer/frame_bottom.png")
xpCloseButton = LoadImage("assets/computer/close_button.png")
xpCloseGlyph = LoadImage("assets/computer/close_glyph.png")
xpIconError = LoadImage("assets/computer/icon_error.png")
xpButton = LoadImage("assets/computer/button.png")
loadingLog = lazyAppend(loadingLog, "Loading font_fantasy")
g.fontRenderer = NewFontRenderer()
loadingLog = lazyAppend(loadingLog, "Loading level")
g.LoadLevel("level/level0.lvl")
loadingLog = lazyAppend(loadingLog, "Setting camera zoom")
g.camera.SetZoom(4.0)
loadingLog = lazyAppend(loadingLog, "Setting GUI scale")
g.view.guiScale = 2.0
loadingLog = lazyAppend(loadingLog, "Creating character")
g.char = CreateCharacter(g)
loadingLog = lazyAppend(loadingLog, "Appending character to entity list")
g.entities = append(g.entities, g.char)
for i := 0; i < 8; i++ {
g.entities = append(g.entities, CreateMichael(g))
}
loadingLog = lazyAppend(loadingLog, "Creating Debug Screen")
g.debugScreen = CreateDebugScreen(g)
loadingLog = lazyAppend(loadingLog, "Ready")
g.ready = true
}
func (g *Game) SetScreen(screen IScreen) {
if g.currentScreen != nil {
g.currentScreen.OnDetach()
}
if screen != nil {
screen.OnAttach()
}
g.currentScreen = screen
}
const (
buttonStateNormal = iota
buttonStateHover
buttonStatePressed
)
func (g *Game) DrawStatedNineGrid(screen *ebiten.Image,
img *ebiten.Image,
state int,
nStates int,
scale float64,
gridInfo NineGridInfo,
x float64,
y float64,
width float64,
height float64) {
atlasWidth, atlasHeight := img.Size()
stateWidth := atlasWidth
stateHeight := atlasHeight / nStates
sx := 0
sy := stateHeight * state
stateImage := img.SubImage(image.Rect(sx, sy, sx+stateWidth, sy+stateHeight)).(*ebiten.Image)
g.DrawNineGrid(screen, stateImage, scale, gridInfo, x, y, width, height)
}
func (g *Game) DrawGUIButton(screen *ebiten.Image, state int, x float64, y float64, width float64, height float64) {
am := AssetManager_GetInstance()
atlasWidth, atlasHeight := am.Get("gui/button").Size()
buttonWidth := atlasWidth
buttonHeight := atlasHeight / 3
// sx := 0
sy := buttonHeight * state
buttonImage := am.Get("gui/button").SubImage(image.Rect(0, sy, buttonWidth, sy+buttonHeight)).(*ebiten.Image)
g.DrawNineGrid(screen, buttonImage, g.view.guiScale, NineGridInfo{Right: 5, Top: 5, Left: 5, Bottom: 5}, x, y, width, height)
}
type NineGridInfo struct {
Top int
Bottom int
Left int
Right int
}
const (
nineGridNW = 0b00001001
nineGridTop = 0b00001010
nineGridNE = 0b00001100
nineGridLeft = 0b00010001
nineGridCenter = 0b00010010
nineGridRight = 0b00010100
nineGridSW = 0b00100001
nineGridBottom = 0b00100010
nineGridSE = 0b00100100
)
const (
nineGridColumnLeft = 1
nineGridColumnCenter = 2
nineGridColumnRight = 4
nineGridRowTop = 8
nineGridRowCenter = 16
nineGridRowBottom = 32
)
func NineGridGetTile_0(img *ebiten.Image, gridTile int, gridInfo NineGridInfo) *ebiten.Image {
var x, y, width, height int
atlasWidth, atlasHeight := img.Size()
bounds := Recti{
Vec2i{gridInfo.Left, gridInfo.Top},
Vec2i{gridInfo.Right, gridInfo.Bottom},
}
switch gridTile {
case nineGridNW:
x = 0
y = 0
width = bounds.p1.X
height = bounds.p1.Y
case nineGridTop:
x = bounds.p1.X
y = 0
width = atlasWidth - (bounds.p1.X + bounds.p2.X)
height = bounds.p1.Y
case nineGridNE:
x = atlasWidth - bounds.p2.X
y = 0
width = bounds.p2.X
height = bounds.p1.Y
case nineGridLeft:
x = 0
y = bounds.p1.Y
width = bounds.p1.X
height = atlasHeight - (bounds.p1.Y + bounds.p2.Y)
case nineGridCenter:
x = bounds.p1.X
y = bounds.p1.Y
width = atlasWidth - (bounds.p1.X + bounds.p2.X)
height = atlasHeight - (bounds.p1.Y + bounds.p2.Y)
case nineGridRight:
x = atlasWidth - bounds.p2.X
y = bounds.p1.Y
width = bounds.p2.X
height = atlasHeight - (bounds.p1.Y + bounds.p2.Y)
case nineGridSW:
x = 0
y = atlasHeight - bounds.p2.Y
width = bounds.p1.X
height = bounds.p2.Y
case nineGridBottom:
x = bounds.p1.X
y = atlasHeight - bounds.p2.Y
width = atlasWidth - (bounds.p1.X + bounds.p2.X)
height = bounds.p2.Y
case nineGridSE:
x = atlasWidth - bounds.p2.X
y = atlasHeight - bounds.p2.Y
width = bounds.p2.X
height = bounds.p2.Y
}
min := img.Bounds().Min
return img.SubImage(image.Rect(
min.X+x,
min.Y+y,
min.X+x+width,
min.Y+y+height)).(*ebiten.Image)
}
func NineGridGetTile(img *ebiten.Image, gridTile int, gridInfo NineGridInfo) *ebiten.Image {
var x, y, width, height int
atlasWidth, atlasHeight := img.Size()
bounds := Recti{
Vec2i{gridInfo.Left, gridInfo.Top},
Vec2i{gridInfo.Right, gridInfo.Bottom},
}
if gridTile&nineGridColumnLeft != 0 {
width = bounds.p1.X
} else if gridTile&nineGridColumnCenter != 0 {
x = bounds.p1.X
width = atlasWidth - (bounds.p1.X + bounds.p2.X)
} else if gridTile&nineGridColumnRight != 0 {
x = atlasWidth - bounds.p2.X
width = bounds.p2.X
}
if gridTile&nineGridRowTop != 0 {
height = bounds.p1.Y
} else if gridTile&nineGridRowCenter != 0 {
y = bounds.p1.Y
height = atlasHeight - (bounds.p1.Y + bounds.p2.Y)
} else if gridTile&nineGridRowBottom != 0 {
y = atlasHeight - bounds.p2.Y
height = bounds.p2.Y
}
min := img.Bounds().Min
return img.SubImage(image.Rect(
min.X+x,
min.Y+y,
min.X+x+width,
min.Y+y+height)).(*ebiten.Image)
}
func NineGridGetTile2(gridTile int, gridInfo *NineGridInfo2) *ebiten.Image {