Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would I control this textgame and how would the classes get a better structure to do this?

If i have a textgame that has a world object that has objects of type room which has objects of items and enemies and then a gamehelper object that has user objects for the actual player. (this game doesn't have wandering enemies since that would complicate my question to much for me :-))

How should I go about either killing an enemy or picking up an object? Or should I think some other way when structuring up my classes completely? I got the suggestion that I would need a manager that controls the world and mediates between users and objects but I can't figure out how that would look/work.

I would also implement interfaces of killable and pickable to separate objects from eachothers when necessary if thats of any relevance... And oh im learning Java so any example code that might be useful to understand could be written in that.

Ie:

        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)
like image 895
Patrik Björklund Avatar asked Nov 24 '25 22:11

Patrik Björklund


2 Answers

I don't see anything obviously wrong with your class structure unless your diagram is a diagram of it. :) Rooms, objects and enemies should certainly not inherit World. It looks like you're diagramming containment, though, which is fine.

How would you go about killing an enemy or picking up an object? Doesn't that depend on what those actions mean in your game? It sounds like, for picking up an object, an Object has an Environment that can be either a Room or a User; an Object whose Environment is a User is in that User's inventory. Picking up the Object changes its Environment from the Room to the User.

Possibly Objects can have Enemies as their Environment as well, so that killing an Enemy destructs the Enemy and moves Objects in its inventory into the Room that the Enemy was occupying. Or maybe you want a "dead Enemy" type of object? Really, it all depends on your game model.

like image 166
chaos Avatar answered Nov 26 '25 23:11

chaos


Interfaces for attackable and pickupable items, perhaps:

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

Then you can do simple type checks to see if you can attack or pick things up.

Yes, a mediator object sounds like a good idea... For instance, it might subscribe to all the IAttackable.Killed events and send a signal to the User class when something dies in the same room as them. It might also be a go-between for organising how much damage each takes.

Personally, I'd have a Creature class, and have both enemies and users subclass from that, to simplify things a little (and potentially enable enemies to fight amungst themselves if the need should arise).

like image 23
Matthew Scharley Avatar answered Nov 26 '25 21:11

Matthew Scharley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!