Refactored to stop passing game around.
[realism.git] / engine / tile.py
blob81d1d777ea3883cc3de52dfe76be4b37b071807e
1 # This file contains several imports inside functions, to avoid poluting the
2 # namespace, which is imported by tiles.py.
5 def get_map_specials(game_map):
6 from game_map import special_keys
7 for key in special_keys:
8 yield game_map.map_key[key][0]
10 def default_action(my_tile):
11 # Do nothing if the player didn't move.
12 if my_tile.pos[0] < 1 or not my_tile.passable():
13 return
15 game_map = my_tile.game_map
17 # Gain health and mana.
18 game_map.hp_mp_ticks()
20 # If there's any more "grace period" left after a battle, use some.
21 if game_map.delay_left > 0:
22 game_map.delay_left -= 1
23 return
25 # Otherwise, see if there's a random encounter.
26 from random import randint
27 from engine.battle import simple_battle
28 (chance, delay, minlvl, maxlvl, mincrits, maxcrits, crittypes, tileset) = \
29 get_map_specials(game_map)
31 if randint(1,100) < int(chance):
32 # Battle!
33 # Add the grace period.
34 game_map.delay_left = int(delay)
35 # Determine the level and number of monsters.
36 level = randint(int(minlvl),int(maxlvl))
37 crits = randint(int(mincrits),int(maxcrits))
38 # Start the battle.
39 simple_battle(level, crits)
41 def safe_action(my_tile):
42 # Do nothing if the player didn't move.
43 if my_tile.pos[0] < 1 or not my_tile.passable():
44 return
46 # Just gain health and mana.
47 my_tile.game_map.hp_mp_ticks()
49 def shop_action(my_tile):
50 # Enter the shop.
51 game.ui.map_mode(my_tile.shop)
53 def save_action(my_tile):
54 #TODO
55 pass
57 def no_action(my_tile):
58 # No action, so we do nothing. How zen of us.
59 pass
61 def can_climb(my_tile, climb_difficulty):
62 # TODO
63 return False
65 class tile(object):
66 def __init__(self, map_pos, tile_type, passable = True, action = default_action, floater = None, vars = {}, save = ()):
68 # Internal import to avoid polluting the main namespace, which gets exported
69 # to tiles.py.
70 from new import instancemethod as im
72 # Where this tile is.
73 (self.game_map, self.pos) = map_pos
75 # The type of tile. Wall, open, door, etc.
76 self.type = tile_type
78 # Function called to determine if the player can move to this tile.
79 # None indicates that the player was sent to another map, so we reset this
80 # one as appropriate.
81 if passable in (True, False, None):
82 method = lambda self: passable
83 elif type(passable) == int:
84 method = lambda self: can_climb(self, passable)
85 else:
86 method = passable
87 self.passable = im(method, self, tile)
89 # Function called when the player tries to move to this tile.
90 self.action = im(action, self, tile)
92 # A creature or object on this tile that may move.
93 self.floater = floater
95 # Vars are any miscellaneous variables that are needed for the tile.
96 self.__dict__.update(vars)
98 # Save contains the list of variable names that need to be saved.
99 # This list *must not* change.
100 self.save = save