Search code examples
javadesign-patternscommandpath-finding

abstract implementation for tricky actions like, uhm, walking


Oh, hi. I'm a junior java developer working in my free time on some 2D tile-based game. Now I am trying to implement the very basic things in game Model - how objects of various types interact with each other. I hope to add network support someday, so now acting objects change their state synchronously - when singleton Ticker (running in its own thread) notifies subscribed objects with .tick(), making them execute command objects previously assigned to them. Implementing simple one-tick activities like picking up some object was quite simple, like:

abstract class Character extends AbstractActing {

    void tick() {
        action.execute();
    }

    private void pickUp(Item item) {
        inventory.add(item);
    }

    private IAction action;

    //...

    class PickUp implements IAction {
        void execute() {
            //check if allowed to do
            pickUp(item)
        }

        PickUp(Item item) {
            this.item = item;
        }

        private Item item;
    }
}

Now I want to write a skeletal implementation for more complicated actions like channeling some spell, kicking one's ass or running to point on map. I suppose that objects representing these complex actions will inside interprete themselves into queue of simple, atomic instructions. For example, "Walk(Location x)" command will internally use pathfinding algorithms to build queue of "DoOneStep(Direction d)" and will be able to rebuild it if something will happen in the way. The problem is that single atomic step will need more information to be executed like Character's speed or ground type. BTW, depending on character's speed I might wanna change Character's real location not every single tick. I think I have to add some step-executor object inside Action that observes objects related to calculations and updates its state.

My queston (finally!) is - is there some well-known design patterns fore me to meditate on? My code grows more unintelligible and un-inheritable with every edit, maybe I shall rewrite entire acting system?


Solution

  • You might want to consider using the State pattern (also known as the State Machine pattern, since it is based on the mathematical theory of finite state machines). The idea is that a character (or any other kind of game object, for that matter) is always in exactly one state (picked from a predetermined collection of possible states), and that certain events can trigger transitions to other states. Then, the tick() method can perform different actions depending on the current state. A more advanced version of this pattern is to have classes for the different states, and use instances of the state classes to collect information about e.g. how long you have been in that state. So in your case, possible states are Walking, PickingUp, Idle, etc.

    Actually, you've almost invented this pattern yourself, as your IAction concept is essentialy the same thing as a state. So I think you're headed in the right direction.

    Edit: To be more specific to your situation, I'd suggest that you simply drop the requirement that an IAction is atomic; instead, it may last for several ticks. IAction.execute() should simply perform as much of the action as can be performed in the course of one tick, such as walking a small distance along the desired path. The action itself can e.g. keep track of where the target position is, how far one has come, etc.

    (Historical note: The original Unreal Engine was based on states (and I'd guess that the newer versions are too) - when coding e.g. a custom weapon, you'd split the code into one section for each possible state, such as Firing, Reloading, Idle, etc.)