added traps and HP-Postions

This commit is contained in:
Lukas Nöllemeyer 2024-08-21 18:14:56 +02:00
parent 96895c5ce5
commit 23d95c4b2f
2 changed files with 30 additions and 7 deletions

View file

@ -12,7 +12,7 @@ from tcod.event import KeySym
from tcod.map import Map
import g
from game.components import Action, Gold, Graphic, Position
from game.components import HP, Action, Gold, Graphic, Position
from game.constants import DIRECTION_KEYS, ACTION_KEYS, FLOOR_CHAR
from game.screens import Push, Screen, ScreenResult
from game.tags import IsWalllike, IsItem, IsPlayer, IsActor
@ -39,12 +39,21 @@ def _handle_movement(player, map, dir):
player.components[Position] = new_pos
_recalc_fov(new_pos)
# Auto pickup gold
for gold in g.world.Q.all_of(components=[Gold], tags=[player.components[Position], IsItem]):
# Auto pickup items
for gold in g.world.Q.all_of(components=[Gold], tags=[new_pos, IsItem]):
player.components[Gold] += gold.components[Gold]
text = f"Picked up {gold.components[Gold]}g, total: {player.components[Gold]}g"
g.world[None].components[("Text", str)] = text
gold.clear()
for hp in g.world.Q.all_of(components=[HP, Position], tags=[new_pos, IsItem]):
h_p = hp.components[HP]
player.components[HP] += hp.components[HP]
text = f"You stepped into a trap and took {-h_p} HP damage"
if h_p > 0:
text = f"You found a Poition and gained {h_p} HP"
text += f", you now have {player.components[HP]} HP"
g.world[None].components[("HP_Text", str)] = text
hp.clear()
def _handle_action(player):
for entity in g.world.Q.all_of(components=[Action, Position]):
@ -126,5 +135,8 @@ class MainScreen(Screen):
# draw the player
for player in g.world.Q.all_of(tags=[IsPlayer]):
_draw_entity(player, camera_pos, w, h)
if text := g.world[None].components.get(("Text", str)):
console.print(x=0, y=console.height - 1, string=text, fg=(255, 255, 255), bg=(0, 0, 0))
if hp_text := g.world[None].components.get(("HP_Text", str)):
console.print(x=0, y=console.height - 2, string=hp_text, fg=(255, 255, 255), bg=(0, 0, 0))

View file

@ -9,7 +9,7 @@ from tcod.map import Map
import g
from game.components import Action, Gold, Graphic, Position
from game.components import HP, Action, Gold, Graphic, Position
from game.tags import IsActor, IsItem, IsPlayer, IsWalllike
world_center: Final = Position(50, 50)
@ -51,7 +51,8 @@ def new_world() -> Registry:
components={
Position: player_pos,
Graphic: Graphic(ch=ord('@'), fg=(255, 0, 0, 255), bg=(255, 255, 255, 0)),
Gold: 0
Gold: 0,
HP: 100,
},
tags=[IsActor, IsPlayer]
)
@ -59,7 +60,8 @@ def new_world() -> Registry:
world.new_entity(
components={
Position: Position(0, 5),
Graphic: Graphic(ch=ord('G'), fg=(100,100,0, 255))
Graphic: Graphic(ch=ord('G'), fg=(100,100,0, 255)),
HP: 20,
},
tags=[IsActor]
)
@ -69,10 +71,19 @@ def new_world() -> Registry:
components={
Position: Position(rng.randint(0, 20), rng.randint(0, 20)),
Graphic: Graphic(ord("$"), fg=(255, 255, 0, 255)),
Gold: rng.randint(1, 10)
Gold: rng.randint(1, 10),
},
tags=[IsItem]
)
if rng.choice([True, False]):
world.new_entity(
components={
Position: Position(rng.randint(0, 10), rng.randint(0, 20)),
Graphic: Graphic(ord("t"), (255, 0, 0, 255)),
HP: rng.randint(-5, 5),
},
tags=[IsItem]
)
for i in range(20):
if i == 5 or i == 9: