Results 1 to 9 of 9
  1. #1
    zock70 is offline Member
    Join Date
    Nov 2011
    Posts
    6
    Rep Power
    0

    Smile 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();
    			}
    		}
    	}
    	
    }
    Java 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.
    		
    	}
    	
    }
    I hope you guys find this useful. ~~Zock
    Last edited by zock70; 04-05-2012 at 02:42 AM. Reason: Rename

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,513
    Rep Power
    25

    Default 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.

  3. #3
    zock70 is offline Member
    Join Date
    Nov 2011
    Posts
    6
    Rep Power
    0

    Default Re: Event planning

    Oh, sorry about that, I'll go ahead and change it. Thank you :)

    EDIT: renamed Event to Tick
    Last edited by zock70; 04-05-2012 at 02:35 AM. Reason: bad name

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,298
    Rep Power
    20

    Default Re: Game Ticks (anyone can use)

    Moved from Advanced Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    zock70 is offline Member
    Join Date
    Nov 2011
    Posts
    6
    Rep Power
    0

    Default Re: Game Ticks (anyone can use)

    Quote Originally Posted by DarrylBurke View Post
    Moved from Advanced Java

    db
    Alright, thanks.

  6. #6
    zock70 is offline Member
    Join Date
    Nov 2011
    Posts
    6
    Rep Power
    0

    Default Re: Game Ticks (anyone can use)

    Bump

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,513
    Rep Power
    25

    Default 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.

  8. #8
    zock70 is offline Member
    Join Date
    Nov 2011
    Posts
    6
    Rep Power
    0

    Default Re: Game Ticks (anyone can use)

    Quote Originally Posted by Norm View Post
    Do you have a question?
    I wanted some feedback so that I can make it better etc

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Game Ticks (anyone can use)

    Quote Originally Posted by zock70 View Post
    I wanted some feedback so that I can make it better etc
    You'll likely be much better off asking a specific question if you have one.

Similar Threads

  1. GUI Planning?
    By Tombomb in forum AWT / Swing
    Replies: 6
    Last Post: 03-27-2012, 03:07 AM
  2. Planning projects
    By jammas615 in forum Advanced Java
    Replies: 2
    Last Post: 10-08-2011, 03:43 PM
  3. I need help planning.
    By AcousticBruce in forum New To Java
    Replies: 2
    Last Post: 02-26-2011, 04:22 PM
  4. program to assist in planning forms
    By shags_j in forum SWT / JFace
    Replies: 6
    Last Post: 11-02-2009, 05:04 AM
  5. checking for an event during an event
    By infinity in forum AWT / Swing
    Replies: 22
    Last Post: 04-09-2009, 01:08 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •