Skip to content

Commit

Permalink
Update v0.8.1
Browse files Browse the repository at this point in the history
Cleanup code in the GameWorld() class, the RenderProcessor and its subprocessors.
  • Loading branch information
Shoes01 committed Apr 27, 2019
1 parent caf1d8c commit 3c4958f
Show file tree
Hide file tree
Showing 15 changed files with 119 additions and 113 deletions.
2 changes: 1 addition & 1 deletion engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def main():
world = build_world()
world.consoles = consoles

while not world.state == 'Exit':
while world.state:
# Do literally everything.
world.process()

Expand Down
35 changes: 19 additions & 16 deletions game.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,25 @@ class GameWorld(world.CustomWorld):
def __init__(self):
super().__init__()
' Flags. '
self.create_dijkstra_map = False
self.generate_map = False
self.pop_state = False
self.reset_game = False
self.victory = False
self.view_log = False
self.flag_create_dijkstra_map = False
self.flag_generate_map = False
self.flag_pop_state = False
self.flag_reset_game = False
self.flag_victory = False
self.flag_view_log = False

' Data. '
self.debug_mode = False # This one is a toggle.
self.events = []
self.key = None
self.messages = []
self.messages_offset = 0
self.mouse_pos = None
self.popup_menus = []
self.redraw = False # This information needs to communicate cross-tick.
self.skill_targeting = False # This one is a toggle.
self.state_stack = ['Exit', 'MainMenu']
self.state_stack = ['MainMenu']
self.ticker = 0
self.toggle_debug_mode = False
self.toggle_skill_targeting = False

' Objects. '
self.consoles = None
Expand All @@ -35,7 +35,10 @@ def __init__(self):

@property
def state(self):
return self.state_stack[-1]
try:
return self.state_stack[-1]
except:
return None

@property
def turn(self):
Expand Down Expand Up @@ -70,12 +73,12 @@ def save_game(self):
data_file['next_entity_id'] = self._next_entity_id

def reset_flags(self):
self.create_dijkstra_map = False
self.generate_map = False
self.pop_state = False
self.reset_game = False
self.victory = False
self.view_log = False
self.flag_create_dijkstra_map = False
self.flag_generate_map = False
self.flag_pop_state = False
self.flag_reset_game = False
self.flag_victory = False
self.flag_view_log = False

class Cursor():
def __init__(self):
Expand Down
4 changes: 2 additions & 2 deletions processors/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ def process(self):
r = math.sqrt( dx**2 + dy**2)

self.world.add_component(ent, VelocityComponent(dx=round(dx/r), dy=round(dy/r)))
self.world.create_dijkstra_map = True
self.world.flag_create_dijkstra_map = True

elif _move:
dx, dy = _move
self.world.add_component(ent, VelocityComponent(dx=dx, dy=dy))
self.world.create_dijkstra_map = True
self.world.flag_create_dijkstra_map = True

elif _pick_up:
if _pick_up is True:
Expand Down
2 changes: 1 addition & 1 deletion processors/debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def __init__(self):
super().__init__()

def process(self):
if self.world.debug_mode and self.world.state == 'Game':
if self.world.toggle_debug_mode and self.world.state == 'Game':
dijkstra_map = self.world.map.dijkstra_map
key = self.world.key
key_char = None
Expand Down
4 changes: 2 additions & 2 deletions processors/dijkstra.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def __init__(self):
super().__init__()

def process(self):
if self.world.create_dijkstra_map:
if self.world.flag_create_dijkstra_map:
game_map = self.world.map
player_pos = self.world.component_for_entity(1, PositionComponent)

Expand Down Expand Up @@ -46,4 +46,4 @@ def process(self):
game_map.dijkstra_map = dijkstra_map
game_map.directory = directory

self.world.create_dijkstra_map = False
self.world.flag_create_dijkstra_map = False
20 changes: 10 additions & 10 deletions processors/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ def process(self):
_view_log = event.get('view_log')

if _boss_killed:
self.world.victory = True
self.world.flag_victory = True

elif _close_popup_menu:
menus = self.world.popup_menus
while len(menus):
menus.pop()

elif _exit:
self.world.pop_state = True
self.world.flag_pop_state = True

elif _key_stroke:
key = _key_stroke
Expand Down Expand Up @@ -71,8 +71,8 @@ def process(self):
self.world.cursor.y += dy

elif _new_map:
self.world.generate_map = True
self.world.create_dijkstra_map = True
self.world.flag_generate_map = True
self.world.flag_create_dijkstra_map = True

elif _player_killed:
self.world.component_for_entity(1, PlayerComponent).killed = True
Expand All @@ -91,16 +91,16 @@ def process(self):
self.world.messages_offset += _scroll

elif _skill_done:
self.world.skill_targeting = False
self.world.toggle_skill_targeting = False

