Zmur
Zmur

Reputation: 334

Calling a method from arraylist

For a turn-based game I want to make a build queue for a planet. User puts items to build in a queue and then, when turn is generated, everything in a queue is being built.

Right now I have a Planet class which has an ArrayList. An object of type Planet is created for each planet the player owns. The array list contains everything user has ordered this planet to build. It also contains 3 variables that store the resources currently present on the planet and 3 variables that store total number of buildings that are currently on the planet.

Every building type is also a class (because building types costs differ), I plan to put objects of corresponding type into the planets queue. I suppose that each building class should have its own building method .build();

I want to cycle through each item in the queue of the planet and launch something like .buld() method. Building an object means subtracting some resources from the planets resource variables and adding number of buildings built to total number of buildings on a planet. The trouble is that I don't understand the main idea, how to achieve this.

The Planet class:

public class Planet extends WorldObject implements Serializable {

    private int defenses;
    private int mines;
    private int factories;
    //it also has 3 variables from WorldObject class:
    int iron;
    int bor;
    int ger;

    ArrayList<Installation> queue;

    private void build(){
        for ( int i = 0; i < queue.size()-1; i++ )
        {
            queue.get(i).build();
        }
    }
}

One of the building classes:

 public class Factory extends Installation {
        int costIron = 10;
        int costGer = 50;

        public build(int amt) {                
            //build itself, remove res from planet, add number built to planet factory list
        }    
    }

If I'm doing this totally wrong, I would be thankful if you say in which "direction" should I look for information.

Upvotes: 0

Views: 147

Answers (2)

NamshubWriter
NamshubWriter

Reputation: 24326

It sounds like Planet should have a Resources object that encapsulates the resources, and that could be passed to Installation.build(). Then Installation.build() could return a object that describes what was built.

Edit: here's an example:

public class MineFactory extends Installation {
    private final Resources cost;

    public MineFactory(Resources cost) {
      this.cost = cost.clone();
    }

    @Override
    public Result build(Resources availableResources) {    
      if (!availableResources.remove(cost)) {
        return Result.empty();
      }
      return Result.mines(2);    
    }    
}

Upvotes: 1

Dima
Dima

Reputation: 40510

You need to either pass your Planet object to build as an argument so that it has access to resources, or, alternatively, do the "building" in the Panet code itself, and just have getCost on the building, that you'd call yo find out how much resources to subtract.

Personally, I'd know kinda prefer the latter approach, but that's more of a question if taste than anything else.

Upvotes: 0

Related Questions