-
Notifications
You must be signed in to change notification settings - Fork 3
/
loader_functions.py
154 lines (123 loc) · 4.62 KB
/
loader_functions.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
import json
import os
from typing import List
from bearlibterminal import terminal
from components.equipment import Equipment
from components.equippable import Equippable
from components.fighter import Fighter
from components.inventory import Inventory
from entity import Entity
from equipment_slots import EquipmentSlots
from game_map import GameMap
from game_messages import MessageLog
from game_states import GameStates
from render_functions import RenderOrder
def get_constants():
window_title: str = 'Bearlibterm/TCOD Roguelike'
screen_width: int = 120
screen_height: int = 45
map_width: int = 80
map_height: int = 40
room_max_size: int = 10
room_min_size: int = 6
max_rooms: int = 30
fov_algorithm: int = 0
fov_light_walls: bool = True
fov_radius: int = 10
max_monsters_per_room: int = 3
max_items_per_room: int = 2
message_log_location_x: int = 0
message_log_location_y: int = map_height
message_log_width: int = screen_width
message_log_height: int = 5
colors = {
'dark_wall': terminal.color_from_argb(0, 0, 0, 100),
'dark_ground': terminal.color_from_argb(0, 50, 50, 150),
'light_wall': terminal.color_from_argb(0, 255, 255, 255),
'light_ground': terminal.color_from_argb(0, 255, 255, 255)
}
constants = {
'colors': colors,
'fov_algorithm': fov_algorithm,
'fov_light_walls': fov_light_walls,
'fov_radius': fov_radius,
'map_height': map_height,
'map_width': map_width,
'max_items_per_room': max_items_per_room,
'max_monsters_per_room': max_monsters_per_room,
'max_rooms': max_rooms,
'message_log_height': message_log_height,
'message_log_location_x': message_log_location_x,
'message_log_location_y': message_log_location_y,
'message_log_width': message_log_width,
'room_max_size': room_max_size,
'room_min_size': room_min_size,
'screen_height': screen_height,
'screen_width': screen_width,
'window_title': window_title,
}
return constants
def get_game_variables(constants):
equipment_component = Equipment()
fighter_component: Fighter = Fighter(hp=30, base_defense=2, base_power=3)
inventory_component: Inventory = Inventory(capacity=26)
player: Entity = Entity(
x=0,
y=0,
char='@',
color=terminal.color_from_argb(0, 255, 255, 255),
name='Player',
blocks=True,
render_order=RenderOrder.ACTOR,
equipment=equipment_component,
fighter=fighter_component,
inventory=inventory_component
)
entities = [player]
equippable_component = Equippable(slot=EquipmentSlots.MAIN_HAND, power_bonus=2)
dagger = Entity(
x=0,
y=0,
char='-',
color=terminal.color_from_argb(0, 153, 204, 255),
name='Dagger',
equippable=equippable_component
)
player.inventory.add_item(dagger)
player.equipment.toggle_equip(dagger)
game_map: GameMap = GameMap(width=constants['map_width'], height=constants['map_height'])
game_map.current_floor.make_map(
player=player,
entities=entities,
constants=constants
)
message_log: MessageLog = MessageLog()
game_state: GameStates = GameStates.PLAYERS_TURN
return player, entities, game_map, message_log, game_state
def load_game():
if not os.path.isfile('save_game.json'):
raise FileNotFoundError
with open('save_game.json') as save_file:
json_data = json.load(save_file)
entities = [Entity.from_json(json_data=entity_json_data) for entity_json_data in json_data['entities']]
player = entities[json_data['player_index']]
game_map = GameMap.from_json(json_data['game_map'])
message_log = MessageLog.from_json(json_data['message_log'])
game_state = GameStates(json_data['game_state'])
return player, entities, game_map, message_log, game_state
def save_game(player: Entity, entities: List[Entity], game_map: GameMap, message_log: MessageLog,
game_state: GameStates):
player_index = entities.index(player)
entities_json_data = [entity.to_json() for entity in entities]
game_map_json_data = game_map.to_json()
message_log_json_data = message_log.to_json()
game_state_json_data = game_state.value
json_data = {
'player_index': player_index,
'entities': entities_json_data,
'game_map': game_map_json_data,
'message_log': message_log_json_data,
'game_state': game_state_json_data
}
with open('save_game.json', 'w') as save_file:
json.dump(json_data, save_file, indent=4)