-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
241 lines (213 loc) · 9.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
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
package main
import (
"fmt"
rg "github.com/gen2brain/raylib-go/raygui"
rl "github.com/gen2brain/raylib-go/raylib"
"runtime/debug"
)
const (
WindowWidth = 1600
WindowHeight = 980
)
// Commit Git hash for current commit
var Commit = func() string {
if info, ok := debug.ReadBuildInfo(); ok {
for _, setting := range info.Settings {
if setting.Key == "vcs.revision" {
return setting.Value
}
}
}
return ""
}()
func main() {
var version = fmt.Sprintf("v1.5/%s", Commit[:7])
fmt.Printf("Starting Chess Heaven and Hell %s\n", version)
rl.SetConfigFlags(rl.FlagWindowResizable)
rl.InitWindow(WindowWidth, WindowHeight, fmt.Sprintf("Chess - Heaven and Hell %s", version))
defer rl.CloseWindow()
rl.SetExitKey(rl.KeyNull)
rl.InitAudioDevice()
defer rl.CloseAudioDevice()
rl.SetTargetFPS(60)
assets.LoadAll()
defer assets.UnloadAll()
rg.LoadStyleDefault()
rg.SetFont(assets.fontComicSansMs)
rg.SetStyle(rg.DEFAULT, rg.TEXT_SIZE, 20)
registerPieceTypes()
registerStatusEffectTypes()
registerObstacleTypes()
setupBoard(0, BoardStyleHeaven, false)
setupBoard(1, BoardStyleEarth, true)
setupBoard(2, BoardStyleHell, false)
sandbox.NewObstacle(Vec2{1, 2}, 0, sandbox.GetObstacleTypeByName(NameCoin).Id)
sandbox.NewObstacle(Vec2{1, 2}, 0, sandbox.GetObstacleTypeByName(NameCoin).Id)
sandbox.NewObstacle(Vec2{6, 5}, 0, sandbox.GetObstacleTypeByName(NameCoin).Id)
sandbox.NewObstacle(Vec2{6, 5}, 0, sandbox.GetObstacleTypeByName(NameCoin).Id)
sandbox.NewObstacle(Vec2{2, 5}, 0, sandbox.GetObstacleTypeByName(NameChaosOrb).Id)
sandbox.NewObstacle(Vec2{5, 2}, 0, sandbox.GetObstacleTypeByName(NameChaosOrb).Id)
sandbox.NewObstacle(Vec2{6, 2}, 2, sandbox.GetObstacleTypeByName(NameFire).Id)
sandbox.NewObstacle(Vec2{1, 5}, 2, sandbox.GetObstacleTypeByName(NameFire).Id)
sandbox.NewObstacle(Vec2{2, 2}, 2, sandbox.GetObstacleTypeByName(NameChaosOrb).Id)
sandbox.NewObstacle(Vec2{5, 5}, 2, sandbox.GetObstacleTypeByName(NameChaosOrb).Id)
var undo = NewUndoRedoSystem()
var ui = NewUiState()
defer ui.Dispose()
var exit = false
for !exit {
if rl.WindowShouldClose() {
exit = AskAboutSaveBeforeExit(&sandbox, &undo)
} else {
CheckSavingAndLoading(&sandbox, &undo)
}
ui.Update()
handleBoardInteraction(&undo, &ui)
rl.BeginDrawing()
rl.ClearBackground(rl.RayWhite)
if ui.tab == TabBoard {
sandbox.Render(uint32(ui.board), false, &ui.selection)
}
ui.Render(&undo)
rl.EndDrawing()
}
}
func IsCoordUnderUi(coord Vec2) bool {
return coord.X < -2 || coord.X >= 12 || coord.Y < -2 || coord.Y >= 10
}
func handleBoardInteraction(undo *UndoRedoSystem, ui *UiState) {
handleMouseInteraction(undo, ui)
var ctrlDown = rl.IsKeyDown(rl.KeyLeftControl) || rl.IsKeyDown(rl.KeyLeftControl)
if pieceId, ok := ui.selection.GetSelectedPieceId(); ok && ui.tab == TabBoard {
if rl.IsKeyPressed(rl.KeyDelete) || rl.IsKeyPressed(rl.KeyBackspace) {
undo.Append(NewDeletePieceCmd(&sandbox, ui, pieceId))
} else if rl.IsKeyPressed(rl.KeyC) && ctrlDown {
var piece = sandbox.GetPiece(pieceId)
ui.clipboard.StorePiece(piece.Typ, piece.Color, piece.Scale, sandbox.GetStatusEffectsOnPiece(pieceId))
} else if rl.IsKeyPressed(rl.KeyX) && ctrlDown {
var piece = sandbox.GetPiece(pieceId)
ui.clipboard.StorePiece(piece.Typ, piece.Color, piece.Scale, sandbox.GetStatusEffectsOnPiece(pieceId))
undo.Append(NewDeletePieceCmd(&sandbox, ui, pieceId))
} else if rl.IsKeyReleased(rl.KeyD) && ctrlDown {
undo.Append(NewDuplicatePieceCmd(&sandbox, ui, pieceId))
} else if rl.IsKeyPressed(rl.KeyC) {
var newColor = 1 - sandbox.GetPiece(pieceId).Color
undo.Append(NewChangeColorOfPieceCmd(&sandbox, pieceId, newColor))
}
}
if rl.IsKeyPressed(rl.KeyZ) && ctrlDown {
undo.Undo(&sandbox, ui)
} else if rl.IsKeyPressed(rl.KeyY) && ctrlDown {
undo.Redo(&sandbox, ui)
} else if rl.IsKeyPressed(rl.KeyV) && ctrlDown {
if !ui.clipboard.isEmpty && ui.tab == TabBoard {
var coord = GetHoveredCoord()
if !IsCoordUnderUi(coord) {
undo.Append(NewPastePieceCmd(&sandbox, ui, coord, uint32(ui.board)))
}
}
}
}
func handleMouseInteraction(undo *UndoRedoSystem, ui *UiState) {
if ui.tab != TabBoard {
return
}
var coord = GetHoveredCoord()
if IsCoordUnderUi(coord) {
return
}
if rl.IsMouseButtonPressed(rl.MouseButtonLeft) {
var coord = GetHoveredCoord()
if ui.mode == 0 {
var piece = sandbox.GetPieceAtVisual(coord, uint32(ui.board))
if piece == nil {
ui.selection.Deselect()
} else {
ui.selection.SelectPiece(piece.Id)
}
} else if ui.mode == 1 {
ui.selection.SelectCoord(coord)
}
} else if id, ok := ui.selection.GetSelectedPieceId(); ok {
if rl.IsMouseButtonPressed(rl.MouseButtonRight) && !rl.IsKeyDown(rl.KeyRightControl) && !rl.IsKeyDown(rl.KeyLeftControl) {
var piece = sandbox.GetPiece(id)
if piece.Coord != coord || piece.Board != uint32(ui.board) {
undo.Append(NewMovePieceCmd(&sandbox, id, coord, uint32(ui.board)))
}
}
} else if id, ok := ui.selection.GetSelectedPieceTypeId(); ok {
if rl.IsMouseButtonPressed(rl.MouseButtonRight) {
undo.Append(NewCreatePieceCmd(&sandbox, id, PieceColor(ui.color), uint32(ui.board), coord))
}
}
}
func registerPieceTypes() {
sandbox.RegisterPieceType(NamePawn, assets.texWhitePawn, assets.texBlackPawn)
sandbox.RegisterPieceType(NameKnight, assets.texWhiteKnight, assets.texBlackKnight)
sandbox.RegisterPieceType(NameBishop, assets.texWhiteBishop, assets.texBlackBishop)
sandbox.RegisterPieceType(NameRook, assets.texWhiteRook, assets.texBlackRook)
sandbox.RegisterPieceType(NameQueen, assets.texWhiteQueen, assets.texBlackQueen)
sandbox.RegisterPieceType(NameKing, assets.texWhiteKing, assets.texBlackKing)
sandbox.RegisterPieceType(NameBomber, assets.texWhiteBomber, assets.texBlackBomber)
sandbox.RegisterPieceType(NameLeopard, assets.texWhiteLeopard, assets.texBlackLeopard)
sandbox.RegisterPieceType(NameChecker, assets.texWhiteChecker, assets.texBlackChecker)
sandbox.RegisterPieceType(NameMountedArcher, assets.texWhiteMountedArcher, assets.texBlackMountedArcher)
sandbox.RegisterPieceType(NameWizard, assets.texWhiteWizard, assets.texBlackWizard)
sandbox.RegisterPieceType(NameArchbishop, assets.texWhiteArchbishop, assets.texBlackArchbishop)
sandbox.RegisterPieceType(NameFortress, assets.texWhiteFortress, assets.texBlackFortress)
sandbox.RegisterPieceType(NameScout, assets.texWhiteScout, assets.texBlackScout)
sandbox.RegisterPieceType(NameWarlock, assets.texWhiteWarlock, assets.texBlackWarlock)
sandbox.RegisterPieceType(NameCelestial, assets.texAngel, assets.texImp)
}
func registerStatusEffectTypes() {
sandbox.RegisterEffectType(NameBloody, RenderStyleBottom, assets.texEffectBlood)
sandbox.RegisterEffectType(NameExperience, RenderStyleBottom, assets.texEffectMedal)
sandbox.RegisterEffectType(NameCurse, RenderStyleBottom, assets.texEffectCurse)
sandbox.RegisterEffectType(NameForcedMove, RenderStyleBottom, assets.texEffectForcedMove)
sandbox.RegisterEffectType(NamePaid2ndMove, RenderStyleBottom, assets.texEffectPaid2ndMove)
sandbox.RegisterEffectType(NamePortalGun, RenderStyleOverlay, assets.texEffectPortalGun)
sandbox.RegisterEffectType(NameStonks, RenderStyleOverlay, assets.texEffectStonks)
sandbox.RegisterEffectType(NameStun, RenderStyleStun, assets.texEffectStun)
sandbox.RegisterEffectType(NameWizardHat, RenderStyleOverlay, assets.texEffectWizardHat)
}
func registerObstacleTypes() {
sandbox.RegisterObstacleType(NameChaosOrb, assets.texObstacleChaosOrb)
sandbox.RegisterObstacleType(NameCoin, assets.texObstacleCoin)
sandbox.RegisterObstacleType(NameIce, assets.texObstacleIce)
sandbox.RegisterObstacleType(NameFire, assets.texObstacleFire)
}
func setupBoard(boardId uint32, style BoardStyle, withPieces bool) {
var board = sandbox.GetBoard(boardId)
board.Id = boardId
board.Style = style
for y := 0; y < 8; y++ {
for x := 0; x < 8; x++ {
sandbox.NewTile(board.Id, Vec2{x, y})
}
}
if !withPieces {
return
}
sandbox.NewPieceFromName(NameRook, BLACK, boardId, Vec2{0, 0})
sandbox.NewPieceFromName(NameKnight, BLACK, boardId, Vec2{1, 0})
sandbox.NewPieceFromName(NameBishop, BLACK, boardId, Vec2{2, 0})
sandbox.NewPieceFromName(NameQueen, BLACK, boardId, Vec2{3, 0})
sandbox.NewPieceFromName(NameKing, BLACK, boardId, Vec2{4, 0})
sandbox.NewPieceFromName(NameBishop, BLACK, boardId, Vec2{5, 0})
sandbox.NewPieceFromName(NameKnight, BLACK, boardId, Vec2{6, 0})
sandbox.NewPieceFromName(NameRook, BLACK, boardId, Vec2{7, 0})
for x := 0; x < 8; x++ {
sandbox.NewPieceFromName(NamePawn, BLACK, boardId, Vec2{x, 1})
}
sandbox.NewPieceFromName(NameRook, WHITE, boardId, Vec2{0, 7})
sandbox.NewPieceFromName(NameKnight, WHITE, boardId, Vec2{1, 7})
sandbox.NewPieceFromName(NameBishop, WHITE, boardId, Vec2{2, 7})
sandbox.NewPieceFromName(NameQueen, WHITE, boardId, Vec2{3, 7})
sandbox.NewPieceFromName(NameKing, WHITE, boardId, Vec2{4, 7})
sandbox.NewPieceFromName(NameBishop, WHITE, boardId, Vec2{5, 7})
sandbox.NewPieceFromName(NameKnight, WHITE, boardId, Vec2{6, 7})
sandbox.NewPieceFromName(NameRook, WHITE, boardId, Vec2{7, 7})
for x := 0; x < 8; x++ {
sandbox.NewPieceFromName(NamePawn, WHITE, boardId, Vec2{x, 6})
}
}