elif _skill_targeting:
self.world.skill_targeting = True
self.world.toggle_skill_targeting = True

elif _toggle_debug:
if self.world.debug_mode:
self.world.debug_mode = False
if self.world.toggle_debug_mode:
self.world.toggle_debug_mode = False
else:
self.world.debug_mode = True
self.world.toggle_debug_mode = True

elif _view_log:
self.world.view_log = True
self.world.flag_view_log = True
2 changes: 1 addition & 1 deletion processors/final.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def __init__(self):
super().__init__()

def process(self):
if self.world._entities and self.world.reset_game:
if self.world._entities and self.world.flag_reset_game:
self.world.clear_database()

self.world.reset_flags()
2 changes: 1 addition & 1 deletion processors/mapgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def __init__(self):
self._leaf_rooms = []

def process(self):
if self.world.generate_map:
if self.world.flag_generate_map:
game_map = self.world.map
game_map.floor += 1

Expand Down
83 changes: 37 additions & 46 deletions processors/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,62 +13,53 @@ def __init__(self):
super().__init__()

def process(self):
game_state = self.world.state

# Draw the border while Debug mode is active.
if game_state == 'Game' and (self.world.redraw is False or self.world.debug_mode):
self.render_border()
return 0
else:
self.world.redraw = False
self.render_border()

con_obj = self.world.consoles['con'] # type: (console, x, y, w, h)
eqp_obj = self.world.consoles['stats']
log_obj = self.world.consoles['log']
map_obj = self.world.consoles['map']
if self.world.redraw and not self.world.toggle_debug_mode:
self.world.redraw = False
else:
return 0

# Draw pretty much all game elements.
render_stats(self.world)
render_entities(self.world)
render_popup_menu(self.world)
render_message_log(self.world)
render_tooltips(self.world)

# Draw the gameover overlay.
if self.world.state == 'GameOver':
libtcod.console_set_color_control(libtcod.COLCTRL_1, COLOR_THEME['BrightRed'], COLOR_THEME['Red'])
self.world.consoles['map'][0].print(3, 3, 'You have %cDIED%c! Press ESC to return to the Main Menu.' % (libtcod.COLCTRL_1, libtcod.COLCTRL_STOP), UI_COLORS['text_mainmenu'], bg_blend=libtcod.BKGND_NONE)

# Draw the main menu.
if game_state == 'MainMenu':
elif self.world.state == 'MainMenu':
_string = 'Welcome to the Main Menu.\n\nPress ENTER to begin.\nPress L to load the last save.\n\nPress ESC to quit.'
map_obj[0].print(3, 3, _string, UI_COLORS['text_mainmenu'])
self.world.consoles['map'][0].print(3, 3, _string, UI_COLORS['text_mainmenu'])

# Draw the victory screen
if game_state == 'VictoryScreen':
# Draw the flag_victory screen
elif self.world.state == 'VictoryScreen':
_string = 'You have won! Press ESC to return to the Main Menu.'
map_obj[0].print(3, 3, _string, UI_COLORS['text_mainmenu'])

# Draw the game.
if game_state == 'Game' or game_state == 'GameOver' or game_state == 'Look' or game_state == 'PopupMenu' or game_state == 'ViewLog' or game_state == 'SkillTargeting':
self.render_border()
render_stats(self.world.consoles['stats'], self.world)
render_entities(self.world.consoles['map'], self.world)
render_popup_menu(self.world.consoles['map'], self.world)
render_message_log(self.world.consoles['log'], self.world)
render_tooltips(self.world.consoles['map'], self.world)
self.world.consoles['map'][0].print(3, 3, _string, UI_COLORS['text_mainmenu'])

# Draw the gameover overlay.
if game_state == 'GameOver':
libtcod.console_set_color_control(libtcod.COLCTRL_1, COLOR_THEME['BrightRed'], COLOR_THEME['Red'])
map_obj[0].print(3, 3, 'You have %cDIED%c! Press ESC to return to the Main Menu.' % (libtcod.COLCTRL_1, libtcod.COLCTRL_STOP), UI_COLORS['text_mainmenu'], bg_blend=libtcod.BKGND_NONE)

# Draw the message log overlay.
if game_state == 'ViewLog':
map_obj[0].clear()
log_obj[0].clear()
render_message_log(self.world.consoles['map'], self.world)
for key, value in self.world.consoles.items():
### Is the console blitting to itself?
# key: console name
# value: console, x, y, w, h
if key == 'con':
continue

value[0].blit(dest=self.world.consoles['con'][0], dest_x=value[1], dest_y=value[2], width=value[3], height=value[4])

eqp_obj[0].blit(dest=con_obj[0], dest_x=eqp_obj[1], dest_y=eqp_obj[2], width=eqp_obj[3], height=eqp_obj[4])
log_obj[0].blit(dest=con_obj[0], dest_x=log_obj[1], dest_y=log_obj[2], width=log_obj[3], height=log_obj[4])
map_obj[0].blit(dest=con_obj[0], dest_x=map_obj[1], dest_y=map_obj[2], width=map_obj[3], height=map_obj[4])

libtcod.console_flush()

con_obj[0].clear()
eqp_obj[0].clear()
log_obj[0].clear()
map_obj[0].clear()


for key, value in self.world.consoles.items():
value[0].clear()

def render_border(self):
if self.world.state == 'MainMenu':
return 0

con_obj = self.world.consoles['con'] # type: (console, x, y, w, h)
eqp_obj = self.world.consoles['stats']
map_obj = self.world.consoles['map']
Expand Down
30 changes: 15 additions & 15 deletions processors/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,56 +12,56 @@ def process(self):
pass

if self.world.state == 'Game':
if self.world.pop_state:
if self.world.flag_pop_state:
self.world.state_stack.pop()
self.world.reset_game = True
self.world.flag_reset_game = True
elif self.world.component_for_entity(1, PlayerComponent).killed:
self.world.remove_component(1, PlayerComponent)
self.world.state_stack.append('GameOver')
elif self.world.cursor.active:
self.world.state_stack.append('Look')
elif self.world.popup_menus:
self.world.state_stack.append('PopupMenu')
elif self.world.skill_targeting:
elif self.world.toggle_skill_targeting:
self.world.state_stack.append('SkillTargeting')
elif self.world.victory:
elif self.world.flag_victory:
self.world.state_stack.append('VictoryScreen')
elif self.world.view_log:
elif self.world.flag_view_log:
self.world.state_stack.append('ViewLog')

elif self.world.state == 'GameOver':
if self.world.pop_state:
if self.world.flag_pop_state:
self.world.state_stack.pop() # Pop to Game
self.world.state_stack.pop() # Pop to MainMenu
self.world.reset_game = True
self.world.flag_reset_game = True

elif self.world.state == 'Look':
if self.world.pop_state:
if self.world.flag_pop_state:
self.world.state_stack.pop()
self.world.cursor.active = False

elif self.world.state == 'MainMenu':
if self.world.generate_map:
if self.world.flag_generate_map:
self.world.state_stack.append('Game')
elif self.world.pop_state:
elif self.world.flag_pop_state:
self.world.state_stack.pop()

elif self.world.state == 'PopupMenu':
if not self.world.popup_menus:
self.world.state_stack.pop()
if self.world.pop_state:
if self.world.flag_pop_state:
self.world.state_stack.pop()

elif self.world.state == 'SkillTargeting':
if not self.world.skill_targeting:
if not self.world.toggle_skill_targeting:
self.world.state_stack.pop()

elif self.world.state == 'VictoryScreen':
if self.world.pop_state:
if self.world.flag_pop_state:
self.world.state_stack.pop() # Pop to Game
self.world.state_stack.pop() # Pop to MainMenu
self.world.reset_game = True
self.world.flag_reset_game = True

elif self.world.state == 'ViewLog':
if self.world.pop_state:
if self.world.flag_pop_state:
self.world.state_stack.pop()
16 changes: 10 additions & 6 deletions processors/sub/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@
from components.stairs import StairsComponent
from components.tile import TileComponent

def render_entities(console_bundle, world):
def render_entities(world):
if world.state == 'MainMenu' or world.state == 'PopupMenu':
return 0

prerender_entities(world)
_entity_directory = []
_ducpliates = []

console, x, y, w, h = console_bundle
console, x, y, w, h = world.consoles['map']

# Print tiles to the console.
for ent, (pos, ren, tile) in world.get_components(PositionComponent, RenderComponent, TileComponent):

Expand Down Expand Up @@ -75,14 +79,14 @@ def render_entities(console_bundle, world):
console.print(cursor.x, cursor.y, cursor.char, cursor.color)

def prerender_entities(world):
game_map = world.map
fov_map = world.map.fov_map

if game_map.fov_map:
if fov_map:
pos_player = world.component_for_entity(1, PositionComponent)
game_map.fov_map.compute_fov(x=pos_player.x, y=pos_player.y, radius=10, light_walls=True, algorithm=0)
fov_map.compute_fov(x=pos_player.x, y=pos_player.y, radius=10, light_walls=True, algorithm=0)

for ent, (pos, ren) in world.get_components(PositionComponent, RenderComponent):
if game_map.fov_map.fov[pos.x, pos.y]:
if fov_map.fov[pos.x, pos.y]:
ren.explored = True
ren.visible = True
else:
Expand Down
Loading

0 comments on commit 3c4958f

Please sign in to comment.