-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame.lua
171 lines (139 loc) · 4.2 KB
/
game.lua
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
require 'entity'
require 'area'
require 'iso'
local game = Context()
function game:init()
-- Setup camera
game.camera = Camera {
scale = config.cameraScale
}
-- Setup cursor
game.cursor = {
tile = nil,
}
-- Create a test area
game.area = Area()
-- Done loading
console:write('Game initialized')
game:printKeys()
end
function game:parseInput(dt)
-- Move camera
local kd = love.keyboard.isDown
if kd('d') or kd('right') then game.camera.x = game.camera.x + dt * config.cameraSpeed end
if kd('a') or kd('left') then game.camera.x = game.camera.x - dt * config.cameraSpeed end
if kd('w') or kd('up') then game.camera.y = game.camera.y - dt * config.cameraSpeed end
if kd('s') or kd('down') then game.camera.y = game.camera.y + dt * config.cameraSpeed end
-- Move cursor
local wx, wy = game.camera:toWorld(love.mouse.getX(), love.mouse.getY())
local query = game.area:tileAt(iso.toOrtho(wx, wy))
if query then
game.cursor.tile = query
else
game.cursor.tile = nil
end
if love.mouse.isDown('r') then
if game.cursor.tile then
game.area:createVilliager(game.cursor.tile)
end
end
if love.mouse.isDown('l') then
if game.cursor.tile then
game.area:createWater(game.cursor.tile)
end
end
end
function game:update(dt)
-- Update globals
tween.update(dt)
-- Input
game:parseInput(dt)
-- Update area
game.area:update(dt)
end
function game:draw()
-- Backdrop
love.graphics.setColor(110, 179, 220)
love.graphics.rectangle('fill', 0, 0, love.graphics.getWidth(), love.graphics.getHeight())
-- Draw in camera
love.graphics.push()
game.camera:applyMatrix()
-- Draw in isometric view
love.graphics.push()
if config.iso then iso.applyMatrix() end
-- Draw area (tiles, )
game.area:draw()
-- Draw cursor
if game.cursor.tile then
color.white(50)
love.graphics.rectangle('fill', game.cursor.tile:getRect())
end
love.graphics.pop()
love.graphics.pop()
if config.debug then
console:draw()
color.white()
love.graphics.print("FPS: " .. love.timer.getFPS(), love.graphics.getWidth() - 75, 5)
end
end
-- Key bindings
game.keys = {
-- ['`'] = function()
-- if not console.active then
-- console.active = true
-- app:pushContext(console)
-- else
-- console.active = false
-- app:popContext()
-- end
-- end,
f1 = { "Toggle debug mode", function()
config.debug = not config.debug
end },
f2 = { "Toggle isometric mode", function()
config.iso = not config.iso
end },
f3 = { "Cycle camera zoom", function()
config.cameraScale = (config.cameraScale % 3) + 1
tween.stop(game.camera.tween)
game.camera.tween = tween(1, game.camera, {scale = config.cameraScale}, 'outQuad')
console:write("Camera zoom level " .. config.cameraScale)
end },
f10 = { "Flush animation timers", function()
console:write("Animation timers reset")
tween.resetAll()
end },
t = { "Randomize tile heights", function ()
console:write('Randomizing tile heights...')
local range = math.random(8, 16)
map2d(game.area.tiles, function(tile)
tile.z = 0
tween.stop(tile.tween)
tile.tween = tween(1, tile, { z = math.random(-range, range)}, 'inOutQuad')
end)
end },
r = { "Reset tile heights", function()
console:write('Tile heights set to zero')
map2d(game.area.tiles, function(tile)
tween.stop(tile.tween)
tile.z = 0
end)
end },
escape = { "Quit", function()
require 'os'
os.exit()
end },
}
function game:printKeys()
console:write('Key bindings...')
for k, v in pairs(game.keys) do
console:write(k:upper() .. ": " .. v[1])
end
end
function game:keypressed(key, unicode)
if game.keys[key] then
game.keys[key][2]()
return true
end
end
return game