Skip to content

Commit

Permalink
fairly drastic overhaul
Browse files Browse the repository at this point in the history
  • Loading branch information
Pomax committed May 18, 2012
1 parent 798636e commit 53acf3e
Show file tree
Hide file tree
Showing 12 changed files with 20,173 additions and 47 deletions.
17 changes: 11 additions & 6 deletions Actor.pde
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ abstract class Actor extends Positionable {
* Get the bounding box for this actor
*/
float[] getBoundingBox() {
if(active==null) return null;
float[] bounds = active.sprite.getBoundingBox();
bounds[0] += x+ox; bounds[1] += y+oy; // top left
bounds[2] += x+ox; bounds[3] += y+oy; // top right
Expand Down Expand Up @@ -255,6 +256,7 @@ abstract class Actor extends Positionable {
void drawObject() {
if(active!=null) {
active.draw();
/*
if(debug) {
noFill();
stroke(255,0,0);
Expand All @@ -268,17 +270,15 @@ abstract class Actor extends Positionable {
fill(255,0,0);
ellipse(0,0,5,5);
}
*/
}
}

// ====== KEY HANDLING ======

protected final boolean[] locked = new boolean[256];
protected final boolean[] keyDown = new boolean[256];
protected final int UP=38, LEFT=37, DOWN=40, RIGHT=39, // cursor keys
BUTTON_A=90, BUTTON_B=88; // jump + shoot

protected final int[] keyCodes = {UP, LEFT, DOWN, RIGHT, BUTTON_A, BUTTON_B};
protected int[] keyCodes = {};

// if pressed, and part of our known keyset, mark key as "down"
private void setIfTrue(int mark, int target) {
Expand All @@ -295,8 +295,7 @@ abstract class Actor extends Positionable {
// lock a key so that it cannot be triggered repeatedly
protected void ignore(int keyCode) {
locked[keyCode] = true;
keyDown[keyCode] = false;
}
keyDown[keyCode] = false; }

// handle key presses
void keyPressed(char key, int keyCode) {
Expand All @@ -308,6 +307,12 @@ abstract class Actor extends Positionable {
for(int i: keyCodes) {
unsetIfTrue(keyCode, i); }}

void mouseMoved(int mx, int my) {}
void mousePressed(int mx, int my, int button) {}
void mouseDragged(int mx, int my, int button) {}
void mouseReleased(int mx, int my, int button) {}
void mouseClicked(int mx, int my, int button) {}

// ====== ABSTRACT METHODS ======

// token implementation
Expand Down
4 changes: 3 additions & 1 deletion Boundary.pde
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ class Boundary extends Positionable {
* detach all actors from this boundary
*/
void detachAll() {
for(Positionable a: attached) {
while(attached.size() > 0) {
Positionable a = attached.remove(0);
a.detach(this);
}
attached.clear();
Expand Down Expand Up @@ -163,6 +164,7 @@ class Boundary extends Positionable {

// continue: get the current and previous bounding boxes
float[] prev = a.getBoundingBox();
if(prev==null) return null;
prev[0] -= dx; prev[1] -= dy;
prev[2] -= dx; prev[3] -= dy;
prev[4] -= dx; prev[5] -= dy;
Expand Down
4 changes: 2 additions & 2 deletions Interactor.pde
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ abstract class Interactor extends Actor {
return persistent || (vx-vw <= x && x <= vx+2*vw && vy-vh <= y && y <=vy+2*vh);
}

// Interactors don't get pickups
final void pickedUp(Pickup pickup) {}
// Interactors don't do anything with pickups by default
void pickedUp(Pickup pickup) {}

// Interactors are not playable
final void handleInput() {}
Expand Down
2 changes: 1 addition & 1 deletion Level.pde
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ abstract class Level {
Computer.arraylists("LevelLayer");
layerids = new HashMap<String, Integer>();
Computer.hashmaps("String","Integer");
viewbox = new ViewBox();
viewbox = new ViewBox(_width, _height);
}

/**
Expand Down
127 changes: 96 additions & 31 deletions LevelLayer.pde
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ abstract class LevelLayer {
ArrayList<Boundary> boundaries;
ArrayList<Drawable> fixed_background, fixed_foreground;
ArrayList<Pickup> pickups;
ArrayList<Pickup> npcpickups;
ArrayList<Decal> decals;
ArrayList<Interactor> interactors;
ArrayList<BoundedInteractor> bounded_interactors;
Expand Down Expand Up @@ -79,15 +80,27 @@ abstract class LevelLayer {
pickups.add(pickup);
bind(pickup);
if(javascript!=null) { javascript.addActor(); }}

void removeForPlayerOnly(Pickup pickup) {
pickups.remove(pickup);
if(javascript!=null) { javascript.removeActor(); }}

// The list of sprites that may only interact with non-players(s) (and boundaries)
void addForInteractorsOnly(Pickup pickup) {
npcpickups.add(pickup);
bind(pickup);
if(javascript!=null) { javascript.addActor(); }}

void removeForInteractorsOnly(Pickup pickup) {
npcpickups.remove(pickup);
if(javascript!=null) { javascript.removeActor(); }}

// The list of fully interacting non-player sprites
void addInteractor(Interactor interactor) {
interactors.add(interactor);
bind(interactor);
if(javascript!=null) { javascript.addActor(); }}

void removeInteractor(Interactor interactor) {
interactors.remove(interactor);
if(javascript!=null) { javascript.removeActor(); }}
Expand All @@ -97,6 +110,7 @@ abstract class LevelLayer {
bounded_interactors.add(bounded_interactor);
bind(bounded_interactor);
if(javascript!=null) { javascript.addActor(); }}

void removeBoundedInteractor(BoundedInteractor bounded_interactor) {
bounded_interactors.remove(bounded_interactor);
if(javascript!=null) { javascript.removeActor(); }}
Expand All @@ -106,9 +120,11 @@ abstract class LevelLayer {
players.add(player);
bind(player);
if(javascript!=null) { javascript.addActor(); }}

void removePlayer(Player player) {
players.remove(player);
if(javascript!=null) { javascript.removeActor(); }}

void updatePlayer(Player oldPlayer, Player newPlayer) {
int pos = players.indexOf(oldPlayer);
if (pos > -1) {
Expand Down Expand Up @@ -149,6 +165,8 @@ abstract class LevelLayer {
Computer.arraylists("Drawable");
pickups = new ArrayList<Pickup>();
Computer.arraylists("Pickup");
npcpickups = new ArrayList<Pickup>();
Computer.arraylists("Pickup");
decals = new ArrayList<Decal>();
Computer.arraylists("Decal");
interactors = new ArrayList<Interactor>();
Expand All @@ -170,6 +188,7 @@ abstract class LevelLayer {
yTranslate = oy;
xScale = sx;
yScale = sy;
nonstandard = (xScale!=1 || yScale!=1 || xTranslate!=0 || yTranslate!=0);
}

// used for statistics
Expand All @@ -187,6 +206,26 @@ abstract class LevelLayer {
return new float[]{vx, vy};
}

/**
* map a layer coordinate to "normal"
*/
float[] mapCoordinateFromScreen(float x, float y) {
float mx = map(x/xScale, 0,viewbox.w, viewbox.x,viewbox.x + viewbox.w);
float my = map(y/yScale, 0,viewbox.h, viewbox.y,viewbox.y + viewbox.h);
return new float[]{mx, my};
}

/**
* map a layer coordinate to "normal"
*/
float[] mapCoordinateToScreen(float x, float y) {
float mx = (x/xScale - xTranslate);
float my = (y/yScale - yTranslate);
mx *= xScale;
my *= yScale;
return new float[]{mx, my};
}

/**
*
*/
Expand All @@ -197,12 +236,10 @@ abstract class LevelLayer {
float[] mapped = mapCoordinate(viewbox.x,viewbox.y);
x = mapped[0];
y = mapped[1];
w = viewbox.w/xScale;
h = viewbox.h/yScale;

// cache the global coordinate transforms
nonstandard = (xScale!=1 || yScale!=1 || xTranslate!=0 || yTranslate!=0);
if(nonstandard) { pushMatrix(); }
w = viewbox.w / xScale;
h = viewbox.h / yScale;

pushMatrix();

// remember to transform the layer coordinates accordingly
translate(viewbox.x-x, viewbox.y-y);
Expand Down Expand Up @@ -267,6 +304,38 @@ abstract class LevelLayer {
}
}


// ---- npc pickups
if(showPickups) {
for(int i = npcpickups.size()-1; i>=0; i--) {
Pickup p = npcpickups.get(i);
if(p.remove) {
npcpickups.remove(i);
if(javascript!=null) { javascript.removeActor(); }
continue; }

// boundary interference?
if(p.interacting && !p.onlyplayerinteraction) {
for(Boundary b: boundaries) {
Computer.interact(b,p); }
for(BoundedInteractor o: bounded_interactors) {
if(o.bounding) {
for(Boundary b: o.boundaries) {
Computer.interact(b,p); }}}}

// npc interaction?
for(Interactor a: interactors) {
if(!a.interacting) continue;
float[] overlap = a.overlap(p);
if(overlap!=null) {
p.overlapOccurredWith(a);
break; }}

// draw pickup
p.draw(x,y,w,h);
}
}

// ---- non-player actors
if(showInteractors) {
for(int i = interactors.size()-1; i>=0; i--) {
Expand Down Expand Up @@ -394,41 +463,37 @@ abstract class LevelLayer {
}
}

if(nonstandard) { popMatrix(); }
popMatrix();
}

/**
* passthrough event
* passthrough events
*/
void keyPressed(char key, int keyCode) {
if(debug) {
if(key=='1') { showBackground = !showBackground; }
if(key=='2') { showBoundaries = !showBoundaries; }
if(key=='3') { showPickups = !showPickups; }
if(key=='4') { showInteractors = !showInteractors; }
if(key=='5') { showActors = !showActors; }
if(key=='6') { showForeground = !showForeground; }
if(key=='7') { showTriggers = !showTriggers; }
if(key=='8') {
for(Pickup p: pickups) { p.debug = !p.debug; }
for(Interactor i: interactors) { i.debug = !i.debug; }
for(Interactor i: bounded_interactors) { i.debug = !i.debug; }
for(Player p: players) { p.debug = !p.debug; }
}
}
for(Player a: players) {
a.keyPressed(key,keyCode); }}

/**
* passthrough event
*/
void keyReleased(char key, int keyCode) {
for(Player a: players) {
a.keyReleased(key,keyCode); }}

void mouseMoved(int mx, int my) {}
void mousePressed(int mx, int my, int button) {}
void mouseDragged(int mx, int my, int button) {}
void mouseReleased(int mx, int my, int button) {}
void mouseClicked(int mx, int my, int button) {}
void mouseMoved(int mx, int my) {
for(Player a: players) {
a.mouseMoved(mx,my); }}

void mousePressed(int mx, int my, int button) {
for(Player a: players) {
a.mousePressed(mx,my,button); }}

void mouseDragged(int mx, int my, int button) {
for(Player a: players) {
a.mouseDragged(mx,my,button); }}

void mouseReleased(int mx, int my, int button) {
for(Player a: players) {
a.mouseReleased(mx,my,button); }}

void mouseClicked(int mx, int my, int button) {
for(Player a: players) {
a.mouseClicked(mx,my,button); }}
}
3 changes: 3 additions & 0 deletions Positionable.pde
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ abstract class Positionable implements Drawable {

// which direction is this positionable facing,
// based on its movement in the last frame?
// -1 means "not set", 0-2*PI indicates the direction
// in radians (0 is ->, values run clockwise)
float direction = -1;

// administrative
Expand Down Expand Up @@ -306,6 +308,7 @@ abstract class Positionable implements Drawable {
float w=width, h=height, ow=other.width, oh=other.height;
float[] bounds = getBoundingBox();
float[] obounds = other.getBoundingBox();
if(bounds==null || obounds==null) return null;

if (false) {
drawBoundingBox(bounds);
Expand Down
4 changes: 3 additions & 1 deletion Sprite.pde
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,10 @@ class Sprite extends Positionable {
if (r != 0) { rotate(r); }
if(hflip) { scale(-1,1); }
if(vflip) { scale(1,-1); }
scale(sx,sy);
if(sx!=1 || sy!=1) { scale(sx,sy); }
translate(-width/2 + ox, -height/2 - oy);
// FIXME: the transforms may result in smoothing.
// I'm not sure how to prevent or correct for that.
image(img, 0, 0);
}
popMatrix();
Expand Down
17 changes: 12 additions & 5 deletions ViewBox.pde
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@ class ViewBox {

// viewbox values
float x=0, y=0, w=0, h=0;

ViewBox() {}
ViewBox(float _w, float _h) { w = _w; h = _h; }
ViewBox(float _x, float _y, float _w, float _h) { x = _x; y = _y; w = _w; h = _h; }
String toString() { return x+"/"+y+" - "+w+"/"+h; }

// current layer transform values
float llox=0, lloy=0, llsx=1, llsy=1;

// ye olde getterse
float getX() { return x-llox; }
float getY() { return y-lloy; }
Expand Down Expand Up @@ -51,13 +56,15 @@ class ViewBox {
// We leave things as they are so
// actors are positioned at scale*midpoint

// Get actor coordinates, transformed
// to screen coordinates.
float ax = who.getX(), ay = who.getY();
// Get actor coordinates, transformed to screen
// coordinates, and forced to integer values.
float ax = round(who.getX()),
ay = round(who.getY());

// Ideally the actor is in the center of the viewbox,
// but the level edges may require different positioning.
float idealx = ax - w/2, idealy = ay - h/2;
float idealx = ax - w/2,
idealy = ay - h/2;

// set values based on visual constraints
x = min( max(0,idealx), level.width - w );
Expand Down
Loading

0 comments on commit 53acf3e

Please sign in to comment.