How can I control this text game and how would classes get a better structure for this?

If I have a text game in which there is a world object that has room objects in which there are objects of objects and enemies, then a gamehelper object that has custom objects for a real player. (there are no wandering enemies in this game, as this will greatly complicate my question :-))

How should I either kill the enemy or collect an object? Or should I think differently when I completely structure my classes? I got a suggestion that I need a manager who controls the world and mediates between users and objects, but I can’t understand how it will look / work .

I would also implement killable and pickable interfaces for separating objects from eachothers when necessary, if it relates to any relevance ... And oh, I'm learning Java, so any sample code that can be useful for understanding can be written in this one.

Those:

        World
         |
    ____Rooms____ (could be like a 4x4 array or something with X and Y cords)
    |           |
  Objects     Enemies (Either killable / pickupable)

        Game
         |
        User (can walk around in rooms, kill monsters and take treasure)
+3
source share
4 answers

I don’t see anything obviously wrong in your class structure if your diagram is not its diagram. :) Numbers, objects and enemies, of course, should not inherit the world. It looks like you're holding back charts, but that's fine.

? , ? , , , ; , , . .

, , , . , , " "? .

+1

, :

interface IAttackable {
    void Attack();
    event Killed;
}
interface ILiftable {
    void Get(Container putHere);
}

, , .

, - ... , IAttackable.Killed User, - , . , .

Creature, , , (, , , ).

+1

- , "". " ", , - .

! OO-Design, .

OO-Design " " Robert C. Martin. " " .

, , - , .

.

, , . , , , , - ! , , , , - "" "" , - , .

- , - . , " " , . . ( !): ? World-Object, 2D- , ( ?). , "". , , , (, , ).

, , , Item "Takeable"... ..

, - .

0

, , , . .

(, Python):

def main_loop():
    while not finished:
        command_text = ask_user_for_input()
        execute_player_command(command_text)
        update_world()
        send_output_to_player()

def execute_player_command(command_text):
    # look up the command object and work out what parameters to pass it
    command, parameters = parse(command_text)
    # ask the command to do its stuff
    command.execute(player, parameters)

def update_world():
    # obviously you can munge these together with an Updateable interface
    # or something similar
    for each creature in all_creatures:
        creature.update()
    for each object in all_objects:
        object.update()

, , , -, , , , update_world - . , , . , , .

. , , , , . , , , , . , , .. , .

0

Source: https://habr.com/ru/post/1711630/


All Articles