Results 1 to 9 of 9
Thread: Game Ticks (anyone can use)
- 04-05-2012, 02:13 AM #1
Member
- Join Date
- Nov 2011
- Posts
- 6
- Rep Power
- 0
Game Ticks (anyone can use)
Hey everyone, I have been working on something new that is great for games.
Java Code:package tickss; import starter.Main; public abstract class Tick{ private boolean stopped;//Is the tick stopped? private int time;//Time in seconds for delay. private boolean looping;//Is the tick looping? private long checkpoint;//when the tick will trigger next (in millis) //Creation of new tick. public Tick(int time){ this.time = time;//Sets the time for delay. stopped = false;//Tick isn't stopped. checkpoint = System.currentTimeMillis() + convertToMillis(time);//sets the time for when Tick starts. } public abstract void execute();//the code that is executed. public long convertToMillis(int seconds){//used to convert seconds to milliseconds because we're lazy. return (long) seconds*1000; } public int convertToSeconds(long millis){//not used in program, but can be used for something else if needed. return (int) millis/1000; } public boolean run(){ if(!stopped){//if the tick is running, this happens if(System.currentTimeMillis() >= checkpoint){ if(looping){//if the tick is supposed to loop, then it sets the next time that the tick will trigger. checkpoint = System.currentTimeMillis() + convertToMillis(time); } execute();//executes the tick. if(!looping){//if the tick is only supposed to happen once, it stops it. stop(); } } return true; }else{//if it stops, this happens return false; } } public void setLooping(boolean looping){//sets whether the tick loops or not this.looping = looping; } public void stop(){//stops the tick. stopped = true; } public void start(){//starts the tick (probably wont be needed by you) stopped = false; } public void setTime(int cycles){//used to change delay for this tick. time = cycles; } }Java Code:package ticks; import java.util.Iterator; import java.util.LinkedList; import starter.Main; public class Runner implements Runnable{ //This is used as the instance that is used for everything. private static final Runner instance = new Runner(); //used to return the instance above public static Runner getRunner(){ return instance; } private LinkedList<Tick> ticksToAdd = new LinkedList<Tick>(); private LinkedList<Tick> ticks = new LinkedList<Tick>(); //Use this to submit ticks or actions that will take place later or regularly. public void submit(Tick tick){ ticksToAdd.add(tick); } //This is done all tha time thanks to the while loop. @Override public void run(){ while(true){ //This if statement checks if there are any new ticks to add to the list and adds them to it if there are. if (ticksToAdd.size() > 0) { ticks.addAll(ticksToAdd); ticksToAdd = new LinkedList<Tick>(); } //This runs through the ticks and runs them. for (Iterator<Tick> it = ticks.iterator(); it.hasNext(); ) { if (!it.next().run()) {//If the tick is stopped, removes them from the list. it.remove(); } } try { Thread.sleep(10);//sleeps so it doesn't hog up a ton of memory. } catch (InterruptedException e) { e.printStackTrace(); } } } }I hope you guys find this useful. ~~ZockJava Code:package starter; import ticks.Tick; import ticks.Runner; public class Main { public static Thread tickHandler = new Thread(Runner.getRunner()); public static void main(String[] args){ //Here is an example of a one-time tick being scheduled. Runner.getRunner().submit(new Tick(5){//5 second delay on when the tick starts. @Override public void execute() { System.out.println("This tick has been executed."); } }); //Here is an example of a looping tick being scheduled. Runner.getRunner().submit(new Tick(5){//5 second delay on when the tick starts and when the next time it runs will be @Override public void execute() { setLooping(true); setTime(10);//changes delay between runs to 10 seconds. System.out.println("This tick has been executed."); } }); tickHandler.start();//starts the tickHandler thread that holds the ticks etc. } }Last edited by zock70; 04-05-2012 at 02:42 AM. Reason: Rename
- 04-05-2012, 02:28 AM #2
Re: Event planning
Poor choice of class name. Already used by Java SE: java.awt.Event
A unique name will save anyone problems.If you don't understand my response, don't ignore it, ask a question.
- 04-05-2012, 02:29 AM #3
Member
- Join Date
- Nov 2011
- Posts
- 6
- Rep Power
- 0
Re: Event planning
Oh, sorry about that, I'll go ahead and change it. Thank you :)
EDIT: renamed Event to TickLast edited by zock70; 04-05-2012 at 02:35 AM. Reason: bad name
- 04-05-2012, 06:25 AM #4
Re: Game Ticks (anyone can use)
Moved from Advanced Java
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 04-05-2012, 05:09 PM #5
Member
- Join Date
- Nov 2011
- Posts
- 6
- Rep Power
- 0
- 04-09-2012, 05:44 PM #6
Member
- Join Date
- Nov 2011
- Posts
- 6
- Rep Power
- 0
Re: Game Ticks (anyone can use)
Bump
- 04-09-2012, 05:47 PM #7
Re: Game Ticks (anyone can use)
Do you have a question?
If you don't understand my response, don't ignore it, ask a question.
- 04-09-2012, 11:13 PM #8
Member
- Join Date
- Nov 2011
- Posts
- 6
- Rep Power
- 0
-
Re: Game Ticks (anyone can use)
Similar Threads
-
GUI Planning?
By Tombomb in forum AWT / SwingReplies: 6Last Post: 03-27-2012, 03:07 AM -
Planning projects
By jammas615 in forum Advanced JavaReplies: 2Last Post: 10-08-2011, 03:43 PM -
I need help planning.
By AcousticBruce in forum New To JavaReplies: 2Last Post: 02-26-2011, 04:22 PM -
program to assist in planning forms
By shags_j in forum SWT / JFaceReplies: 6Last Post: 11-02-2009, 05:04 AM -
checking for an event during an event
By infinity in forum AWT / SwingReplies: 22Last Post: 04-09-2009, 01:08 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks