2
2
import unittest
3
3
from unittest .mock import patch
4
4
5
+ from numpy import power
6
+
5
7
from actions import (
6
8
Action , ActionWithDirection ,
7
9
MovementAction ,
10
12
PickupAction ,
11
13
ItemAction ,
12
14
DropItem ,
15
+ TakeStairAction ,
13
16
)
14
17
from entity import Entity , Actor , Item
15
- from game_map import GameMap
18
+ from game_map import GameMap , GameWorld
16
19
from engine import Engine
17
20
from components .ai import BaseAI , HostileEnemy
18
21
from components .fighter import Fighter
19
22
from components .consumable import Consumable
20
23
from components .inventory import Inventory
24
+ from components .level import Level
21
25
import tile_types
22
26
import color
23
27
from exceptions import Impossible
@@ -62,6 +66,61 @@ def test_perform(self):
62
66
'''
63
67
64
68
69
+ class Test_Actions_TakeStairsAction (unittest .TestCase ):
70
+ def test_perform_with_stairs (self ):
71
+ actor = Actor (
72
+ ai_cls = BaseAI ,
73
+ fighter = Fighter (hp = 10 , defense = 10 , power = 10 ),
74
+ inventory = Inventory (capacity = 5 ),
75
+ level = Level ()
76
+ )
77
+ eng = Engine (player = actor )
78
+ eng .game_world = GameWorld (
79
+ engine = eng ,
80
+ map_width = 10 ,
81
+ map_height = 10 ,
82
+ max_rooms = 5 ,
83
+ room_min_size = 3 ,
84
+ room_max_size = 4 ,
85
+ max_monsters_per_room = 2 ,
86
+ max_items_per_room = 1 ,
87
+ )
88
+ eng .game_world .generate_floor ()
89
+ actor .x , actor .y = eng .game_map .downstairs_location
90
+ action = TakeStairAction (entity = actor )
91
+
92
+ with patch ('message_log.MessageLog.add_message' ) as patch_add_message :
93
+ action .perform ()
94
+
95
+ self .assertEqual (action .engine .game_world .current_floor , 2 )
96
+ patch_add_message .assert_called_once ()
97
+
98
+ def test_perform_no_stairs (self ):
99
+ actor = Actor (
100
+ ai_cls = BaseAI ,
101
+ fighter = Fighter (hp = 10 , defense = 10 , power = 10 ),
102
+ inventory = Inventory (capacity = 5 ),
103
+ level = Level ()
104
+ )
105
+ eng = Engine (player = actor )
106
+ eng .game_world = GameWorld (
107
+ engine = eng ,
108
+ map_width = 10 ,
109
+ map_height = 10 ,
110
+ max_rooms = 5 ,
111
+ room_min_size = 3 ,
112
+ room_max_size = 4 ,
113
+ max_monsters_per_room = 2 ,
114
+ max_items_per_room = 1 ,
115
+ )
116
+ eng .game_world .generate_floor ()
117
+ # actor.x, actor.y = eng.game_map.downstairs_location
118
+ action = TakeStairAction (entity = actor )
119
+
120
+ with self .assertRaises (Impossible ):
121
+ action .perform ()
122
+
123
+
65
124
class Test_Actions_ActionWithDirection (unittest .TestCase ):
66
125
def test_init (self ):
67
126
'''
@@ -125,10 +184,20 @@ def test_target_actor_with_actor(self):
125
184
test that target_actor returns and actor if one exists at the location
126
185
of the action
127
186
'''
128
- pl = Actor (ai_cls = BaseAI , fighter = Fighter (
129
- hp = 10 , defense = 10 , power = 10 ), inventory = Inventory (capacity = 5 )) # player at 0, 0
130
- ent = Actor (x = 1 , y = 1 , ai_cls = BaseAI , fighter = Fighter (
131
- hp = 10 , defense = 10 , power = 10 ), inventory = Inventory (capacity = 5 )) # entity at 1, 1
187
+ pl = Actor (
188
+ ai_cls = BaseAI ,
189
+ fighter = Fighter (hp = 10 , defense = 10 , power = 10 ),
190
+ inventory = Inventory (capacity = 5 ),
191
+ level = Level (),
192
+ ) # player at 0, 0
193
+ ent = Actor (
194
+ x = 1 ,
195
+ y = 1 ,
196
+ ai_cls = BaseAI ,
197
+ fighter = Fighter (hp = 10 , defense = 10 , power = 10 ),
198
+ inventory = Inventory (capacity = 5 ),
199
+ level = Level (),
200
+ ) # entity at 1, 1
132
201
eng = Engine (player = pl )
133
202
gm = GameMap (engine = eng , width = 10 , height = 10 )
134
203
gm .entities = {pl , ent }
@@ -143,8 +212,12 @@ def test_target_actor_noner(self):
143
212
test that target_actor returns nothing when none exists at the location
144
213
of the action
145
214
'''
146
- pl = Actor (ai_cls = BaseAI , fighter = Fighter (
147
- hp = 10 , defense = 10 , power = 10 ), inventory = Inventory (capacity = 5 )) # player at 0, 0
215
+ pl = Actor (
216
+ ai_cls = BaseAI ,
217
+ fighter = Fighter (hp = 10 , defense = 10 , power = 10 ),
218
+ inventory = Inventory (capacity = 5 ),
219
+ level = Level ()
220
+ ) # player at 0, 0
148
221
eng = Engine (player = pl )
149
222
gm = GameMap (engine = eng , width = 10 , height = 10 )
150
223
gm .entities = {pl , }
@@ -184,9 +257,11 @@ def test_perform_player_with_target_and_damage(self, mock_add_message):
184
257
put out a message with the correct color
185
258
'''
186
259
pl = Actor (x = 0 , y = 0 , ai_cls = HostileEnemy , fighter = Fighter (
187
- hp = 10 , defense = 0 , power = 5 ), inventory = Inventory (capacity = 5 )) # player at 0,0
260
+ hp = 10 , defense = 0 , power = 5 ), inventory = Inventory (capacity = 5 ),
261
+ level = Level ()) # player at 0,0
188
262
ent = Actor (x = 1 , y = 1 , ai_cls = HostileEnemy , fighter = Fighter (
189
- hp = 10 , defense = 0 , power = 5 ), inventory = Inventory (capacity = 5 )) # blocking entity at 1,1
263
+ hp = 10 , defense = 0 , power = 5 ), inventory = Inventory (capacity = 5 ),
264
+ level = Level ()) # blocking entity at 1,1
190
265
eng = Engine (player = pl )
191
266
gm = GameMap (engine = eng , width = 10 , height = 10 )
192
267
# add blocking entity to the game map, add game map to engine and player
@@ -213,11 +288,14 @@ def test_perform_enemy_with_target_and_damage(self, mock_add_message):
213
288
put out a message with the correct color
214
289
'''
215
290
pl = Actor (x = 0 , y = 0 , ai_cls = HostileEnemy , fighter = Fighter (
216
- hp = 10 , defense = 0 , power = 5 ), inventory = Inventory (capacity = 5 )) # player at 0,0
291
+ hp = 10 , defense = 0 , power = 5 ), inventory = Inventory (capacity = 5 ),
292
+ level = Level ()) # player at 0,0
217
293
ent = Actor (x = 1 , y = 1 , ai_cls = HostileEnemy , fighter = Fighter (
218
- hp = 10 , defense = 0 , power = 5 ), inventory = Inventory (capacity = 5 )) # blocking entity at 1,1
294
+ hp = 10 , defense = 0 , power = 5 ), inventory = Inventory (capacity = 5 ),
295
+ level = Level ()) # blocking entity at 1,1
219
296
ent2 = Actor (x = 2 , y = 2 , ai_cls = HostileEnemy , fighter = Fighter (
220
- hp = 10 , defense = 0 , power = 5 ), inventory = Inventory (capacity = 5 )) # blocking entity at 1,1
297
+ hp = 10 , defense = 0 , power = 5 ), inventory = Inventory (capacity = 5 ),
298
+ level = Level ()) # blocking entity at 1,1
221
299
eng = Engine (player = pl )
222
300
gm = GameMap (engine = eng , width = 10 , height = 10 )
223
301
# add blocking entity to the game map, add game map to engine and player
@@ -244,9 +322,11 @@ def test_perform_with_target_and_no_damage(self, mock_add_message):
244
322
test that a Melee Action with a target will print when no damage is done
245
323
'''
246
324
pl = Actor (x = 0 , y = 0 , ai_cls = HostileEnemy , fighter = Fighter (
247
- hp = 10 , defense = 0 , power = 5 ), inventory = Inventory (capacity = 5 )) # player at 0,0
325
+ hp = 10 , defense = 0 , power = 5 ), inventory = Inventory (capacity = 5 ),
326
+ level = Level ()) # player at 0,0
248
327
ent = Actor (x = 1 , y = 1 , ai_cls = HostileEnemy , fighter = Fighter (
249
- hp = 10 , defense = 5 , power = 5 ), inventory = Inventory (capacity = 5 )) # blocking entity at 1,1
328
+ hp = 10 , defense = 5 , power = 5 ), inventory = Inventory (capacity = 5 ),
329
+ level = Level ()) # blocking entity at 1,1
250
330
eng = Engine (player = pl )
251
331
gm = GameMap (engine = eng , width = 10 , height = 10 )
252
332
# add blocking entity to the game map, add game map to engine and player
@@ -350,10 +430,22 @@ def test_perform_melee(self, mock_add_message):
350
430
verify that a BumpAction performs the same as a MeleeAction
351
431
basically a copy of Test_Actions_MeleeAction.test_perform_with_target
352
432
'''
353
- pl = Actor (x = 0 , y = 0 , ai_cls = HostileEnemy , fighter = Fighter (
354
- hp = 10 , defense = 0 , power = 5 ), inventory = Inventory (capacity = 5 )) # player at 0,0
355
- ent = Actor (x = 1 , y = 1 , ai_cls = HostileEnemy , fighter = Fighter (
356
- hp = 10 , defense = 0 , power = 5 ), inventory = Inventory (capacity = 5 )) # blocking entity at 1,1
433
+ pl = Actor (
434
+ x = 0 ,
435
+ y = 0 ,
436
+ ai_cls = HostileEnemy ,
437
+ fighter = Fighter (hp = 10 , defense = 0 , power = 5 ),
438
+ inventory = Inventory (capacity = 5 ),
439
+ level = Level ()
440
+ ) # player at 0,0
441
+ ent = Actor (
442
+ x = 1 ,
443
+ y = 1 ,
444
+ ai_cls = HostileEnemy ,
445
+ fighter = Fighter (hp = 10 , defense = 0 , power = 5 ),
446
+ inventory = Inventory (capacity = 5 ),
447
+ level = Level ()
448
+ ) # blocking entity at 1,1
357
449
eng = Engine (player = pl )
358
450
gm = GameMap (engine = eng , width = 10 , height = 10 )
359
451
# add blocking entity to the game map, add game map to engine and player
@@ -398,7 +490,8 @@ def test_init(self):
398
490
test that a pickup action can be initialized okay
399
491
'''
400
492
actor = Actor (ai_cls = HostileEnemy , fighter = Fighter (
401
- hp = 10 , defense = 10 , power = 10 ), inventory = Inventory (capacity = 5 ))
493
+ hp = 10 , defense = 10 , power = 10 ), inventory = Inventory (capacity = 5 ),
494
+ level = Level ())
402
495
action = PickupAction (entity = actor )
403
496
self .assertIsInstance (action , PickupAction )
404
497
@@ -412,7 +505,9 @@ def test_perform_with_item_and_capacity(self):
412
505
x = 5 , y = 6 ,
413
506
ai_cls = HostileEnemy ,
414
507
fighter = Fighter (hp = 10 , defense = 10 , power = 10 ),
415
- inventory = Inventory (capacity = 5 ))
508
+ inventory = Inventory (capacity = 5 ),
509
+ level = Level ()
510
+ )
416
511
eng = Engine (player = actor )
417
512
gm = GameMap (engine = eng , width = 10 , height = 10 )
418
513
item = Item (
@@ -445,7 +540,9 @@ def test_perform_with_no_item_on_map(self):
445
540
x = 5 , y = 6 ,
446
541
ai_cls = HostileEnemy ,
447
542
fighter = Fighter (hp = 10 , defense = 10 , power = 10 ),
448
- inventory = Inventory (capacity = 5 ))
543
+ inventory = Inventory (capacity = 5 ),
544
+ level = Level ()
545
+ )
449
546
eng = Engine (player = actor )
450
547
gm = GameMap (engine = eng , width = 10 , height = 10 )
451
548
# item = Item(
@@ -471,7 +568,9 @@ def test_perform_with_item_in_wrong_spot(self):
471
568
x = 5 , y = 6 ,
472
569
ai_cls = HostileEnemy ,
473
570
fighter = Fighter (hp = 10 , defense = 10 , power = 10 ),
474
- inventory = Inventory (capacity = 5 ))
571
+ inventory = Inventory (capacity = 5 ),
572
+ level = Level ()
573
+ )
475
574
eng = Engine (player = actor )
476
575
gm = GameMap (engine = eng , width = 10 , height = 10 )
477
576
item = Item (
@@ -497,7 +596,9 @@ def test_perform_with_no_capacity(self):
497
596
x = 5 , y = 6 ,
498
597
ai_cls = HostileEnemy ,
499
598
fighter = Fighter (hp = 10 , defense = 10 , power = 10 ),
500
- inventory = Inventory (capacity = 0 ))
599
+ inventory = Inventory (capacity = 0 ),
600
+ level = Level ()
601
+ )
501
602
eng = Engine (player = actor )
502
603
gm = GameMap (engine = eng , width = 10 , height = 10 )
503
604
item = Item (
@@ -522,7 +623,8 @@ def test_init_no_targetxy(self):
522
623
and the target_xy gets set to the x, y of the entity
523
624
'''
524
625
actor = Actor (x = 5 , y = 6 , ai_cls = HostileEnemy , fighter = Fighter (
525
- hp = 10 , defense = 10 , power = 10 ), inventory = Inventory (capacity = 5 ))
626
+ hp = 10 , defense = 10 , power = 10 ), inventory = Inventory (capacity = 5 ),
627
+ level = Level ())
526
628
item = Item (consumable = Consumable ())
527
629
item_action = ItemAction (entity = actor , item = item )
528
630
self .assertEqual (item_action .item , item )
@@ -534,7 +636,8 @@ def test_init_with_targetxy(self):
534
636
and the target_xy gets set
535
637
'''
536
638
actor = Actor (ai_cls = HostileEnemy , fighter = Fighter (
537
- hp = 10 , defense = 10 , power = 10 ), inventory = Inventory (capacity = 5 ))
639
+ hp = 10 , defense = 10 , power = 10 ), inventory = Inventory (capacity = 5 ),
640
+ level = Level ())
538
641
item = Item (consumable = Consumable ())
539
642
item_action = ItemAction (entity = actor , item = item , target_xy = (5 , 6 ))
540
643
self .assertEqual (item_action .item , item )
@@ -545,7 +648,8 @@ def test_property_target_actor(self):
545
648
test that get_actor_at_location is called with the correct inputs
546
649
'''
547
650
actor = Actor (ai_cls = HostileEnemy , fighter = Fighter (
548
- hp = 10 , defense = 10 , power = 10 ), inventory = Inventory (capacity = 5 ))
651
+ hp = 10 , defense = 10 , power = 10 ), inventory = Inventory (capacity = 5 ),
652
+ level = Level ())
549
653
item = Item (consumable = Consumable ())
550
654
eng = Engine (player = actor )
551
655
gm = GameMap (engine = eng , width = 10 , height = 10 )
@@ -564,7 +668,8 @@ def test_perform(self):
564
668
passing in the existing itemAction
565
669
'''
566
670
actor = Actor (ai_cls = HostileEnemy , fighter = Fighter (
567
- hp = 10 , defense = 10 , power = 10 ), inventory = Inventory (capacity = 5 ))
671
+ hp = 10 , defense = 10 , power = 10 ), inventory = Inventory (capacity = 5 ),
672
+ level = Level ())
568
673
item = Item (consumable = Consumable ())
569
674
item_action = ItemAction (entity = actor , item = item )
570
675
@@ -582,7 +687,8 @@ def test_perform(self):
582
687
ent = Actor (
583
688
ai_cls = BaseAI ,
584
689
fighter = Fighter (hp = 10 , defense = 10 , power = 10 ),
585
- inventory = Inventory (capacity = 1 )
690
+ inventory = Inventory (capacity = 1 ),
691
+ level = Level ()
586
692
)
587
693
item = Item (consumable = Consumable ())
588
694
ent .inventory .items .append (item )
0 commit comments