Giter VIP home page Giter VIP logo

Comments (2)

TStand90 avatar TStand90 commented on September 6, 2024

Part 5 doesn't get into actual Entity actions, but I'm glad you're thinking ahead to it. For part 5 specifically, I think this is enough:

    def handle_enemy_turns(self):
        for entity in self.game_map.entities - {self.player}:
            print(f'The {entity.name} wonders when it will get to take a real turn.')

When we get to Part 6 (where the enemies to take real turns), what if we have a "can_act" property, which does all the necessary checks? It can check if the entity is an Actor and if it isn't dead.

from tcod_tutorial_v2.

HexDecimal avatar HexDecimal commented on September 6, 2024

Just assume I'm talking about part X and later in the issues posts. Ignore the code for future parts until they become relevant.

The thing about a property is it can't hint at the type of the class by itself. Which is why tcod's event.type had to be replaced with isinstance. At the very least you'd need to add assert isinstance or some other type cast in the property or after the check.

There's an alternative method which can be used. Which is to add an empty on_turn method to Entity and override it for Actor, the overridden method on Actor will know that the type for self is Actor.

class Entity:
    def on_turn(self, engine: Engine) -> None:
        """Called on each non-player entity during handle_enemy_turns."""


class Actor(Entity):
    def on_turn(self, engine: Engine) -> None:
        """Handle actions performed by an enemy actor."""
        if self.fighter.hp <= 0:
            continue
        print(f'The {self.name} wonders when it will get to take a real turn.')

...
def handle_enemy_turns(self):
    for entity in self.game_map.entities - {self.player}:
        entity.on_turn(self)

I don't know about the performance of this. I suspect it would be a little faster than isinstance.

from tcod_tutorial_v2.

Related Issues (17)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.