Skip to content

Commit 4adc28d

Browse files
committed
added dodging
1 parent 56ea888 commit 4adc28d

File tree

10 files changed

+75
-26
lines changed

10 files changed

+75
-26
lines changed

actions.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import color
66
import exceptions
7+
import random
78
if TYPE_CHECKING:
89
from engine import Engine
910
from entity import Actor, Entity, Item
@@ -81,6 +82,15 @@ def perform(self) -> None:
8182
if not target:
8283
raise exceptions.Impossible("Nothing to attack.")
8384

85+
# Chance for the attacker to dodge, based on his dodge chance
86+
roll = random.randint(1, 100)
87+
if roll <= target.fighter.dodge:
88+
self.engine.message_log.add_message(
89+
f"{self.entity.name.capitalize()} Tries to attack, but {target.name.capitalize()} dodges the attack!",
90+
color.status_effect_applied
91+
)
92+
return
93+
8494
damage = self.entity.fighter.power - target.fighter.defense
8595

8696
attack_desc = f"{self.entity.name.capitalize()} attacks {target.name.capitalize()}"

color.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
black = (0, 0, 0)
33
red = (255, 0, 0)
44

5+
bg_lab = (0, 0, 45)
6+
current_bg = bg_lab
7+
58
player_atk = (255, 153, 0)
69
enemy_atk = (153, 0, 0)
710
needs_target = (51, 153, 255)

components/equipment.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,18 @@ def power_bonus(self) -> int:
3939
bonus += self.armor.equippable.power_bonus
4040

4141
return bonus
42+
43+
@property
44+
def dodge_bonus(self) -> int:
45+
bonus = 0
46+
47+
if self.weapon is not None and self.weapon.equippable is not None:
48+
bonus += self.weapon.equippable.dodge_bonus
49+
50+
if self.armor is not None and self.armor.equippable is not None:
51+
bonus += self.armor.equippable.dodge_bonus
52+
53+
return bonus
4254

4355
def item_is_equipped(self, item: Item) -> bool:
4456
return self.weapon == item or self.armor == item

components/equippable.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ def __init__(
1717
equipment_type: EquipmentType,
1818
power_bonus: int = 0,
1919
defense_bonus: int = 0,
20+
dodge_bonus: int = 0,
2021
):
2122
self.equipment_type = equipment_type
2223

2324
self.power_bonus = power_bonus
2425
self.defense_bonus = defense_bonus
26+
self.dodge_bonus = dodge_bonus
2527

2628
class PocketKinfe(Equippable):
2729
def __init__(self) -> None:

components/fighter.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,29 @@
1313
class Fighter(BaseComponent):
1414
parent: Actor
1515

16-
def __init__(self, hp: int, base_defense: int, base_power: int):
16+
def __init__(self, hp: int, base_dodge: int, base_defence, base_power: int):
1717
self.max_hp = hp
1818
self._hp = hp
19-
self.base_defense = base_defense
19+
self.base_dodge = base_dodge
20+
self.base_defence = base_defence
2021
self.base_power = base_power
2122

2223
@property
23-
def defense(self) -> int:
24-
return self.base_defense + self.defense_bonus
24+
def dodge(self) -> int:
25+
return self.base_dodge + self.dodge_bonus
2526

2627
@property
2728
def power(self) -> int:
2829
return self.base_power + self.power_bonus
30+
31+
@property
32+
def defense(self) -> int:
33+
return self.base_defence + self.defense_bonus
2934

3035
@property
31-
def defense_bonus(self) -> int:
36+
def dodge_bonus(self) -> int:
3237
if self.parent.equipment:
33-
return self.parent.equipment.defense_bonus
38+
return self.parent.equipment.dodge_bonus
3439
else:
3540
return 0
3641

@@ -40,6 +45,13 @@ def power_bonus(self) -> int:
4045
return self.parent.equipment.power_bonus
4146
else:
4247
return 0
48+
49+
@property
50+
def defense_bonus(self) -> int:
51+
if self.parent.equipment:
52+
return self.parent.equipment.defense_bonus
53+
else:
54+
return 0
4355

4456
@property
4557
def hp(self) -> int:

components/level.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,14 @@ def increase_power(self, amount: int = 1) -> None:
6666

6767
self.increase_level()
6868

69-
def increase_defense(self, amount: int = 1) -> None:
70-
self.parent.fighter.base_defense += amount
69+
def increase_dodge(self, amount: int = 1) -> None:
70+
self.parent.fighter.base_dodge += amount
7171

72-
self.engine.message_log.add_message("Your movements are getting swifter!")
72+
# Cap at 50%
73+
if self.parent.fighter.base_dodge > 50:
74+
self.parent.fighter.base_dodge = 50
75+
self.engine.message_log.add_message("You are at your max dodge chance! Increase your dodge chance with some sort of special gear.")
76+
77+
self.engine.message_log.add_message("You are more dodgy!")
7378

7479
self.increase_level()

entity_factories.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
color=(255, 255, 0),
1212
name="Player",
1313
ai_cls=HostileEnemy,
14-
fighter=Fighter(hp=30, base_defense=1, base_power=2),
14+
fighter=Fighter(hp=30, base_dodge=5, base_power=2, base_defence=1),
1515
inventory=Inventory(capacity=26),
1616
inspect_message="It's yourself. What would you ask for?",
1717
level=Level(level_up_base=200),
@@ -23,7 +23,7 @@
2323
color=(255, 80, 80),
2424
name="Slime Mold",
2525
ai_cls=HostileEnemy,
26-
fighter=Fighter(hp=10, base_defense=0, base_power=3),
26+
fighter=Fighter(hp=10, base_dodge=0, base_power=3, base_defence=0),
2727
inventory=Inventory(capacity=0),
2828
inspect_message="It's a slime mold... That is alive! It looks hungry for your flesh.",
2929
level=Level(xp_given=35),
@@ -34,7 +34,7 @@
3434
color=(200, 174, 137),
3535
name="Rusty Automaton",
3636
ai_cls=HostileEnemy,
37-
fighter=Fighter(hp=16, base_defense=1, base_power=4),
37+
fighter=Fighter(hp=16, base_defence=1, base_power=4, base_dodge=4),
3838
inventory=Inventory(capacity=0),
3939
inspect_message="He looks like he's been here a while. I Won't say he's having a great time existing.",
4040
level=Level(xp_given=100),
@@ -45,7 +45,7 @@
4545
color=(244, 227, 210),
4646
name="Hunter Humanoid",
4747
ai_cls=HostileEnemy,
48-
fighter=Fighter(hp=20, base_defense=2, base_power=7),
48+
fighter=Fighter(hp=20, base_defence=1, base_power=7, base_dodge=10),
4949
inventory=Inventory(capacity=0),
5050
level=Level(xp_given=150),
5151
equipment=Equipment(),
@@ -56,7 +56,7 @@
5656
color=(0, 204, 102),
5757
name="Acid Mold",
5858
ai_cls=HostileEnemy,
59-
fighter=Fighter(hp=19, base_defense=1, base_power=6),
59+
fighter=Fighter(hp=19, base_defence=2, base_power=6, base_dodge=5),
6060
inventory=Inventory(capacity=0),
6161
level=Level(xp_given=50),
6262
equipment=Equipment(),
@@ -67,7 +67,7 @@
6767
color=(0, 204, 102),
6868
name="Mama Mold",
6969
ai_cls=SpawnerEnemy,
70-
fighter=Fighter(hp=30, base_defense=1, base_power=0),
70+
fighter=Fighter(hp=30, base_defence=0, base_power=0, base_dodge=0),
7171
inventory=Inventory(capacity=0),
7272
level=Level(xp_given=200),
7373
equipment=Equipment(),

game_map.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import numpy as np # type: ignore
66
from tcod.console import Console
7-
7+
from color import current_bg
88
from entity import Actor, Item
99
import tile_types
1010

