Skip to content

Commit

Permalink
Refactor end turn logic
Browse files Browse the repository at this point in the history
Lose one turn when wearing/taking off armor. Prevent AI from taking a
turn after game over.
  • Loading branch information
leomartius committed Sep 24, 2024
1 parent e1b6bba commit d3fdb80
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
11 changes: 11 additions & 0 deletions game/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from game.entity import Actor, ArmorItem, Item, Player, WeaponItem
from game.level import Level
from game.messages import MessageLog
from game.turn import end_turn


class Action:
Expand Down Expand Up @@ -106,6 +107,10 @@ def perform(self, actor: Actor, level: Level, log: MessageLog) -> bool:
if self.item.cursed and actor.inventory.is_equipped(self.item):
log.append("You can't. It appears to be cursed.")
return False
if self.item is actor.inventory.armor_slot:
end_turn(actor, level, log)
if actor.stats.hp == 0:
return False
actor.inventory.remove_item(self.item)
self.item.x, self.item.y = actor.x, actor.y
level.entities.add(self.item)
Expand Down Expand Up @@ -144,6 +149,9 @@ def __init__(self, item: Item):

def perform(self, actor: Actor, level: Level, log: MessageLog) -> bool:
assert isinstance(actor, Player)
end_turn(actor, level, log)
if actor.stats.hp == 0:
return False
actor.inventory.armor_slot = self.item
self.item.identified = True
log.append(f"You are now wearing {self.item}.")
Expand Down Expand Up @@ -175,6 +183,9 @@ def perform(self, actor: Actor, level: Level, log: MessageLog) -> bool:
if armor.cursed:
log.append("You can't. It appears to be cursed.")
return False
end_turn(actor, level, log)
if actor.stats.hp == 0:
return False
actor.inventory.armor_slot = None
log.append(f"You used to be wearing {armor}.")
return True
Expand Down
6 changes: 2 additions & 4 deletions game/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import tcod

import game.turn
from game.action import Action, BumpAction, DropAction, StairsAction, TakeOffAction, UseAction, WaitAction
from game.constants import Glyph
from game.entity import Player
Expand Down Expand Up @@ -91,10 +92,7 @@ def event(self, event: tcod.event.Event, player: Player, level: Level, log: Mess
def do_action(action: Action, player: Player, level: Level, log: MessageLog) -> State:
end_turn = action.perform(player, level, log)
if end_turn:
level.update_fov(player.x, player.y)
for actor in level.actors:
if actor.ai:
actor.ai.take_turn(actor, level, player).perform(actor, level, log)
game.turn.end_turn(player, level, log)
if player.stats.hp == 0:
assert log.unread > 0
return More()
Expand Down
14 changes: 14 additions & 0 deletions game/turn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from __future__ import annotations

from game.entity import Player
from game.level import Level
from game.messages import MessageLog


def end_turn(player: Player, level: Level, log: MessageLog) -> None:
level.update_fov(player.x, player.y)
for actor in level.actors:
if actor.ai:
if player.stats.hp == 0:
break
actor.ai.take_turn(actor, level, player).perform(actor, level, log)

0 comments on commit d3fdb80

Please sign in to comment.