This repository has been archived by the owner on Sep 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyTower.py
203 lines (162 loc) · 5.16 KB
/
pyTower.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
# -*- coding: utf-8 -*-
import pygame
import yaml
from os import walk
from multiprocessing import Queue # Should move this stuff to messages?
from Queue import Empty
from time import time
from pytower.constants import *
from pytower import messages
from pytower.menus import Menus
from pytower.window import Window
from pytower.game import Game
from pytower.object import Object
from pytower.map import Map
from pytower.utility import FullPath
from pytower import logic
def quit():
if None != menus:
menus.join()
pygame.quit()
exit()
def notify_ui():
tx.put_nowait( messages.Message( messages.NOTIFY_TIME, { 'time': game.clock } ) )
tx.put_nowait( messages.Message( messages.NOTIFY_CASH, { 'cash': game.cash } ) )
tx.put_nowait( messages.Message( messages.NOTIFY_POPULATION, { 'population': game.population } ) )
pygame.init()
window = Window()
window.init_loading()
window.set_loading( 'Loading Maps' )
maps = []
maps_min = []
for root, dirs, files in walk( 'maps/' ):
for file in files:
if file == 'map.yaml':
import_path = root.replace( '/', '.' ) + '.map'
f = open( root + '/' + file )
map_yaml = yaml.load( f )
f.close()
# TODO: Version checking
map = Map( map_yaml, import_path, FullPath( root ) )
maps.append( map )
maps_min.append( map.name )
window.set_loading( 'Spawning Menu' )
tx = Queue()
rx = Queue()
menus = Menus( 'qt', tx, rx )
tx.put_nowait( messages.Message( messages.MAPS, {'maps': maps_min } ) )
menus.main_menu()
game = Game()
window.set_loading( 'Ready!' )
while True:
# Only redraw if we have to...
for event in pygame.event.get():
if event.type == pygame.VIDEOEXPOSE:
window.update()
elif event.type == pygame.QUIT:
tx.put_nowait( messages.Message( messages.QUIT ) )
pygame.quit()
exit()
try:
msg = rx.get_nowait()
print "SDL RX:", msg
if messages.QUIT == msg.instruction:
quit()
elif messages.NEW_GAME == msg.instruction:
for i in maps:
if i.name == msg.map:
game.new_from_map( i )
break
del maps
del maps_min
break
except Empty:
pygame.time.delay( IPQUEUE_SLEEP )
window.set_loading( 'Loading Objects' )
objects = []
for root, dirs, files in walk( 'objects/' ):
for file in files:
if file == 'object.yaml':
import_path = root.replace( '/', '.' ) + '.object'
f = open( root + '/' + file )
object_yaml = yaml.load( f )
f.close()
# TODO: Version checking
objects.append( Object( object_yaml, import_path ) )
window.set_loading( 'Loading UI' )
tx.put_nowait( messages.Message( messages.OBJECTS, {'objects': objects } ) )
menus.in_game_menu()
window.floor_offset = game.map.floors - WINDOW_FLOORS - int( game.map.dirt_floors / 2 )
window.slice_offset = 0 #TODO: Fix this to a good value: int( game.map.slices / 2 ) ?
window.load_tile_set( game.map.get_tile_paths( window.floor_offset, game.clock ) )
frame_remains = TICK_REAL_TIME
paused = False
cursor_object = None
while True:
frame_start = time()
floor_offset = window.floor_offset
slice_offset = window.slice_offset
for event in pygame.event.get():
if event.type == pygame.QUIT:
tx.put_nowait( messages.Message( messages.QUIT ) )
quit()
elif event.type == pygame.MOUSEMOTION:
window.move_cursor( event.pos )
#elif event.type == pygame.MOUSEBUTTONUP:
#if event.button == 1:
## Snap to grid
#pos = ( int( event.pos[0] / SLICE_WIDTH ) * SLICE_WIDTH, int( event.pos[1] / FLOOR_HEIGHT ) * FLOOR_HEIGHT )
#floor = int( pos[1] / FLOOR_HEIGHT ) + window.floor_offset
#slice = int( pos[0] / SLICE_WIDTH ) + window.slice_offset
#if "floor" == cursor_object:
#if logic.addFloorSlice( pos, game ):
#window.refresh( floor, slice, game )
elif event.type == pygame.KEYUP:
if event.key == pygame.K_DOWN:
floor_offset = floor_offset + 1
elif event.key == pygame.K_UP:
floor_offset = floor_offset - 1
elif event.key == pygame.K_LEFT:
slice_offset = slice_offset - 1
elif event.key == pygame.K_RIGHT:
slice_offset = slice_offset + 1
# Check with the UI message queue
try:
msg = rx.get_nowait()
print msg
if messages.QUIT == msg.instruction:
quit()
elif messages.PAUSE == msg.instruction:
paused = True
elif messages.PLAY == msg.instruction:
paused = False
elif messages.SET_CURSOR == msg.instruction:
cursor_object = msg.object
window.set_cursor( msg.cursor )
except Empty:
pass
# Over-adjust corrections...
if slice_offset + WINDOW_SLICES > game.map.slices:
slice_offset = game.map.slices - WINDOW_SLICES
elif slice_offset < 0:
slice_offset = 0
if floor_offset + WINDOW_FLOORS > game.map.floors:
floor_offset = game.map.floors - WINDOW_FLOORS
elif floor_offset < 0:
floor_offset = 0
# Now choose the right rendering function
if floor_offset != window.floor_offset:
window.floor_offset = floor_offset
window.slice_offset = slice_offset
window.load_tile_set( game.map.get_tile_paths( window.floor_offset, game.clock ) )
elif slice_offset != window.slice_offset:
window.slice_offset = slice_offset
window.refresh_background()
if not paused:
frame_end = time()
frame_remains = frame_remains - ( frame_end - frame_start )
if 0 >= frame_remains:
game.clock_tick()
notify_ui()
frame_remains = TICK_REAL_TIME
window.update()