forked from TheGreatestZenMaster/UnsuiPythonGame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameInstance.py
262 lines (215 loc) · 9.34 KB
/
GameInstance.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
'''
This class defines the object which holds all game information.
Think of it like a giant game container in which everything goes.
'''
import sys
from items.Key import Key
from items.Door import Door
from rooms.Room import Room
from Player.Player import Player
from monsters.Wolf import Wolf
from config import UnsuiConfigLoader
import user_input
BASE_ACTIONS = ["look", "exit", "stats", "location", "help", "go"] # these are the actions which should always be available.
class GameInstance(object):
def __init__(self):
self.actions_available = BASE_ACTIONS
self.player = Player("NoName", "Male", "Human", None)
self.config_loader = UnsuiConfigLoader()
self.config_loader.generate()
#------- Actions Functions --------#
def generate_rooms_dict(self):
''' this returns a list of all rooms in the area '''
return self.config_loader.get_by_type('room')
def take_action(self,action,input=user_input.default_input):
"""
This function takes an action specified by a string
and completes that action.
"""
# NOTE: currently there is no check to ensure that each action is available
# TODO: check to see if action is available before trying... like:
# if action in self.actions_available:
# === Base Actions: ===
room_dict = self.generate_rooms_dict()
if action == "exit":
sys.exit()
elif action == "look":
print self.player.current_location.description
elif action == "enter":
raise NotImplementedError('action_main call needs to be fixed') #action_main()
elif action == "go":
print self.player.current_location.exits
travel_location = input("Which Room?")
try:
self.player.current_location = self.config_loader.get_by_type_and_name('room', self.player.current_location.exits[int(travel_location)-1])
except ValueError:
try:
self.player.current_location = self.config_loader.get_by_type_and_name('room', travel_location)
except ValueError:
print 'Place not recognized.'
elif action == "stats":
self.player.player_status()
return True
elif action == "help":
user_input.help_info()
return True
elif action == "location":
self.player.player_location()
return True
# === iteminteractions ===
elif action == "grab":
self.player.inventory.add_item(user_input.choose_object(self.keys))
# === monster interactions
elif action == "fight":
opponent_engine()
return True
else:
print "That's not a valid command!!!"
"""
# NOTE: code below here is out-of-date and needs to be updated for use here. ~7yl4r
# ------- Status Functions ------#
# These functions are there to display the info about the room/player on request
def visible_keys():
dict_of_room_keys = generate_keys_dict()
count = 0
for key in dict_of_room_keys:
if key.room == player.current_location:
count += 1
if count == 0:
print "There are no keys"
else:
print "You can see %r keys." % count
def visible_doors():
if player.current_location == "Hallway":
count = len(list_of_doors)
print "You can see %r doors numbered accordingly." % count
else:
print "You can see the door you came through"
def door_status(door):
if door.locked:
print "Door number %r is locked!" % door.number
elif not door.locked:
print "Door number %r is unlocked!" % door.number
def visible_monsters():
dict_of_room_monsters = populate_room_monster_dict()
for monster in dict_of_room_monsters:
print "You can see a %r!" % monster.name
def battle_engine(monster):
while True:
Wolf.attack(monster)
Player.attack(player, monster)
def opponent_engine():
dict_of_room_monsters = populate_room_monster_dict()
for monster in dict_of_room_monsters:
battle_engine(monster)
return False
#------- Room Populating function ----#
def generate_rooms_dict(self):
return game.config_loader.get_by_type('room')
def generate_doors_dict():
room_dict = generate_rooms_dict()
dict_of_doors_in_room = {}
return game.player.current_location.exits
if game.player.current_location.name == "Hallway":
for door in game.doors:
dict_of_doors_in_room[door] = door.name
return dict_of_doors_in_room
else:
room = game.player.current_location
for i in room_dict:
if i.name == room:
room_number = i.number
for door in list_of_doors:
if room_number == door.number:
door_name = door.name
dict_of_doors_in_room[door] = door_name
return dict_of_doors_in_room
def generate_keys_dict():
dict_of_keys = {}
for key in game.keys:
dict_of_keys[key] = key.name
return dict_of_keys
def generate_room_keys_dict():
dict_of_keys = generate_keys_dict()
dict_of_room_keys = {}
for key in dict_of_keys:
if key.room == player.current_location:
dict_of_room_keys[key] = key.name
return dict_of_room_keys
def generate_monsters_dict():
dict_of_monsters = {}
for monster in list_of_monsters:
dict_of_monsters[monster] = monster.name
return dict_of_monsters
def populate_room_monster_dict():
dict_of_room_monsters = {}
dict_of_monsters = generate_monsters_dict()
for monster in dict_of_monsters:
if monster.room == player.current_location:
dict_of_room_monsters[monster] = monster.name
return dict_of_room_monsters
#-------- Player Functions --------#
def xp_check():
global player
xp_needed = player.level * 10 + 15
if player.xp >= xp_needed:
player.level_up()
player.xp -= xp_needed
print "You have leveled up!"
else:
xp_needed = player.level * 10 + 15
print "Not enough xp to level up!", "You need %r" % xp_needed, "You have %r" % player.xp
def prompt_levelup():
check_levelup = raw_input("Would you like to see if you leveled up?")
if check_levelup == "yes":
xp_check()
#------ Main Loops ---------- #
def hall_room_transition():
print game.player.current_location.exits
room_choice = raw_input("Which room would you like to enter?(please enter a number)")
if not room_choice.isdigit():
print "Nope, it's not a number you entered."
game.player.move_room(game.config_loader.get_by_type_and_name('room', game.player.current_location.exits[int(room_choice)-1]))
# Locked doors are to be reimplemented soon. For now I'm testing the configuration loader functionality
dict_of_doors = generate_doors_dict()
room_dict = generate_rooms_dict()
for i in dict_of_doors:
if i.number == int(room_choice):
door_choice = i
door_number = i.number
if door_choice.locked:
print "The door seems to be locked. Maybe you need to use a key!"
use_key = raw_input("Use a key? (yes or no)")
if use_key.lower() == "yes":
for x in game.player.inventory:
if x.match == door_choice.match:
door_choice.locked = False
print "You used the key!"
door_status(door_choice)
enter_through_door = raw_input("Enter the room?")
if enter_through_door == "yes":
print "You entered the room!"
for room in room_dict:
if room.number == door_number:
player.current_location = room.name
break
else:
print "There are no matching keys in your inventory!"
break
elif use_key.lower() == "no":
print "OK, but the door is still locked!"
elif not door_choice.locked:
print "You entered the room!"
for room in room_dict:
if room.number == door_number:
game.player.move_room(room)
def action_main():
while True:
if game.player.current_location.name == "Hallway":
hall_room_transition()
break
else:
generate_room_keys_dict()
game_engine()
return False
"""