Skip to content

Commit

Permalink
stjs #11
Browse files Browse the repository at this point in the history
- state creators
  • Loading branch information
Tapsi committed Dec 7, 2014
1 parent d9f8db5 commit 6eae506
Show file tree
Hide file tree
Showing 28 changed files with 2,273 additions and 2,118 deletions.
4 changes: 3 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions gameSrc/net/wolfTec/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,15 @@ public abstract class Constants {
static {
logHeaders.$put("unknown", "??????");
logHeaders.$put("xmlhttp", "XMLREQ");
logHeaders.$put("localization", " I18N");
logHeaders.$put("cwtMediator", "MEDITR");
logHeaders.$put("widgets", " GUI");
logHeaders.$put("renderer", "RENDER");
logHeaders.$put("assertion", "ASSERT");
logHeaders.$put("cwtMediator", "MEDITR");
logHeaders.$put("localization", " I18N");
logHeaders.$put("statemachine", "STATEM");
}

/**
*
* @param key
* @return
*/
Expand Down
27 changes: 11 additions & 16 deletions gameSrc/net/wolfTec/CustomWarsTactics.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
import net.wolfTec.model.Config;
import net.wolfTec.model.GameRound;
import net.wolfTec.network.MessageRouter;
import net.wolfTec.renderer.RenderingContext;
import net.wolfTec.renderer.Sprite;
import net.wolfTec.renderer.SpriteDatabase;
import net.wolfTec.renderer.TileVariantCalculator;
import net.wolfTec.states.GameRoundSetup;
import net.wolfTec.model.GameRoundSetup;
import net.wolfTec.states.StateData;
import net.wolfTec.states.Statemachine;
import net.wolfTec.utility.Audio;
Expand All @@ -28,7 +29,6 @@
import org.stjs.javascript.JSGlobal;
import org.stjs.javascript.dom.Element;
import org.stjs.javascript.functions.Callback1;
import org.stjs.javascript.stjs.STJS;

/**
* Central mediator (monolithic). Every service, data holder etc. can be accessed by this object. A direct access
Expand Down Expand Up @@ -154,7 +154,7 @@ public abstract class CustomWarsTactics {
/**
*
*/
private static final SpriteDatabase spriteDb;
public static final SpriteDatabase spriteDb;

/**
*
Expand All @@ -166,6 +166,8 @@ public abstract class CustomWarsTactics {
*/
public static final GameRoundSetup gameRoundSetup;

public static final RenderingContext renderCtx;

// Construction of the mediator
static {
Debug.logInfo(LOG_HEADER, "Initialize...");
Expand All @@ -187,16 +189,17 @@ public abstract class CustomWarsTactics {
actionInvoker = new ActionInvoker(Constants.ACTION_POOL_SIZE);
variantCalculator = new TileVariantCalculator();
netMessageRouter = new MessageRouter();
loadingHandler = new LoadingHandler();
gameRoundSetup = new GameRoundSetup();
gameWorkflowData = new StateData();
renderCtx = new RenderingContext();
gameWorkflow = new Statemachine();
inputHandler = new InputHandler();
ai = new AiHandler();
loadingHandler = new LoadingHandler();
spriteDb = new SpriteDatabase();
audioHandler = new Audio();
features = new Features();
i18n = new Localization();
spriteDb = new SpriteDatabase();
gameWorkflowData = new StateData();
gameRoundSetup = new GameRoundSetup();
ai = new AiHandler();

Debug.logInfo(LOG_HEADER, "Setup input backends");
inputHandler.backends.$put("keyboards",
Expand Down Expand Up @@ -307,14 +310,6 @@ public static void resetConfiguration() {
gameConfigNames.forEach(resetConfigObject);
}

public static void registeredSprite (String id, int numberOfSlots) {
spriteDb.sprites.$put(id, new Sprite(numberOfSlots));
}

public static Sprite getSprite (String id) {
return spriteDb.sprites.$get(id);
}

public static void main(String[] args) {
Debug.logInfo(LOG_HEADER, "Starting CustomWars: Tactics " + Constants.VERSION);
}
Expand Down
9 changes: 6 additions & 3 deletions gameSrc/net/wolfTec/database/Database.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.wolfTec.database;

import net.wolfTec.Constants;
import net.wolfTec.utility.Debug;
import org.stjs.javascript.Array;
import org.stjs.javascript.JSCollections;
Expand All @@ -12,6 +13,8 @@
*/
public abstract class Database<T extends ObjectType> {

public static final String LOG_HEADER = Constants.logHeader("database.sheet");

/**
* Holds all type sheet objects.
*/
Expand All @@ -32,18 +35,18 @@ public void registerSheetByString(String data) {
}

public void registerSheetByObject(T type) {
Debug.logInfo("Validating sheet " + type.ID);
Debug.logInfo(LOG_HEADER, "Validating sheet " + type.ID);
type.validate();

Debug.logInfo("Register sheet with ID " + type.ID);
Debug.logInfo(LOG_HEADER, "Register sheet with ID " + type.ID);
this.sheets.$put(type.ID, type);
this.types.push(type.ID);
}

public abstract T parseJSON(String data);

/**
* @param key id of the sheet
* @param sheetId id of the sheet
* @return sheet object
*/
public T getSheet(String sheetId) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.wolfTec.states;
package net.wolfTec.model;

import net.wolfTec.Constants;
import net.wolfTec.CustomWarsTactics;
Expand Down
100 changes: 100 additions & 0 deletions gameSrc/net/wolfTec/renderer/LayeredCanvas.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,104 @@
package net.wolfTec.renderer;

import net.wolfTec.Constants;
import org.stjs.javascript.Array;
import org.stjs.javascript.Global;
import org.stjs.javascript.JSCollections;
import org.stjs.javascript.dom.Canvas;
import org.stjs.javascript.dom.canvas.CanvasRenderingContext2D;

public class LayeredCanvas {

private Canvas cv;
private CanvasRenderingContext2D ctx;
public int w;
public int h;
private Array<CanvasRenderingContext2D> contexts;
private Array<Canvas> layers;

public LayeredCanvas (String canvasId, int frames, int w, int h) {

// root canvas
this.cv = (Canvas) Global.window.document.getElementById(canvasId);
this.cv.width = w;
this.cv.height = h;
this.ctx = this.cv.getContext("2d");
this.w = w;
this.h = h;

// cached layers
if (frames > 0) {
this.contexts = JSCollections.$array();
this.layers = JSCollections.$array();

int n = 0;
while (n < frames) {
Canvas cv = (Canvas) Global.window.document.createElement("canvas");

cv.width = w;
cv.height = h;

this.contexts.$set(n, cv.getContext("2d"));
this.layers.$set(n, cv);

n++;
}
}
}

/**
*
* @param index
*/
public void renderLayer (int index) {
CanvasRenderingContext2D ctx = this.getContext(Constants.INACTIVE_ID);
ctx.clearRect(0, 0, this.w, this.h);
ctx.drawImage(getLayer(index), 0, 0, this.w, this.h);
}

/**
*
* @param {number?} index
* @returns {HTMLCanvasElement}
*/
public Canvas getLayer (int index) {
if (index == Constants.INACTIVE_ID) {
return this.cv;
}

return this.layers.$get(index);
}

/**
*
* @param index
*/
public void clear (int index) {
this.getContext(index).clearRect(0, 0, this.w, this.h);
}

/**
*
*/
public void clearAll () {
int n = this.layers.$length() - 1;
while (n >= 0) {
this.clear(n);
n--;
}
this.clear(Constants.INACTIVE_ID);
}

/**
*
* @param {number?} index
* @returns {CanvasRenderingContext2D}
*/
public CanvasRenderingContext2D getContext (int index) {
if (index == Constants.INACTIVE_ID) {
return this.ctx;
}

return this.contexts.$get(index);
}
}
51 changes: 51 additions & 0 deletions gameSrc/net/wolfTec/renderer/Pagination.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package net.wolfTec.renderer;

import org.stjs.javascript.Array;
import org.stjs.javascript.JSCollections;
import org.stjs.javascript.functions.Callback0;

public class Pagination {

public int page;
public final Array<Object> list;
public final Array<Object> entries;
public final Callback0 updateFn;

public Pagination (Array<Object> list, int pageSize, Callback0 updateFn) {
this.page = 0;
this.list = list;

this.entries = JSCollections.$array();
while (pageSize > 0) {
this.entries.push(null);
pageSize--;
}

this.updateFn = updateFn;
}

/**
* Selects a page from the list. The entries of the selected page will be saved in the **entries** property
* of the pagination object.
*
* @param index
*/
public void selectPage (int index) {
int PAGE_SIZE = this.entries.$length();

if (index < 0 || index * PAGE_SIZE >= this.list.$length()) {
return;
}

this.page = index;

index = (index * PAGE_SIZE);
for (int n = 0; n < PAGE_SIZE; n++) {
this.entries.$set(n, index + n >= this.list.$length() ? null : this.list.$get(index + n));
}

if (this.updateFn != null) {
this.updateFn.$invoke();
}
}
}
Loading

0 comments on commit 6eae506

Please sign in to comment.