SilabGarza - RS Private Server  
Home Forum Play Register Store Market Highscores Mark Forums Read
Go Back   SilabGarza - RS Private Server > SilGar Media > Computers > Programming
Reload this Page [PI] Hyperion's eventmanager converted to PI


[PI] Hyperion's eventmanager converted to PI
Old
  (#1 (permalink))
phl0w is Offline
ARE YOU MAD?
image
 
phl0w's Avatar
 
Posts: 59
Cookies Given: 0
Cookies Received: 0
Time Online: 20 h 55 m
   
Default [PI] Hyperion's eventmanager converted to PI - 01-24-2012, 12:03 PM

Make a new folder "tick"
Put in 2 files:
Code:
package server.tick;

/**
 * Represents a task that is executed in the future, once or periodically.
 * Each "tick" represents 0.6ms, or one game loop.
 * @author Michael Bull
 *
 */
public abstract class Tick {
	
	/*
	 * What is the difference between remainingTicks and tickDelay?
	 * ============================================================
	 * 
	 * tickDelay = the delay in ticks between consecutive executions.
	 *   e.g. a combat event may run at 3 ticks, which mean each 3*600 ms
	 *        it is executed
	 *        
	 * remainingTicks = the number of ticks until this tickable next
	 *        runs. e.g. the tick delay will be 1 if it'll be ran in the next
	 *        cycle.
	 */

	/**
	 * The amount of ticks before this event executes.
	 */
	private int remainingTicks;
	
	/**
	 * The ticks to reset to once executed if the tickable still runs.
	 */
	private int tickDelay;
	
	/**
	 * The running flag.
	 */
	private boolean running = true;
	
	/**
	 * Creates a tickable with the specified amount of ticks.
	 * @param ticks The amount of ticks.
	 */
	public Tick(int ticks) {
		this.remainingTicks = ticks;
		this.tickDelay = ticks;
	}

	/**
	 * Gets the tick delay.
	 * @return The delay, in ticks.
	 */
	public int getTickDelay() {
		return tickDelay;
	}
	
	/**
	 * Gets the remaining ticks.
	 * @return The remaining ticks.
	 */
	public int getRemainingTicks() {
		return remainingTicks;
	}
	
	/**
	 * Sets the tick delay.
	 * @param ticks The amount of ticks to set.
	 * @throws IllegalArgumentException if the delay is negative.
	 */
	public void setTickDelay(int ticks) {
		if(ticks < 0) {
			throw new IllegalArgumentException("Tick amount must be positive.");
		}
		this.tickDelay = ticks;
	}
	
	/**
	 * Checks if the tick is running.
	 * @return <code>true</code> if the tick is still running, <code>false</code> if not.
	 */
	public boolean isRunning() {
		return running;
	}
	
	/**
	 * Stops the tick from running in the future.
	 */
	public void stop() {
		running = false;
	}
	
	/**
	 * The execute method is called when the tick is run. The general contract
	 * of the execute method is that it may take any action whatsoever.
	 */
	public abstract void execute();

	/**
	 * This is executed every cycle and handles the counters and such.
	 */
	public void cycle() {
		if(remainingTicks-- <= 1) {
			remainingTicks = tickDelay;
			if(isRunning()) {
				execute();
			}
		}
	}
	
}
and

Code:
package server.tick;

import java.util.LinkedList;
import java.util.List;

/**
 * A class that manages <code>Tickable</code>s for a specific
 * <code>GameEngine</code>.
 * @author Michael Bull
 *
 */
public class TickManager {
	
	/**
	 * The list of tickables.
	 */
	private List<Tick> ticks = new LinkedList<Tick>();
	
	/**
	 * @return The tickables.
	 */
	public List<Tick> getTickables() {
		return ticks;
	}
	
	/**
	 * Submits a new tickable to the <code>GameEngine</code>.
	 * @param tickable The tickable to submit.
	 */
	public void submit(final Tick tick) {
		ticks.add(tick);
	}	
	
}
World.java (make sure to import!):
Add
Code:
	private TickManager tickManager;
under
Code:
	private BlockingExecutorService backgroundLoader = new BlockingExecutorService(Executors.newSingleThreadExecutor());
then under
Code:
	public BlockingExecutorService getBackgroundLoader() {
		return backgroundLoader;
	}
add
Code:
	/**
	 * Gets the tickable manager.
	 * @return The tickable manager.
	 */
	public TickManager getTickManager() {
		return tickManager;
	}
under
Code:
			this.engine = engine;
add
Code:
			this.tickManager = new TickManager();
Then under
Code:
private void registerGlobalEvents() {
		submit(new GameProcess());
		submit(new CleanupEvent());
	}
add
Code:
	/**
	 * Submits a new tickable.
	 * @param tickable The tickable to submit.
	 */
	public void submit(final Tick tick) {
		submit(new Task() {
			@Override
			public void execute(GameEngine context) {
				// DO NOT REMOVE THIS CODE, IT PREVENTS CONCURRENT MODIFICATION
				// PROBLEMS!
				World.this.tickManager.submit(tick);
			}
		});
	}
How to use:
Code:
		World.getWorld().submit(new Tick(3) {
			@Override
			public void execute() {
				int cycle = 0;
				if (cycle != 3) {
					cycle++;
				}
				if (cycle == 3) {
					System.out.println("Hi!");
					cycle = 0;
					this.stop();
				}
			}
		});
	}




~[email protected]
  
Old
  (#2 (permalink))
Brittany is Offline
SG
image
 
Brittany's Avatar
 
Posts: 24,709
Cookies Given: 611
Cookies Received: 3,789
Time Online: 203 d 20 h 29 m
   
Default 01-24-2012, 12:07 PM

Great job Flowbro.

Can't wait to see more (:



Sometimes we just need a friend.
Someone to hold us when you can't stand.
Sometimes even to pretend like you matter for that split second that everything seems to be crashing down.
And sometimes it's just easier to say goodbye.

  
adsense code2.3
Closed Thread

Bookmarks

[PI] Hyperion's eventmanager converted to PI

Programming




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
adsense2.6
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -7. The time now is 12:00 AM.





Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2017, vBulletin Solutions, Inc.