📄 gamecontroller.java
字号:
package net.sf.jawp.gf.system;
import java.util.Timer;
import java.util.TimerTask;
import net.sf.jawp.gf.api.domain.GameWorld;
import net.sf.jawp.gf.api.services.UserService;
import net.sf.jawp.gf.domain.GameWorldBase;
import net.sf.jawp.gf.domain.GameWorldRO;
import net.sf.jawp.gf.persistence.PersistenceController;
import net.sf.jawp.gf.server.UserServiceImpl;
import net.sf.jawp.gf.system.transaction.DoStepTransaction;
/**
* main class for controlling single game
* @author jarek
* @version $Revision: 1.26 $
* @param <GAME> game type
* @param <GAMEVIEW> read only view of game
* @param <GAMESERVICE> service definition for game
*/
public class GameController<GAMESERVICE , GAME extends GameWorldBase<GAMEVIEW>, GAMEVIEW extends GameWorldRO>
{
private final PersistenceController<RootSystem<GAME, GAMEVIEW, GAMESERVICE>, RootSystemUnmodifiable<GAMEVIEW> > persistence;
private final UserService<GAMESERVICE> userService;
private GamesTimer timer;
public GameController( final String gameStore, final boolean load)
{
this.persistence = new PersistenceController<RootSystem<GAME, GAMEVIEW, GAMESERVICE> , RootSystemUnmodifiable<GAMEVIEW> >();
if ( load && isStoredVersionAvailable(gameStore))
{
this.persistence.loadSystem( gameStore);
}
else
{
if (isStoredVersionAvailable( gameStore) )
{
this.persistence.disposeSystem(gameStore);
}
final RootSystem<GAME, GAMEVIEW, GAMESERVICE> rs = createInitialSystem();
this.persistence.initSystem( rs, gameStore);
}
this.userService = new UserServiceImpl<GAMESERVICE, GAME, GAMEVIEW>( this.persistence);
}
private boolean isStoredVersionAvailable(final String gameStore)
{
return PersistenceController.isStoredSystemAvailable(gameStore);
}
public final UserService<GAMESERVICE> getUserService()
{
return this.userService;
}
public final void close()
{
this.stopTimer();
this.persistence.closeSystem();
}
public final void dispose()
{
this.stopTimer();
this.persistence.disposeSystem();
}
protected RootSystem<GAME, GAMEVIEW, GAMESERVICE> createInitialSystem()
{
return new RootSystem<GAME, GAMEVIEW, GAMESERVICE>();
}
public final synchronized void startTimer()
{
this.timer = new GamesTimer(this);
this.timer.start();
}
public final synchronized void stopTimer()
{
if ( this.timer != null)//timer can be not started
{
this.timer.finish();
}
this.timer = null;
}
public final boolean isRunning()
{
return this.timer != null;
}
public final void doStep(final long timeSlice)
{
final DoStepTransaction<GAME, GAMEVIEW, GAMESERVICE> step = new DoStepTransaction<GAME, GAMEVIEW, GAMESERVICE>(timeSlice);
this.persistence.runCommand(step);
doInternalAI(timeSlice);
}
private void doInternalAI(final long timeSlice)
{
for (final GameWorld gw : this.persistence.getSystemObject().getGameWorlds() )
{
final GAME game = this.persistence.getSystemObject().findGame( gw.getKey());
doInternalAI( game);
}
}
protected void doInternalAI( final GAME game)
{
}
public final int getNumberOfUsers()
{
return this.persistence.getUnmodifiableObject().getNumberOfUsers();
}
public final int getNumberOfGames()
{
return this.persistence.getUnmodifiableObject().getNumberOfGames();
}
protected final PersistenceController<RootSystem<GAME, GAMEVIEW, GAMESERVICE>, RootSystemUnmodifiable<GAMEVIEW> >
getPersistenceController()
{
return this.persistence;
}
/**
* simple thread that calls doStep transaction
*
* @author jarek
* @version $Revision: 1.26 $
*
*/
private static final class GamesTimer
{
private Timer timer = null;
private GameController<?, ?, ?> gameController;
private long period = 1000; //default value
GamesTimer(final GameController<?, ?, ?> gameController)
{
timer = new Timer("Timer: " + gameController.toString(), false);
this.gameController = gameController;
}
public void start()
{
final TimerTask stepTask = new TimerTask()
{
public void run()
{
gameController.doStep(period);
}
};
timer.schedule(stepTask, 10, period);
}
public void finish()
{
timer.cancel();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -