-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
311 lines (241 loc) · 6.32 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
package main
import (
"image"
_ "image/png"
"log"
"math/rand"
paths "github.com/SolarLune/paths"
"github.com/hajimehoshi/ebiten"
lua "github.com/yuin/gopher-lua"
"golang.org/x/image/font"
)
const (
screenWidth = 1366 / 3 // Multiplied by 3 later to scale images
screenHeight = 768 / 3 // ^
)
var (
gameInitialized bool = false
testBackgroundImage *ebiten.Image
mdataFont font.Face
mversionFont font.Face
gameReference *Game
tempSpawnCount int = 1
)
// Game is the info for the game
type Game struct {
player Player
cursor Cursor
enemies []Enemy
enemySpawners []EnemySpawner
items []Item
gibHandlers []GibHandler
bloodEmitters []BloodEmitter
bulletExplosions []BulletExplosion
shadows []*Shadow
walls []Tile
tiles [][]Tile
borders []Border
ui []UI
maps []Map
portals []Portal
currentMap Map
lightHandler LightHandler // Controls the lights!
shadowID int // Shadow IDs, starts at 0 then increments when a shadow is added
state int // The game state, 0 is in main menu, 1 is in game, 2 is paused
settings Settings // Game settings
}
// Init initializes the game
func (g *Game) Init() {
// Set gameReference
gameReference = g
// Init lightHandler
g.lightHandler = initLightHandler()
playerLightID := g.lightHandler.addLight(g.lightHandler.lightImages.playerLight, 0)
// Init maps
initMaps(g)
// Player
g.player = createPlayer(
newVec2f(screenWidth/2, screenHeight/2),
g,
playerLightID,
)
g.shadows = append(g.shadows, &g.player.shadow)
// Test items! ============================
// TODO: REMOVE THIS v
testItem := createItem(
newVec2f(300, 100), // Position
iitemsSpritesheet, // Image
)
testItem.init()
g.items = append(g.items, testItem)
testItem = createItem(
newVec2f(200, 100), // Position
iitemsSpritesheet, // Image
)
testItem.init()
g.items = append(g.items, testItem)
testItem = createItem(
newVec2f(100, 100), // Position
iitemsSpritesheet, // Image
)
testItem.init()
g.items = append(g.items, testItem)
// TODO: REMOVE THIS ^
// Test items! ============================
// Cursor
g.cursor = createCursor(iUISpritesheet)
// Background image
testBackgroundImage, _ = loadImage("./Assets/Art/background.png")
// Fonts
g.InitFonts()
// Generate starting walls
g.walls = generateWalls(itileSpritesheet)
g.tiles = generateTiles(itileSpritesheet)
generateBigTiles(g.tiles, itileSpritesheet)
g.borders = generateBorders(itileSpritesheet)
g.ui = generateUI(iUISpritesheet)
// Init lua functions
L := lua.NewState()
defer L.Close()
initLuaFunctions(L)
// Make the astar path channel
astarChannel = make(chan *paths.Path, 500)
// State starts in game [temporary]
g.state = 1
// Init music
loadMusic()
// Play song
//go music[rand.Intn(len(music)-1)].play()
// GAME SETTINGS
loadSettings(&g.settings)
if g.settings.Graphics.Fullscreen { // Enable fullscreen if enabled
ebiten.SetFullscreen(true)
}
// Enemy spawners
g.enemySpawners = append(g.enemySpawners, createEnemySpawner(g.currentMap.randomPosition(), EBeefEye, float64(rand.Intn(600)+60), image.Rect(31, 50, 59, 71)))
g.enemySpawners = append(g.enemySpawners, createEnemySpawner(g.currentMap.randomPosition(), EWorm, float64(rand.Intn(300)+50), image.Rect(0, 0, 17, 22)))
}
// Update updates the game
func (g *Game) Update(screen *ebiten.Image) error {
if !gameInitialized {
g.Init()
gameInitialized = true
}
// Update game
if g.state == 1 {
updateGame(screen, g)
}
return nil
}
func updateGame(screen *ebiten.Image, g *Game) {
// Update cursor
g.cursor.update()
// Update map
updateMaps()
// Update player
g.player.update(g)
g.player.gun.updateBullets(g)
updateBulletExplosions(g)
// Update items
updateItems(g)
for i := 0; i < len(g.portals); i++ {
g.portals[i].update(g)
}
// Update enemies
for i := range g.enemySpawners {
g.enemySpawners[i].update(g)
}
// Update gib handlers
updateGibHandlers(g)
// Update light
g.lightHandler.update()
updateEnemies(g)
// Game info update/check
checkChangeDisplayInfo()
// Update UI
updateUI(g)
}
// Draw renders everything!
func (g *Game) Draw(screen *ebiten.Image) {
bgop := &ebiten.DrawImageOptions{}
screen.DrawImage(testBackgroundImage, bgop)
// Draw game
if g.state == 1 { // inGame
drawGame(screen, g)
}
}
func drawGame(screen *ebiten.Image, g *Game) {
// Render game walls/tiles
renderTiles(g, screen)
// Render maps
renderMaps(screen)
// Render gibHandlers
renderGibHandlers(g, screen)
renderItems(g, screen)
// Render shadows
for i := 0; i < len(g.shadows); i++ {
g.shadows[i].render(screen)
}
// Render portals
for i := 0; i < len(g.portals); i++ {
g.portals[i].render(screen)
}
// Render enemies behind player
for _, s := range g.enemySpawners {
s.render(screen)
}
renderEnemies(g, screen)
// Render player
g.player.render(screen)
g.player.gun.renderBullets(screen)
if g.player.isDrawable {
g.player.gun.render(screen)
}
renderBulletExplosions(g, screen)
// Render borders
for _, b := range g.borders {
b.render(screen)
}
// Render light
g.lightHandler.render(screen)
// Basic text render calls
if displayInfo {
displayGameInfo(screen, g.player)
}
// Render UI
renderUI(g, screen)
// Render cursor
g.cursor.render(screen)
}
// Layout is the screen layout?...
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return screenWidth, screenHeight
}
func main() {
loadPregameResources()
ebiten.SetWindowSize(screenWidth*3, screenHeight*3)
ebiten.SetWindowTitle("U N R A Y")
ebiten.SetWindowResizable(true)
// Load icon
icon16x16, _ := loadRegularImage("./Assets/Art/Icon/icon16.png")
icon32x32, _ := loadRegularImage("./Assets/Art/Icon/icon32.png")
icon48x48, _ := loadRegularImage("./Assets/Art/Icon/icon48.png")
icon64x64, _ := loadRegularImage("./Assets/Art/Icon/icon64.png")
ebiten.SetWindowIcon([]image.Image{icon16x16, icon32x32, icon48x48, icon64x64})
// Hide cursor
ebiten.SetCursorMode(ebiten.CursorModeHidden)
if err := ebiten.RunGame(&Game{}); err != nil {
log.Fatal(err)
}
}
func loadPregameResources() {
// Images
loadPlayerImages()
loadUIImages()
loadTileImages()
loadItemsImages()
loadEnemiesImages()
loadParticlesImages()
loadLightingImages()
initListOfAllItemNames()
}