88from fantasy_forge .gateway import Gateway
99from fantasy_forge .inventory import Inventory , InventoryFull , InventoryTooSmall
1010from fantasy_forge .item import Item
11- from fantasy_forge .messages import Messages
1211from fantasy_forge .shell import Shell
1312from fantasy_forge .weapon import Weapon
1413from fantasy_forge .world import World
@@ -23,23 +22,25 @@ class Player(Character):
2322 shell : Shell
2423 seen_entities : dict [str , Entity ]
2524 armour_slots : dict [str , Armour ]
25+ world : World
2626
2727 def __init__ (
2828 self : Self ,
29- messages : Messages ,
29+ world : World ,
3030 name : str ,
3131 description : int ,
3232 health : int = BASE_PLAYER_HEALTH ,
3333 ):
3434 super ().__init__ (
35- messages ,
35+ world . messages ,
3636 dict (
3737 name = name ,
3838 description = description ,
3939 health = health ,
4040 ),
4141 )
42- self .area = Area .empty (messages )
42+ self .world = world
43+ self .area = Area .empty (world .messages )
4344 # put us in the void
4445 # We will (hopefully) never see this, but it's important for the
4546 # transition to the next area.
@@ -370,14 +371,39 @@ def drop(self, item_name: str):
370371 item = item_name ,
371372 )
372373
373- def main_loop (self , world : World , stdin = None , stdout = None ):
374+ def shout (self : Self , message : str ) -> None :
375+ for area in self .world .areas .values ():
376+ self .messages .to (
377+ area .players , "player-shouts" , player = self .name , message = message
378+ )
379+
380+ def say (self : Self , message : str ) -> None :
381+ self .messages .to (
382+ self .area .players , "player-says" , player = self .name , message = message
383+ )
384+
385+ def whisper (self : Self , target : str , message : str ) -> None :
386+ for player in self .area .players :
387+ if player .name == target :
388+ self .messages .to (
389+ [player , self ],
390+ "player-whispers" ,
391+ player = self .name ,
392+ target = target ,
393+ message = message ,
394+ )
395+ break
396+ else :
397+ self .messages .to ([self ], "whisper-target-nonexistant" )
398+
399+ def main_loop (self , stdin = None , stdout = None ):
374400 """Runs the game."""
375401 if stdout is None :
376- print (world .intro_text )
402+ print (self . world .intro_text )
377403 else :
378- stdout .write (world .intro_text + "\n " )
379- self .shell = Shell (world .messages , self , stdin = stdin , stdout = stdout )
380- self .enter_area (world .spawn )
404+ stdout .write (self . world .intro_text + "\n " )
405+ self .shell = Shell (self . world .messages , self , stdin = stdin , stdout = stdout )
406+ self .enter_area (self . world .spawn )
381407 self .shell .cmdloop ()
382408 # afterwards, leave the current area
383409 self .leave_area ()
0 commit comments