Java-Gaming.org    
Featured games (78)
games approved by the League of Dukes
Games in Showcase (429)
Games in Android Showcase (89)
games submitted by our members
Games in WIP (468)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Using less timers in a game  (Read 393 times)
0 Members and 1 Guest are viewing this topic.
Offline Fiskpotatis

Junior Newbie





« Posted 2013-05-18 20:13:47 »

It doesn't really lag that much in my game but I know that I could reduce alot of lag in my game by having less timers running. The game have multiple timers because it have one timer that updates everything like the players location and alla the obstacles then I have other timers that I use to remove the power ups that you could get, for example I have one timer that have it's initial delay set to 5000 and when it is runned one time (it will only run one time) it will remove a specific power up and the I have another timer that have it's initial delay set to 20 000. How would I keep the different delay of each timer but still only using one por at least fever than I use now?    
Online Roquen
« Reply #1 - Posted 2013-05-18 20:55:34 »

Simple answer: priority queue.
Offline ReBirth
« Reply #2 - Posted 2013-05-19 04:09:25 »

Or feed entites with delta and let them manage themselves according to it.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 613
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #3 - Posted 2013-05-21 02:45:38 »

Simple answer: priority queue.
True, but it's not as trivial 'just' using a PriorityQueue.


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
import java.util.*;

public class TimeQueue<T> {
   private final PriorityQueue<Slot<T>> queue;

   public TimeQueue() {
      queue = new PriorityQueue<Slot<T>>(11, new Comparator<Slot<T>>() {
         @Override
         public int compare(Slot<T> o1, Slot<T> o2) {
            return o1.time <= o2.time ? -1 : +1; // never return 0
        }
      });
   }

   public static class Slot<Q> {
      public final long time;
      public final Q item;

      public Slot(long time, Q item) {
         this.time = time;
         this.item = item;
      }

      @Override
      public String toString() {
         return "Slot[time=" + time + ", item=" + item + "]";
      }
   }

   // --

   public int size() {
      return queue.size();
   }

   public void clear() {
      queue.clear();
   }

   public Slot<T> insert(long time, T item) {
      Slot<T> slot = new Slot<T>(time, item);
      queue.add(slot);
      return slot;
   }

   public T poll(long now) {
      return hasItem(now) ? queue.poll().item : null;
   }

   public Slot<T> pollSlot(long now) {
      return hasItem(now) ? queue.poll() : null;
   }

   public Slot<T> peekSlot() {
      return queue.peek();
   }

   private final boolean hasItem(long now) {
      Slot<T> peeked = queue.peek();
      return (peeked != null && peeked.time <= now);
   }

   public List<T> drain() {
      List<T> list = new ArrayList<>();
      for (Slot<T> slot; (slot = queue.poll()) != null;) {
         list.add(slot.item);
      }
      return list;
   }

   public List<Slot<T>> drainSlots() {
      List<Slot<T>> list = new ArrayList<>();
      for (Slot<T> slot; (slot = queue.poll()) != null;) {
         list.add(slot);
      }
      return list;
   }

   public int remove(T item) {
      int count = 0;
      for (Iterator<Slot<T>> it = queue.iterator(); it.hasNext();) {
         if (item == it.next().item) {
            it.remove();
            count++;
         }
      }
      return count;
   }

   public int remove(Filter<T> filter) {
      int count = 0;
      for (Iterator<Slot<T>> it = queue.iterator(); it.hasNext();) {
         if (filter.accept(it.next().item)) {
            it.remove();
            count++;
         }
      }
      return count;
   }
}



1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
TimeQueue<Runnable> eventQueue = new TimeQueue<>();
eventQueue.insert(Game.TICK_TIME + 1000, new Task());
eventQueue.insert(Game.TICK_TIME + 500, new Task());
eventQueue.insert(Game.TICK_TIME + 500, new Task());
eventQueue.insert(Game.TICK_TIME + 2500, new Task());

public void tick(long now) {
   while(true) {
      Runnable task = eventQueue.poll(now);
      if(task == null) {
         break;
      }
      task.run();
   }
}

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

theagentd (6 views)
2014-04-24 23:00:44

xsi3rr4x (83 views)
2014-04-15 18:08:23

BurntPizza (75 views)
2014-04-15 03:46:01

UprightPath (86 views)
2014-04-14 17:39:50

UprightPath (69 views)
2014-04-14 17:35:47

Porlus (86 views)
2014-04-14 15:48:38

tom_mai78101 (109 views)
2014-04-10 04:04:31

BurntPizza (169 views)
2014-04-08 23:06:04

tom_mai78101 (265 views)
2014-04-05 13:34:39

trollwarrior1 (217 views)
2014-04-04 12:06:45
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!