Skip to content

Commit

Permalink
Update, stairs now exist, and message handler for stairs works. Nothi…
Browse files Browse the repository at this point in the history
…ng happens yet.
  • Loading branch information
pvcraven committed Feb 25, 2020
1 parent e2fc046 commit 0013993
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 8 deletions.
3 changes: 3 additions & 0 deletions source/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
SCREEN_HEIGHT = int(SPRITE_HEIGHT * MAP_HEIGHT * SPRITE_SCALE + STATUS_PANEL_HEIGHT)

WALL_CHAR = chr(219)
STAIRS_DOWN_CHAR = chr(25)
FOV_RADIUS = 10
DEATH_DELAY = 0.5

Expand All @@ -39,6 +40,7 @@
KEYMAP_SELECT_ITEM_0 = [arcade.key.KEY_0]
KEYMAP_USE_ITEM = [arcade.key.U]
KEYMAP_DROP_ITEM = [arcade.key.D]
KEYMAP_USE_STAIRS = [arcade.key.ENTER]

# Game States
NORMAL = 1
Expand All @@ -53,6 +55,7 @@
TILE_LIGHTNING_SCROLL = 5
TILE_FIREBALL_SCROLL = 6
TILE_HEALING_POTION = 7
TILE_STAIRS_DOWN = 8

colors = {
"dark_wall": (0, 0, 100, 255),
Expand Down
9 changes: 8 additions & 1 deletion source/entities/restore_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@
from entities.fireball_scroll import FireballScroll
from entities.orc import Orc
from entities.troll import Troll
from entities.stairs import Stairs
from entities.lightning_scroll import LightningScroll
from entities.entity import Entity


def restore_entity(entity_dict):
entity_name = list(entity_dict.keys())[0]

if entity_name == 'Potion':
if entity_name == 'Entity':
entity = Entity()
elif entity_name == 'Potion':
entity = Potion()
elif entity_name == 'FireballScroll':
entity = FireballScroll()
Expand All @@ -17,6 +22,8 @@ def restore_entity(entity_dict):
entity = Orc()
elif entity_name == 'Troll':
entity = Troll()
elif entity_name == 'Stairs':
entity = Stairs()
else:
raise ValueError(f"Error, don't know how to restore {entity_name}.")

Expand Down
31 changes: 31 additions & 0 deletions source/entities/stairs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import arcade
from entities.entity import Entity


class Stairs(Entity):
def __init__(self,
x: int = 0,
y: int = 0,
char: str = "X",
color=arcade.csscolor.WHITE,
visible_color=arcade.csscolor.WHITE,
not_visible_color=arcade.csscolor.WHITE,
name=None,
blocks=False,
fighter=None,
ai=None,
inventory=None,
item=None,
floor: int = 0):
super().__init__(x, y, char, color, visible_color, not_visible_color, name, blocks, fighter, ai, inventory, item)
self.floor = floor

def get_dict(self):
dict = super().get_dict()
dict['floor'] = self.floor
return dict

def restore_from_dict(self, result):
super().restore_from_dict(result)
self.floor = result['floor']

25 changes: 20 additions & 5 deletions source/game_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import Optional

from constants import *
from entities.stairs import Stairs
from entities.inventory import Inventory
from entities.entity import Entity
from procedural_generation.game_map import GameMap
Expand Down Expand Up @@ -117,11 +118,8 @@ def restore_from_dict(self, data):
self.player.restore_from_dict(player_dict['Entity'])

for entity_dict in data['dungeon']:
entity_name = list(entity_dict.keys())[0]
if entity_name == 'Entity':
entity = Entity()
entity.restore_from_dict(entity_dict[entity_name])
self.dungeon_sprites.append(entity)
entity = restore_entity(entity_dict)
self.dungeon_sprites.append(entity)

for entity_dict in data['entities']:
entity = restore_entity(entity_dict)
Expand Down Expand Up @@ -205,6 +203,18 @@ def dying(self, target: Entity):
]
return results

def use_stairs(self):
# Get all the entities at this location
entities = arcade.get_sprites_at_exact_point(
self.player.position, self.dungeon_sprites
)
# For each entity
for entity in entities:
if isinstance(entity, Stairs):
return [{"message": "You haven't learned how to take the stairs yet."}]

return [{"message": "There are no stairs here"}]

def pick_up(self):
"""
Handle a pick-up item entity request.
Expand Down Expand Up @@ -293,5 +303,10 @@ def process_action_queue(self, delta_time: float):
[{"message": f"You dropped the {item.name}."}]
)

if "use_stairs" in action:
result = self.use_stairs()
if result:
new_action_queue.extend(result)

# Reload the action queue with new items
self.action_queue = new_action_queue
3 changes: 3 additions & 0 deletions source/game_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@ def on_key_press(self, key: int, modifiers: int):
elif key == arcade.key.L:
self.load()

elif key in KEYMAP_USE_STAIRS:
self.game_engine.action_queue.extend([{"use_stairs": True}])

def on_key_release(self, key: int, modifiers: int):
"""Called when the user releases a key. """

Expand Down
8 changes: 7 additions & 1 deletion source/map_to_sprites.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from entities.potion import Potion
from entities.orc import Orc
from entities.troll import Troll

from entities.stairs import Stairs

def map_to_sprites(game_map: List[List[int]]) -> arcade.SpriteList[Entity]:

Expand All @@ -34,6 +34,12 @@ def map_to_sprites(game_map: List[List[int]]) -> arcade.SpriteList[Entity]:
sprite.block_sight = False
sprite.visible_color = colors["light_ground"]
sprite.not_visible_color = colors["dark_ground"]
elif game_map[x][y] == TILE_STAIRS_DOWN:
sprite = Stairs(x, y, STAIRS_DOWN_CHAR, arcade.csscolor.WHITE)
sprite.name = "Stairs Down"
sprite.block_sight = False
sprite.visible_color = colors["light_ground"]
sprite.not_visible_color = colors["dark_ground"]
elif game_map[x][y] == TILE_ORC:
sprite = Orc(x, y)
elif game_map[x][y] == TILE_TROLL:
Expand Down
9 changes: 8 additions & 1 deletion source/procedural_generation/game_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,10 @@ def place_entities(room, entities, max_monsters_per_room, max_items_per_room):


class GameMap:
def __init__(self, width: int, height: int):
def __init__(self, width: int, height: int, dungeon_level: int = 1):
self.map_width = width
self.map_height = height
self.dungeon_level = dungeon_level
self.tiles = [
[TILE_WALL for _ in range(self.map_height)] for _ in range(self.map_width)
]
Expand All @@ -84,6 +85,8 @@ def make_map(
):
rooms = []
num_rooms = 0
center_of_last_room_x = None
center_of_last_room_y = None

for r in range(MAX_ROOMS):
# random width and height
Expand Down Expand Up @@ -120,6 +123,8 @@ def make_map(

# center coordinates of previous room
(prev_x, prev_y) = rooms[num_rooms - 1].center()
center_of_last_room_x = new_x
center_of_last_room_y = new_y

# flip a coin (random number that is either 0 or 1)
if randint(0, 1) == 1:
Expand All @@ -139,6 +144,8 @@ def make_map(
rooms.append(new_room)
num_rooms += 1

self.tiles[center_of_last_room_x][center_of_last_room_y] = TILE_STAIRS_DOWN

def create_room(self, room):
# go through the tiles in the rectangle and make them passable
for x in range(room.x1 + 1, room.x2):
Expand Down

0 comments on commit 0013993

Please sign in to comment.