-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.wren
105 lines (92 loc) · 3.5 KB
/
game.wren
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
/// The imports of files that are used in this file usually go at the top.
/// If two files import each other (circular dependencies), then import go to the bottom of the file.
import "xs" for Data, Input, Render
// ^ ^ class names that are imported
// | file name without extension, relative to the this file (except shared modules and system libs)
import "xs_math"for Math, Bits, Vec2, Color
import "xs_assert" for Assert
import "xs_ec"for Entity, Component
import "xs_components" for Transform, Body, Renderable, Sprite, GridSprite, AnimatedSprite
import "types" for Type
import "directions" for Directions
import "gameplay" for Level
import "background" for Background
// There needs class called Game in you main file
class Game {
// There are the states of the game
// Wren does not have enums, so we use static variables
static loading { 0 }
static generating { 1 }
static starting { 2 }
static playing { 3 }
static gameover { 4 }
// Initialize the game, which means initializing all the systems
// and some variables that are used in the game`s logic
static initialize() {
Entity.initialize()
Level.initialize()
Tile.initialize()
Create.initialize()
Gameplay.initialize()
__time = 0
__state = generating // Skip loading
var alg = Data.getNumber("Algorithm|Single Room|Randy|RandomWalk|BSP")
if(alg == 0) {
__alg = SingleRoom
} else if(alg == 1) {
__alg = Randy
} else if(alg == 2) {
__alg = RandomWalk
} else if(alg == 3) {
__alg = BSPer
} else if(alg == 4) {
__alg = MyRandomWalker
} else {
System.print("Invalid algorithm number, using default")
__alg = SingleRoom.new()
}
__genFiber = Fiber.new { __alg.generate() }
__background = Background.new()
}
// Update the game, which means updating all the systems
static update(dt) {
if(__state == Game.generating) {
genStep(dt)
} else {
Gameplay.update(dt)
}
Entity.update(dt)
__alg.debugRender()
__background.update(dt)
}
// This function is called when the game is in the generating state
// It is used to generate the level in steps, so the player can see the progress
// it uses a fiber and a coroutine to do that. It's advance(ish) stuff, can be ignored
static genStep(dt) {
var visualize = Data.getBool("Visualize Generation", Data.debug)
if(visualize) {
__time = __time - dt
if(__time <= 0.0) {
if(!__genFiber.isDone) {
__time = __genFiber.call()
} else {
__state = Game.playing
}
}
} else {
while(!__genFiber.isDone) {
__genFiber.call()
}
__state = playing
}
}
// Render the game, which means rendering all the systems and entities
static render() {
__background.render()
Gameplay.render()
}
}
/// Import classes from other files that might have circular dependencies (import each other)
import "create" for Create
import "generators" for SingleRoom, Randy, BSPer, RandomWalk, MyRandomWalker //If you create a new Generator then add it's classname here
import "gameplay" for Hero, Tile, Gameplay