-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
295 lines (223 loc) · 7.99 KB
/
main.py
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
#!/usr/bin/python2
# coding: utf8
from bearlibterminal import terminal as blt
#import libtcodpy as libtcod
from time import time
import game
# save/load
import game_loaders
import constants
import tileset
import renderer
import hud
import main_menu
import game_vars
import handle_input
from game_states import GameStates
def cell_to_iso(x,y):
offset_x = constants.MAP_WIDTH * 4
iso_x = y / constants.TILE_HEIGHT + (x - offset_x) / constants.TILE_WIDTH
iso_y = y / constants.TILE_HEIGHT - (x - offset_x) / constants.TILE_WIDTH - CAMERA.offset[1]
return iso_x, iso_y
# main loop
def game_main_loop():
"""
Main loop:
1. fps
2. mouse
3. draw (including things dependent on mouse position)
* redraw hud always
* redraw map only if told to (basically only if moved)
4. handle input
5. process turns
"""
game_quit = False
fps_update_time = time()
fps_counter = fps_value = 0
while not game_quit:
#clear
#blt.clear()
# hud layer
blt.layer(1)
blt.clear_area(0, 0, blt.state(blt.TK_WIDTH), blt.state(blt.TK_HEIGHT))
# used by menus
blt.layer(3)
blt.clear_area(0, 0, blt.state(blt.TK_WIDTH), blt.state(blt.TK_HEIGHT))
#blt.layer(4)
#blt.clear_area(0,0, blt.state(blt.TK_WIDTH, blt.state(blt.TK_HEIGHT)))
#blt.layer(5)
#blt.clear_area(0,0,blt.state(blt.TK_WIDTH), blt.state(blt.TK_HEIGHT))
blt.layer(0)
if not game_vars.game_state == GameStates.MAIN_MENU:
blt.layer(1)
blt.puts(2,1, "[color=white]FPS: %d ms %.3f" % (fps_value, 1000/(fps_value * 1.0) if fps_value else 0) )
# debug
blt.puts(2,2, "Redraw: %s" % str(game_vars.redraw))
#mouse
pix_x, pix_y, _, _ = game_handle_mouse()
# camera
CAMERA.update()
# draw
if game_vars.redraw:
blt.layer(0)
blt.clear_area(0,0, blt.state(blt.TK_WIDTH), blt.state(blt.TK_HEIGHT))
blt.layer(2)
blt.clear_area(0,0,blt.state(blt.TK_WIDTH), blt.state(blt.TK_HEIGHT))
#if game_vars.labels:
# redraw this layer since it has effects now
blt.layer(4)
blt.clear_area(0, 0, blt.state(blt.TK_WIDTH, blt.state(blt.TK_HEIGHT)))
renderer.draw_game()
# on top of map
blt.layer(1)
renderer.draw_messages(game_vars.message_history)
blt.layer(1)
renderer.draw_mouseover(pix_x, pix_y)
blt.color(4294967295)
hud.draw_hud(pix_x, pix_y)
# effects
for ef in game_vars.level.current_effects:
ef.update()
if not ef.render:
game_vars.level.current_effects.remove(ef)
# refresh term
blt.refresh()
if not game_vars.game_state == GameStates.MAIN_MENU:
# fps
fps_counter += 1
tm = time()
if tm > fps_update_time + 1:
fps_value = fps_counter
fps_counter = 0
fps_update_time = tm
# avoid blocking the game with blt.read
key = None
if blt.has_input():
key = blt.read()
player_action = handle_input.game_handle_keys(key)
if handle_input.get_fake_action() is not None:
print("Faking an action")
player_action = handle_input.get_fake_action()
handle_input.reset_fake_action()
#print player_action
if player_action == "QUIT":
game_quit = True
break
else:
GAME.map_calculate_fov()
if player_action == "mouse_click":
print "Click"
if player_action is not None and player_action not in ["no-action", "mouse_click", "redraw"]:
game_vars.redraw = True
# print("Advancing time")
# advance time
game_vars.calendar_game.turn += 1
#toggle game state to enemy turn
game_vars.game_state = GameStates.ENEMY_TURN
elif player_action == "redraw":
game_vars.redraw = True
else:
game_vars.redraw = False
# enemy turn
if game_vars.game_state == GameStates.ENEMY_TURN:
for ent in game_vars.level.current_entities:
if ent.ai:
ent.ai.take_turn(game_vars.player, game_vars.ai_fov_map)
if game_vars.game_state == GameStates.PLAYER_DEAD:
print("Player's dead, breaking the loop")
break
if not game_vars.game_state == GameStates.PLAYER_DEAD:
game_vars.game_state = GameStates.PLAYER_TURN
# resting (potentially other stuff)
game_vars.player.creature.player.act()
# test passage of time
#print(GAME.calendar.get_time_date(GAME.calendar.turn))
if game_vars.game_state == GameStates.PLAYER_DEAD:
# force redraw
game_vars.redraw = True
#print("PLAYER DEAD")
#if GAME.game_state == GameStates.PLAYER_TURN:
# print("PLAYER TURN")
#save if not dead
if not game_vars.game_state == GameStates.PLAYER_DEAD and not game_vars.game_state == GameStates.MAIN_MENU:
#print(str(GAME.game_state) + " we should save game")
game_loaders.save_game(GAME, CAMERA, game_vars.player)
# quit the game
blt.close()
# mouse movement
def game_handle_mouse():
# values
m_x = blt.state(blt.TK_MOUSE_X)
m_y = blt.state(blt.TK_MOUSE_Y)
pix_x = blt.state(blt.TK_MOUSE_PIXEL_X)
pix_y = blt.state(blt.TK_MOUSE_PIXEL_Y)
# mouse test
blt.layer(1)
blt.puts(
3, 7,
"Cursor: [color=orange]%d:%d[/color] [color=dark gray]cells[/color]"
", [color=orange]%d:%d[/color] [color=dark gray]pixels[/color]" % (
m_x,
m_y,
pix_x,
pix_y))
# map tile picking
# fake an offset of camera offset * cell width
pix_x = pix_x - CAMERA.offset[0] * blt.state(blt.TK_CELL_WIDTH)
# fake an offset of camera offset * cell height
pix_y = pix_y - CAMERA.offset[1] * blt.state(blt.TK_CELL_HEIGHT)
blt.puts(2, 3, "[color=red] iso coords based on pixels: %d %d" % (renderer.pix_to_iso(pix_x, pix_y)))
blt.layer(0)
return pix_x, pix_y, m_x, m_y
def mouse_picking(m_x, m_y):
# log_h = blt.state(blt.TK_HEIGHT) - (constants.NUM_MESSAGES)
# mouse picking test
w = 4
h = 9
n = 0
while True:
# detect mousing over message log
#if m_x < 40 and m_y >= log_h:
# break
code = blt.pick(m_x, m_y, n)
if code == 0: break
blt.layer(1)
blt.puts(w + n * 2, h, u"%c" % (code))
blt.layer(0)
n += 1
#
if n == 0:
blt.puts(w, h, "Empty cell")
def game_initialize():
global GAME, PLAYER, CAMERA
blt.open()
# default terminal size is 80x25
blt.set("window: size=160x45, cellsize=8x16, title='Veins of the Earth'; font: default")
# use the full version of Fixedsys Excelsior because we need arrows
blt.set("font:font/FSEX300.ttf, size=8x16")
#vsync
blt.set("output.vsync=true")
# mouse
blt.set("input.filter={keyboard, mouse+}")
blt.set("input: precise-mouse=true, mouse-cursor=true")
blt.composition(True)
# needed to avoid insta-close
blt.refresh()
# menu background
blt.set("0xE100: gfx/Veins.png")
# tiles
tileset.set_tiles()
# main menu
GAME = game.obj_Game(True)
game_vars.game_state = GameStates.MAIN_MENU
ret = main_menu.main_menu_outer()
if ret is not False and ret is not None:
GAME, PLAYER, CAMERA = ret[0], ret[1], ret[2]
else:
# quit
blt.close()
# fix issue where the map is black on turn 1
GAME.map_calculate_fov()
if __name__ == '__main__':
game_initialize()
game_main_loop()