forked from jpalladino84/Python-Roguelike-Framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload.py
More file actions
122 lines (102 loc) · 3.7 KB
/
load.py
File metadata and controls
122 lines (102 loc) · 3.7 KB
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
"""
Loads data from config files into the database
This needs to happen before the game fully loads.
"""
import json
from peewee import SqliteDatabase
from settings import DATABASE_NAME
from dungeon.config import LEVELS
from dungeon.models import Dungeon, DungeonObject, DungeonLevel
from item.models import Item
from item.components import Inventory
from character.models import Character
from character.components import CharacterClass
from character.config import PLAYER
TABLES = [Dungeon, DungeonObject, DungeonLevel, Item, Character, CharacterClass]
database = SqliteDatabase(DATABASE_NAME)
# TODO as a game gets bigger this could take longer... Should maybe insert a loading screen.
def load_game():
init_database()
load_levels()
load_player()
def init_database():
"""
Initialize the database with the necessary tables.
This is to be done once at the start of the game.
"""
database.connect()
database.create_tables(TABLES, safe=True)
def load_levels():
for level in LEVELS:
new_level = DungeonLevel(
level_id=level['id'],
level_name=level['name'],
level_desc=level['desc'],
max_room_size=level['max_room_size'],
min_room_size=level['min_room_size'],
max_rooms=level['max_rooms'],
is_final_level=level['is_final_level']
)
new_level.save()
# load any items for the level
for item in level['items']:
new_item = Item(
level_id=level['id'],
name=item['name'],
description=item['desc'],
ascii_char=item['ascii_char'],
inventory_list=item['inventory_list'],
category=item['category'],
stat_mod=item['stat_mod'],
operation=item['op'],
value=item['value'],
fgcolor=json.dumps(item['fgcolor']),
bgcolor=json.dumps(item['bgcolor'])
)
new_item.save()
load_monsters(level)
def load_monsters(level_config):
level = DungeonLevel.get(DungeonLevel.level_id == level_config['id'])
for monster in level_config['monsters']:
character_class = monster['character_class']
new_character_class = CharacterClass(
class_name=character_class['name'],
max_hp=character_class['max_hp'],
hp=character_class['hp'],
defense=character_class['defense'],
attack=character_class['attack'],
speed=character_class['speed'],
)
new_character_class.save()
new_monster = Character(
level=level,
name=monster['name'],
ascii_char=monster['ascii_char'],
fgcolor=json.dumps(monster['fgcolor']),
bgcolor=json.dumps(monster['bgcolor']),
character_state=monster['character_state'],
character_class=new_character_class,
inventory=Inventory()
)
new_monster.save()
def load_player():
character_class = PLAYER['character_class']
new_character_class = CharacterClass(
class_name=character_class['name'],
max_hp=character_class['max_hp'],
hp=character_class['hp'],
defense=character_class['defense'],
attack=character_class['attack'],
speed=character_class['speed'],
)
new_character_class.save()
new_player = Character(
name=PLAYER['name'],
ascii_char=PLAYER['ascii_char'],
fgcolor=json.dumps(PLAYER['fgcolor']),
bgcolor=json.dumps(PLAYER['bgcolor']),
character_state=PLAYER['character_state'],
character_class=new_character_class,
inventory=Inventory()
)
new_player.save()