@@ -182,6 +182,7 @@ def render(self, console: Console) -> None:
182182
If it isn't, but it's in the "explored" array, then draw it with the "dark" colors.
183183
Otherwise, the default is "SHROUD".
184184
"""
185+
185186
console.rgb[0 : self.width, 0 : self.height] = np.select(
186187
condlist=[self.visible, self.explored],
187188
choicelist=[self.tiles["light"], self.tiles["dark"]],
@@ -198,6 +199,7 @@ def render(self, console: Console) -> None:
198199
console.print(
199200
x=entity.x, y=entity.y, string=entity.char, fg=entity.color
200201
)
202+
201203

202204
class GameWorld:
203205
"""

input_handlers.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ def on_render(self, console: tcod.Console) -> None:
551551
x=x + 1,
552552
y=6,
553553
fg=color.text_console,
554-
string=f"c) Agility (+1 defense, from {self.engine.player.fighter.defense})".upper(),
554+
string=f"c) Agility (+5% dodge chance, from {self.engine.player.fighter.dodge}%)".upper(),
555555
)
556556

557557
def ev_keydown(self, event: tcod.event.KeyDown) -> Optional[ActionOrHandler]:
@@ -565,7 +565,7 @@ def ev_keydown(self, event: tcod.event.KeyDown) -> Optional[ActionOrHandler]:
565565
elif index == 1:
566566
player.level.increase_power()
567567
else:
568-
player.level.increase_defense()
568+
player.level.increase_dodge(5)
569569
else:
570570
self.engine.message_log.add_message("Invalid entry.", color.invalid)
571571

@@ -600,7 +600,7 @@ def on_render(self, console: tcod.Console) -> None:
600600
x=x,
601601
y=y,
602602
width=width,
603-
height=7,
603+
height=8,
604604
title=self.TITLE,
605605
clear=True,
606606
fg=color.text_console,
@@ -625,4 +625,7 @@ def on_render(self, console: tcod.Console) -> None:
625625
)
626626
console.print(
627627
x=x + 1, y=y + 5, fg=color.text_console, string=f"Defense: {self.engine.player.fighter.defense}".upper()
628-
)
628+
)
629+
console.print(
630+
x=x + 1, y=y + 6, fg=color.text_console, string=f"Dodge chance: {self.engine.player.fighter.dodge}%".upper()
631+
)

tile_types.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from typing import Tuple
2-
2+
from color import current_bg
33
import numpy as np # type: ignore
44

55
# Tile graphics structured type compatible with Console.tiles_rgb.
@@ -23,7 +23,7 @@
2323
)
2424

2525
# SHROUD represents unexplored, unseen tiles
26-
SHROUD = np.array((ord(" "), (255, 255, 255), (0, 0, 0)), dtype=graphic_dt)
26+
SHROUD = np.array((ord(" "), (255, 255, 255), (current_bg)), dtype=graphic_dt)
2727

2828
def new_tile(
2929
*, # Enforce the use of keywords, so that parameter order doesn't matter.
@@ -38,15 +38,15 @@ def new_tile(
3838

3939

4040
floor = new_tile(
41-
walkable=True, transparent=True, light=(ord("."), (102, 0, 51), (0, 0, 0)), dark=(ord("."), (51, 51, 51), (0, 0, 0)), autotile=False
41+
walkable=True, transparent=True, light=(ord("."), (102, 0, 51), current_bg), dark=(ord("."), (51, 51, 51), (current_bg)), autotile=False
4242
)
4343
wall = new_tile(
44-
walkable=False, transparent=False, light=(ord(" "), (153, 0, 153), (0,0,0)), dark=(ord(" "), (51, 51, 51), (0, 0, 0)), autotile=True
44+
walkable=False, transparent=False, light=(ord(" "), (153, 0, 153), current_bg), dark=(ord(" "), (51, 51, 51), (current_bg)), autotile=True
4545
)
4646
down_stairs = new_tile(
4747
walkable=True,
4848
transparent=True,
49-
dark=(ord(">"), (0, 100, 100), (0, 0 ,0)),
50-
light=(ord(">"), (255, 204, 102), (0, 0, 0)),
49+
dark=(ord(">"), (0, 100, 100), (current_bg)),
50+
light=(ord(">"), (255, 204, 102), (current_bg)),
5151
autotile=False
5252
)

0 commit comments

Comments
 (0)