/*
* Datei: TetrisMaster.java
* Autor(en): Lukas König
* Java-Version: 6.0
* Erstellt: ??.06.2010
*
* (c) This file and the EAS (Easy Agent Simulation) framework containing it
* is protected by Creative Commons by-nc-sa license. Any altered or
* further developed versions of this file have to meet the agreements
* stated by the license conditions.
*
* In a nutshell
* -------------
* You are free:
* - to Share -- to copy, distribute and transmit the work
* - to Remix -- to adapt the work
*
* Under the following conditions:
* - Attribution -- You must attribute the work in the manner specified by the
* author or licensor (but not in any way that suggests that they endorse
* you or your use of the work).
* - Noncommercial -- You may not use this work for commercial purposes.
* - Share Alike -- If you alter, transform, or build upon this work, you may
* distribute the resulting work only under the same or a similar license to
* this one.
*
* + Detailed license conditions (Germany):
* http://creativecommons.org/licenses/by-nc-sa/3.0/de/
* + Detailed license conditions (unported):
* http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en
*
* This header must be placed in the beginning of any version of this file.
*/
package eas.users.demos.tetris;
import java.util.List;
import eas.miscellaneous.StaticMethods;
import eas.miscellaneous.system.windowFrames.GeneralDialog;
import eas.plugins.masterScheduler.AbstractDefaultKeyEventMaster;
import eas.plugins.masterScheduler.KeyEventFilter;
import eas.plugins.standard.visualization.AllroundVideoPlugin;
import eas.plugins.standard.visualization.chartPlugin.AllroundChartPlugin;
import eas.simulation.Wink;
import eas.startSetup.ParCollection;
/**
* @author Lukas König
*/
public class TetrisMaster extends AbstractDefaultKeyEventMaster<TetrisEnvironment> {
/**
*
*/
private static final long serialVersionUID = -1216561543313818108L;
private int minus = 56;
private int pause = 500 + this.minus;
private int stepsPause = 130;
@Override
public TetrisEnvironment[] generateRunnables(ParCollection params) {
return new TetrisEnvironment[] {new TetrisEnvironment(0, params)};
}
@Override
public String id() {
return eas.simulation.ConstantsSimulation.DEFAULT_MASTER_SCHEDULER_ID + "-tetris";
}
@Override
public void runDuringSimulation(
TetrisEnvironment umg,
Wink simZyk,
ParCollection params) {
super.runDuringSimulation(umg, simZyk, params);
if (simZyk.getLastTick() == 0) {
vidPlug.getLiveWindow().setAlwaysOnTop(true);
vidPlug.getLiveWindow().setSize(500, 666);
vidPlug.setClickPosition(10, 10);
}
try {
/*
* Sleep 5 times for one fifth of the pause time. This reduces
* waiting time after a key event compared to waiting once the
* whole pause time.
*/
for (int i = 0; i < 5; i++) {
Thread.sleep(this.pause / 5);
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
if (simZyk.getLastTick() % this.stepsPause == 0) {
this.pause -= this.minus;
umg.setPause(this.pause);
this.minus -= 3;
if (this.minus < 0) {
this.minus = 0;
}
}
}
AllroundVideoPlugin vidPlug;
@Override
public void runBeforeSimulation(
TetrisEnvironment umg,
ParCollection params) {
super.runBeforeSimulation(umg, params);
vidPlug = (AllroundVideoPlugin) umg.getSimTime().getPluginObject(new AllroundVideoPlugin().id());
umg.getSimTime().requestEvents(vidPlug, new KeyEventFilter());
}
@Override
public void runAfterSimulation(TetrisEnvironment umg, ParCollection params) {
super.runAfterSimulation(umg, params);
umg.broadcastRemainingChartEvents(umg.getSimTime().getCurrentTime());
GeneralDialog dia = new GeneralDialog(
null,
null,
"Game over - ranking: "
+ umg.getPlace()
+ " - points: "
+ (umg.getPoints()),
GeneralDialog.OK_BUTT, "Your name?",
1,
40,
true);
dia.setVisible(true);
umg.getScorez().put(umg.getPoints(), dia.getText());
umg.storeScorez();
// for (long p : this.scorez.keySet()) {
// StaticMethods.log(StaticMethods.LOG_INFO, "\n(" + i + ") "
// + this.scorez.get(p) + " - " + p, this.getParCollection(),
// "plain", null);
// i--;
// }
StaticMethods.log(
StaticMethods.LOG_INFO,
"Game over! " + (umg.getPoints()) + " points.",
params,
"",
umg.getPoints());
GeneralDialog dia2 = new GeneralDialog(null, null,
"HIGHSCOREZ - ranking: " + umg.getPlace() + " - points: "
+ (umg.getPoints()), GeneralDialog.OK_BUTT, umg.scorezString(),
30, 60, false);
dia2.setVisible(true);
}
@Override
public List<String> getSupportedPlugins() {
List<String> list = super.getSupportedPlugins();
list.add(new AllroundChartPlugin().id());
return list;
}
}