1

I have a few questions about having code that runs automatically within a given interval. I'm programming a kind of game mode where it checks if the players have killed all the monsters in the map (I have my methods for these). I was wondering what's the best way to program this check? I've looked up ways where a person made a ScheduledExecutorService during a class constructor....

private ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();

But I saw online where people used

static void main(String[] args) {

And a run method to do the checks. Which would be better? I'm simply wanting to have a check that runs every few or so seconds that sees if the player has cleared the map, and if he or she has, then to progress to the next stage.

matteo rulli
  • 1,443
  • 2
  • 18
  • 30
Jacob Macallan
  • 959
  • 2
  • 8
  • 29
  • 1
    You dont need to check each time if you know how many monster their are at the start and keep track of them each time the player kill a monster. – yahya el fakir Jul 22 '15 at 09:40
  • I think the answer for this question is here [Stackoverflow][1] [1]: http://stackoverflow.com/questions/22163662/how-to-create-a-java-cron-job – Jijo John Jul 22 '15 at 09:41

2 Answers2

12

I think you can check this every time the player kills a monster. Maybe you want to listen to a KillMonster event. You can create interfaces and implement that in your other class.

public interface KillMonsterEventListener {
    void onKillMonster ();
}

And then in the player class you can call a method to call onKillMonster() of all the event listeners. I think you know how to do that. Just create a list of KillMonsterEventListeners and have a method like:

public void addKillMonsterEventListener (KillMonsterEventListener listener) {
    listeners.add (listener); //listeners is the list of KillMonsterEventListeners.
}
Mast
  • 1,788
  • 4
  • 29
  • 46
Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • 1
    this is the most elegant way to make the job. – Turrican Jul 22 '15 at 09:53
  • Thanks. I didn't think that I'll even get an upvote! @Turrican – Sweeper Jul 22 '15 at 10:06
  • I never have really dealt with interfaces before, but what I'm getting is to make the interface and add the code to my player class and define the methods that were in the interface. I add a listener each time my player kills a mob ... what if I only want it during a specific event? As in ... when my player is in a certain instance. I only want it during a game-mode and not during any other time. – Jacob Macallan Jul 22 '15 at 11:57
  • @Xari You can add parameters to the method `onKillMonster`. For example, you can add a `Player player` parameter to check whether the player is in the right game mode. Also, you don't need to add the listener _that_ many times. You just need to add it once maybe after calling the constructer of the player. – Sweeper Jul 22 '15 at 12:04
  • Forgive me then, but I never could grasp my head around interfaces except of the idea that it is flexible when it comes to creating variations of the implementation. How does the implementation in this case help with checking if the mobs on the map are completely dead? Once I add the listener, what theoretically should I be writing that checks for all the monsters being dead? – Jacob Macallan Jul 22 '15 at 12:07
  • @Xari You should have a variable that keeps track of the monsters, right? Check that variable. – Sweeper Jul 22 '15 at 12:10
  • Thank you, but now I'm confused about the point of the interface. Technically, couldn't I just increment a variable every time a mob is killed after it checks a boolean value for whether or not the player is in the instance, and upon incrementing check if it exceeds the amount of mobs originally in the map? – Jacob Macallan Jul 22 '15 at 12:11
  • @Xari Well, you're right. However, adding this event can improve your code's flexibility. What if later you want to do something else when the player kills a monster? – Sweeper Jul 22 '15 at 12:15
  • Ah, that definitely makes sense. It's a bit difficult to wrap my head around. I can have different implementations with different listeners, right? Each listener might have something that's different, but ultimately I can call the same method on all the implementations and they'll all do different things. – Jacob Macallan Jul 22 '15 at 12:36
1

Overall, it is somewhat costly to use Executor for just that, because you can easily have a counter for number of monsters and modify it whenever a monster is killed in the update tick. When it reaches 0, the player has cleared the level. Generally Executor is fine for larger operations like autosave to the file, it's also worth noting that the code will be executed on a background thread. Since you are just going to query the number of monsters left in the game and not alter UI, it should be fine.

Regards static void main(String[] args) { I don't see how that fits in the question.

AlmasB
  • 3,377
  • 2
  • 15
  • 17