import java.util.ArrayList; import java.awt.Rectangle; public class TrainerField extends BApplet { Avatar avatar; float xAngle, yAngle; final int BOARD_SIZE = 400; boolean[] pressed = new boolean[128]; ArrayList blocks; ArrayList grubs; ArrayList tokens; ArrayList helpStrings; ArrayList startScreenOptions; int lives; int time; int startScreenOption = 0; int state; int level; int levelStartMillis; final int GAME = 0; final int HELP_SCREEN = 1; final int START_SCREEN = 3; final int GAME_OVER = 4; void setup() { framerate(24); size(600, 400); noSmooth(); state = START_SCREEN; tokens = new ArrayList(); grubs = new ArrayList(); blocks = new ArrayList(); helpStrings = new ArrayList(); helpStrings.add("use w, a, s, and d to move"); helpStrings.add(""); helpStrings.add("press space to jump"); helpStrings.add(""); helpStrings.add("drag to rotate board"); helpStrings.add(""); helpStrings.add("collect the tokens"); helpStrings.add(" by moving through them"); helpStrings.add(""); helpStrings.add("press escape to quit"); startScreenOptions = new ArrayList(); startScreenOptions.add("start game"); startScreenOptions.add("view help"); } void loop() { background(90); switch(state) { case START_SCREEN: drawStartScreen(); if(pressed[UP] && startScreenOption > 0) { startScreenOption--; pressed[UP] = false; } if(pressed[DOWN] && startScreenOption < startScreenOptions.size()-1) { startScreenOption++; pressed[DOWN] = false; } if(pressed[10]) { pressed[10] = false; if(startScreenOption == GAME) { level = 1; lives = 4; startLevel(level); } state = startScreenOption; } break; case HELP_SCREEN: float y=30; stroke(255); for(int i=0; i 0 && key < pressed.length) { pressed[key] = true; } } void keyReleased() { if(key > 0 && key < pressed.length) { pressed[key] = false; } } void drawStartScreen() { stroke(255); spell(60, 100, "welcome to the game", 20, 25); for(int i=0; i tangent) { angle -= (angle-tangent)/10; } if(velocity < MAX_VELOCITY) { velocity+= 0.5; } x += velocity * sin(radians(angle)); y += velocity * cos(radians(angle)); } public void draw() { stroke(0); fill(50, 200, 200); push(); translate(x, y, z); rotate(radians(-angle)); beginShape(TRIANGLE_STRIP); vertex(0, 0, 0); vertex(-w/2, h/2, wingz); vertex(-w/2, -h/2, wingz); endShape(); beginShape(TRIANGLE_STRIP); vertex(0, 0); vertex(w/2, h/2, wingz); vertex(w/2, -h/2, wingz); endShape(); pop(); if(destination != null) { //line(destination.getX(), destination.getY(), this.z, x, y, this.z); } } } class Block { float x, y, z, w, h, depth; color c; Rectangle r; public Block(float x, float y, float z, float w, float h, float depth, color c) { this.x = x; this.y = y; this.z = z; this.w = w; this.h = h; this.depth = depth; this.c = c; this.r = new Rectangle((int)x, (int)y, (int)w, (int)h); } public boolean hitXY(float x, float y) { boolean inY = (y > this.y-(h/2) && y < this.y+(h/2)); boolean inX = (x > this.x-(w/2) && x < this.x+(w/2)); return (inX && inY); } public boolean hitZ(float z) { return (z > this.z-(depth/2) && z < this.z+(depth/2)); } public boolean hit(float x, float y, float z) { return (hitXY(x, y) && hitZ(z)); } public void draw() { stroke(40, 40, 40); fill(c); push(); translate(x, y, z); box(w, h, depth); pop(); } } class Point3D { float x, y, z, angle; public Point3D(float x, float y, float z) { this.x = x; this.y = y; this.z = z; this.angle = 0; } public Point3D(float x, float y, float z, float angle) { this.x = x; this.y = y; this.z = z; this.angle = angle; } public float getX() { return x; } public float setX(float x) { this.x = x; return x; } public float getY() { return y; } public float setY(float y) { this.y = y; return y; } public float getZ() { return z; } public float setZ(float Z) { this.z = z; return z; } public float getAngle() { return angle; } public float rotate(float inc) { angle += inc; return angle; } } class Avatar { float x, y, z, angle, velocity, spin, zvel, w, h, d; final float MAX_VELOCITY = 5; // pixels final float MAX_SPIN = 8; // degrees final float VELOCITY_INCREMENT = 0.5; final float SPIN_INCREMENT = 4; ArrayList trail; boolean jumping; public Avatar(float x, float y, float z) { this.x = x; this.y = y; this.z = z; this.velocity = 0; this.angle = 0; this.spin = 0; this.zvel = 0; this.trail = new ArrayList(); this.jumping = true; this.w = 12; this.h = 30; this.d = 10; } public void senseKeys() { // Update the angle of the avatar. if((pressed['a']||pressed[37]) && spin < MAX_SPIN) { spin += SPIN_INCREMENT; } if((pressed['d']||pressed[39]) && spin > -MAX_SPIN) { spin -= SPIN_INCREMENT; } if((pressed['w']||pressed[38]) && velocity < MAX_VELOCITY) { if(jumping) { velocity += VELOCITY_INCREMENT/2; } else { velocity += VELOCITY_INCREMENT; } } if((pressed['s']||pressed[40]) && velocity > -MAX_VELOCITY) { if(jumping) { velocity -= VELOCITY_INCREMENT/2; } else { velocity -= VELOCITY_INCREMENT; } } if(pressed[' '] && !jumping) { jumping = true; zvel+=6; } } public void doFriction() { if(velocity > 0) { velocity -= VELOCITY_INCREMENT/2; } if(velocity < 0) { velocity += VELOCITY_INCREMENT/2; } if(spin > 0) { spin -= SPIN_INCREMENT/2; } if(spin < 0) { spin += SPIN_INCREMENT/2; } } public void move() { doFriction(); senseKeys(); angle += radians(spin); float newx = x + sin(angle) * velocity; float newy = y + cos(angle) * velocity; float newz = z + zvel; boolean hit = false; for(int i=0; i < blocks.size() && !hit; i++) { Block b = (Block)blocks.get(i); if(b.hit(newx, newy, newz)) { hit = true; jumping = false; if(z >= b.z+(b.depth/2)) { zvel = 0; newz = b.z+(b.depth/2); } else { newx = x; newy = y; newz = z; velocity *= -1; } } } for(int i=0; i -10) { zvel -= VELOCITY_INCREMENT; } x = newx; y = newy; z = newz; if(abs(velocity) > 0.3 || abs(zvel) > 0.3) { trail.add(new Point3D(x,y,z)); if(trail.size() > 30) { trail.remove(0); } } if(z < -300) { lives--; reset(); } if(z > 200) { zvel = -1; } } public void draw() { push(); translate(x, y, z); rotate(-angle); stroke(100); fill(255); beginShape(TRIANGLE_STRIP); vertex(0, 0, 0); vertex(w/2, -(h/2), 0); vertex(0, -d, d); vertex(0, 0, 0); vertex(-(w/2), -(h/2), 0); vertex(0, -d, d); vertex(0, -d, d); vertex(-(w/2), -(h/2), 0); vertex((w/2), -(h/2), 0); endShape(); pop(); for(int i=0; i