For a good while now, Im trying to structure my code better and focus on using OOP concepts more and more in my code .
I have a simple question about the following scenario and how it is solved the best in the view of design pattern.
I have a game, in which a player can rob a shop. The shop is represented by the "Shop" object and the robbery is represented by the "RobEvent". And I have a "Spawner" object, which handles the spawning of the securities, when someone attempts to rob a shop.
My central problem is, that I have the feeling, that the RobEvent object has too much information and functionality described below and that I could split information and functionality in more objects and for this I need the help of you!
public class Main {
public static void main(String[] args)
{
}
public class Shop {
private String name;
private Location location;
private boolean isBeingRobbed = false;
/*
* Here one of the problems, should the shop have the
* data of the npcs, which will appear
* in the shop when the player attempts to rob it.
* Should it have methods to place "spawn" positions of
* the securities, the type, the amount etc.
* and store it here?
*
*/
public Shop(String name, Location location){
this.name = name;
this.location = location;
}
public boolean isBeingRobbed(){
return isBeingRobbed;
}
protected void setBeingRobbed(boolean bool){
this.isBeingRobbed = bool;
}
}
public class RobEvent extends Looper {
private Shop shop;
RobEvent(Shop shop){
this.shop = shop;
}
public void start(){
shop.setBeingRobbed(true);
}
@Override
protected void logic(){
/*
* Big chunk of game logic
* Spawning the securities, checking if they
* all appeared(they got a delay of few seconds each),
* check if he ran away before everything
* spawned, check if he killed all, check if all appeared
*/
}
private void completed(){
shop.setBeingRobbed(false);
}
}
public class Spawner {
/* not important things
*
* just has a method to
* handle the spawn of a security
*/
public void spawn(NPC npc){
/*
* spawn the npc aka security
*/
}
}
}
My biggest problem is, that the logic() method gets really big. It is a method, which gets looped every second by the super class.
More detail: - The RobEvent has to know if it is currently spawning securities and do specific things, - it has to check if all got spawned and do specific things, - it has to check if the player ran away before it was completed and do specific things, - it has to check if the player killed all and do specific things etc.
Most annoying is keeping track of the spawned securities, because if he runs away, they all have to be despawned. I have the feeling that this is too much information in this object and that I could for example maybe split the tracking of the spawned securities in a seperate object and just do something like "tracker.despawn" when the RaidEvent has a specific state.
EDIT: Code is jut a really simplefication of the actual code.
Aucun commentaire:
Enregistrer un commentaire