Permalink
Browse files

started work on the API and tutorials

  • Loading branch information...
Pomax committed May 28, 2012
1 parent 43c300f commit e56ecfb903d399c0cf17150f364276f1c64ad0a6
View
@@ -192,7 +192,7 @@ abstract class Actor extends Positionable {
* surfaces.
*/
void attachTo(Boundary boundary, float[] correction) {
- println("attaching to boundary "+boundary);
+// println("attaching to boundary "+boundary);
// record attachment
boundaries.add(boundary);
@@ -343,6 +343,22 @@ abstract class Actor extends Positionable {
locked[keyCode] = true;
keyDown[keyCode] = false; }
+ // add a key listener
+ protected void handleKey(char key) {
+ int keyCode = int(key),
+ len = keyCodes.length;
+ int[] _tmp = new int[len+1];
+ arrayCopy(keyCodes,0,_tmp,0,len);
+ _tmp[len] = keyCode;
+ keyCodes = _tmp;
+ }
+
+ // check whether a key is pressed or not
+ protected boolean isKeyDown(char key) {
+ int keyCode = int(key);
+ return keyDown[keyCode];
+ }
+
// handle key presses
void keyPressed(char key, int keyCode) {
for(int i: keyCodes) {
View
@@ -134,7 +134,7 @@ class Boundary extends Positionable {
proximity = dotproduct;
}
}
- println("dot-proximity to boundary: "+proximity);
+// println("dot-proximity to boundary: "+proximity);
return abs(proximity) < 0.01;
}
View
@@ -21,9 +21,7 @@ static class CollisionDetection {
*/
static void interact(Boundary b, Actor a)
{
- if (a.isAttachedTo(b)) {
- println("we don't check our own boundary");
- return; }
+ if (a.isAttachedTo(b)) { return; }
float[] correction = blocks(b,a);
if(correction != null) { a.attachTo(b, correction); }
@@ -130,13 +128,13 @@ static class CollisionDetection {
// cases (1) or (5)?
B_sum = B_current + B_previous;
if (B_sum == 0 || B_sum == 8) {
- println("case " + (B_sum==8? "1" : "5"));
+// println("case " + (B_sum==8? "1" : "5"));
return null;
}
// case (4)?
if (B_previous < 4 && B_current == 0) {
- println("case 4");
+// println("case 4");
return null;
}
@@ -165,15 +163,17 @@ static class CollisionDetection {
// if we're seeing case (3), set up bbox correctly.
if (B_current == 0) {
- println("case 3");
+// println("case 3");
// two of these edges are guaranteed to not have intersections,
// since otherwise the intersection would be inside either
// [previous] or [current], which is case (2) instead.
bbox = new float[]{previous[(corner+2)%8],previous[(corner+3)%8],
previous[(corner+6)%8],previous[(corner+7)%8],
current[(corner+6)%8], current[(corner+7)%8],
current[(corner+2)%8], current[(corner+3)%8]}; }
- else { println("case 2"); }
+ else {
+// println("case 2");
+ }
// Now that we have the correct box, perform line/line
// intersection detection for each edge on the box.
@@ -206,7 +206,7 @@ static class CollisionDetection {
float[] i1 = intersections.get(0),
i2 = intersections.get(1);
- println("***");
+// println("***");
float[] ideal = getLineLineIntersection(px,py,cx,cy, i1[0],i1[1],i2[0],i2[1], false);
if (ideal == null) {
/*
@@ -225,7 +225,7 @@ static class CollisionDetection {
println("translated: "+tr[0]+","+tr[1]+","+tr[2]+","+tr[3]+","+tr[4]+","+tr[5]+","+tr[6]+","+tr[7]+","+tr[8]);
exit();
*/
- println("error could not find the ideal point");
+// println("error could not find the ideal point");
return new float[]{px-cx, py-cy};
}
return new float[]{ideal[0]-cx, ideal[1]-cy};
@@ -239,7 +239,7 @@ static class CollisionDetection {
}
// Uncaught cases get a pass - with a warning.
- println("unknown case! (B_current: "+B_current+", B_previous: "+B_previous+")");
+// println("unknown case! (B_current: "+B_current+", B_previous: "+B_previous+")");
return null;
}
View
@@ -0,0 +1,69 @@
+/**
+ * This encodes all the boilerplate code
+ * necessary for level drawing and input
+ * handling.
+ */
+
+// global levels container
+HashMap<String, Level> levelSet;
+
+// global 'currently active' level
+Level activeLevel = null;
+
+// setup sets up the screen size, and level container,
+// then calls the "initialize" method, which you must
+// implement yourself.
+void setup() {
+ size(screenWidth, screenHeight);
+ noLoop();
+ levelSet = new HashMap<String, Level>();
+ SpriteMapHandler.init(this);
+ SoundManager.init(this);
+ initialize();
+}
+
+// draw loop
+void draw() { activeLevel.draw(); }
+
+// event handling
+void keyPressed() { activeLevel.keyPressed(key, keyCode); }
+void keyReleased() { activeLevel.keyReleased(key, keyCode); }
+void mouseMoved() { activeLevel.mouseMoved(mouseX, mouseY); }
+void mousePressed() { activeLevel.mousePressed(mouseX, mouseY, mouseButton); }
+void mouseDragged() { activeLevel.mouseDragged(mouseX, mouseY, mouseButton); }
+void mouseReleased() { activeLevel.mouseReleased(mouseX, mouseY, mouseButton); }
+void mouseClicked() { activeLevel.mouseClicked(mouseX, mouseY, mouseButton); }
+
+/**
+ * Levels are added to the game through this function.
+ */
+void addLevel(String name, Level level) {
+ levelSet.put(name, level);
+ if (activeLevel == null) {
+ activeLevel = level;
+ loop();
+ }
+}
+
+/**
+ * We switch between levels with this function.
+ *
+ * Because we might want to move things from the
+ * old level to the new level, this function gives
+ * you a reference to the old level after switching.
+ */
+Level setActiveLevel(String name) {
+ Level oldLevel = activeLevel;
+ activeLevel = levelSet.get(name);
+ return oldLevel;
+}
+
+/**
+ * Levels can be removed to save memory, etc.
+ * as long as they are not the active level.
+ */
+void removeLevel(String name) {
+ if (levelSet.get(name) != activeLevel) {
+ levelSet.remove(name);
+ }
+}
View
@@ -148,6 +148,13 @@ abstract class LevelLayer {
// level viewbox
ViewBox viewbox;
+ /**
+ * fallthrough constructor
+ */
+ LevelLayer(Level p) {
+ this(p, p.width, p.height);
+ }
+
/**
* Constructor
*/
View
@@ -53,7 +53,7 @@ abstract class Positionable extends Position implements Drawable {
}
boolean isAttachedTo(Boundary b) {
- println("attached to b? " + boundaries.size() + " attachments found.");
+// println("attached to b? " + boundaries.size() + " attachments found.");
return boundaries.contains(b);
}
@@ -282,7 +282,7 @@ abstract class Positionable extends Position implements Drawable {
// we're attached to one or more boundaries, so we
// are subject to (compound) impulse redirection.
if(boundaries.size()>0) {
- println("redirecting impulse {"+_dx+","+_dy+"} over boundary surfaces...");
+// println("redirecting impulse {"+_dx+","+_dy+"} over boundary surfaces...");
float[] redirected = new float[]{_dx, _dy};
for(int b=boundaries.size()-1; b>=0; b--) {
Boundary boundary = boundaries.get(b);
@@ -291,20 +291,18 @@ abstract class Positionable extends Position implements Drawable {
continue;
}
redirected = boundary.redirectForce(redirected[0], redirected[1]);
- println("redirected to {"+redirected[0]+","+redirected[1]+"}.");
+// println("redirected to {"+redirected[0]+","+redirected[1]+"}.");
}
x += redirected[0];
y += redirected[1];
}
ix *= ixF;
iy *= iyF;
-
- // Not unimportant: round the impulse to two significant decimals,
- // or these values don't stabilise for about 100 frames even though
- // on-screem it looks like we're standing still.
- ix = round(100*ix)/100;
- iy = round(100*iy)/100;
+
+ // Not unimportant: cutoff resolution.
+ if(abs(ix) < 0.01) { ix = 0; }
+ if(abs(iy) < 0.01) { iy = 0; }
}
// implemented by subclasses
View
@@ -21,11 +21,16 @@ static class SoundManager {
public static PImage volume_overlay;
+ static void init(PApplet sketch, String resourceLocation) {
+ DIR = resourceLocation;
+ init(sketch);
+ }
+
static void init(PApplet sketch) {
owners = new HashMap<Object,AudioPlayer>();
audioplayers = new HashMap<String,AudioPlayer>();
- mute_overlay = sketch.loadImage("graphics/mute.gif");
- unmute_overlay = sketch.loadImage("graphics/unmute.gif");
+ mute_overlay = sketch.loadImage("mute.gif");
+ unmute_overlay = sketch.loadImage("unmute.gif");
volume_overlay = (muted ? unmute_overlay : mute_overlay);
minim = new Minim(sketch);
reset();
View
@@ -13,7 +13,7 @@ static class SpriteMapHandler {
* This method must be called before cutTiledSpriteSheet can be used.
* Typically this involves calling setSketch(this) in setup().
*/
- static void setSketch(PApplet s) {
+ static void init(PApplet s) {
globalSketch = s;
}
View
@@ -6,12 +6,20 @@ class State {
Sprite sprite;
Actor actor;
+ // this point is considered the "center point" when
+ // swapping between states. If a state has an anchor
+ // at (3,3), mapping to world coordinate (240,388)
+ // and it swaps for a state that has (10,10) as anchor,
+ // the associated actor is moved (-7,-7) to effect
+ // the anchor being in the same place before and after.
+ float ax, ay;
+
// shortcut constructor
State(String name, String spritesheet) {
this(name, spritesheet, 1, 1);
}
- // full constructor
+ // bigger shortcut constructor
State(String _name, String spritesheet, int rows, int cols) {
name = _name;
sprite = new Sprite(spritesheet, rows, cols);
View
Binary file not shown.
Oops, something went wrong.

0 comments on commit e56ecfb

Please sign in to comment.