package org.nyu.mocap.anmotion.processing.simplegame;
import java.util.ArrayList;
import org.nyu.mocap.anmotion.java.Player;
import org.nyu.mocap.anmotion.protocol.Instruction;
import org.nyu.mocap.anmotion.protocol.InstructionType;
import processing.core.PApplet;
public class SimpleGamePlayer implements Player {
public static final int MIN_Y_MOVEMENT = -20;
public static final int MAX_BULLET_COUNT = 10;
public static final int SCORE_TO_WON = 1000;
private SimpleGame game;
private String name;
private int x;
private int y;
private ArrayList<Bullet> bullets;
private int width;
private int height;
private int score;
private int lives;
private boolean won;
private int color;
public SimpleGamePlayer(SimpleGame game, String name) {
this.game = game;
this.name = name;
width = 40;
height = 20;
color = game.color(game.random(255),game.random(255),game.random(255));
this.y = game.getH() - height;
this.x = (game.getW() - this.width) / 2;
reset();
}
public void reset(){
bullets = new ArrayList<Bullet>();
score = 0;
lives = 5;
won = false;
}
public boolean isAlive(){
return lives>0;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public int getLives() {
return lives;
}
public void setLives(int lives) {
this.lives = lives;
}
public void setName(String name) {
this.name = name;
}
public void draw() {
// draw the player
game.fill(color);
game.pushMatrix();
game.translate(x, y);
game.rect(0, 0, width, height);
game.popMatrix();
// draw the bullet
for (int i = 0; i < bullets.size(); i++) {
Bullet b = bullets.get(i);
b.draw();
b.y -= b.speed;
if (b.y < 0) {
bullets.remove(i);
}
}
drawInfo();
}
@Override
public String getName() {
// TODO Auto-generated method stub
return null;
}
@Override
public void moveBy(int dx, int dy) {
// TODO Auto-generated method stub
if(!isAlive())
return;
this.x += dx;
// this.y +=dy;
if (this.x < 0)
this.x = 0;
if (this.x > game.width - this.width)
this.x = game.width - this.width;
if (dy < MIN_Y_MOVEMENT) {
addBullet(MIN_Y_MOVEMENT - dy);
}
}
public void addBullet(int speed) {
if (bullets.size() == MAX_BULLET_COUNT)
return;
int color = game.color(game.random(255), game.random(255), game
.random(255));
Bullet bullet = new Bullet(color, this.x, game.height, speed);
bullets.add(bullet);
game.synth.zap(7);
}
public void shoot() {
// shoot the monster
ArrayList<MonsterBall> balls = game.getBalls();
for (MonsterBall ball : balls) {
shootMonster(ball);
}
}
public void shootMonster(MonsterBall ball) {
for (int i = 0; i < bullets.size(); i++) {
Bullet bullet = bullets.get(i);
if (PApplet.dist(bullet.x, bullet.y, ball.getX(), ball.getY()) < Bullet.RADIOUS
+ ball.getRadious()) {
ball.onBeenShot();
score++;
game.synth.chime(7);
if(!ball.isAlive()){
score+=5;
game.synth.boom(9);
}
// remove this bullet
bullets.remove(i);
}
}
if(score>=SCORE_TO_WON){
won = true;
}
}
public void drawInfo(){
String info = name+"--Score:"+score+"--Lives:"+lives;
if(!isAlive()){
info+="![Dead]";
}else if(won){
info="Won. Press \"Restart\" to play again";
}
game.text(info, x, y-20);
}
@Override
public void performeAction(int type) {
// TODO Auto-generated method stub
switch(type){
case InstructionType.FIRE:
break;
case InstructionType.MOVE_TO_CENTER:
this.setX((game.getW()-width)/2);
break;
case InstructionType.RESTART:
this.reset();
break;
}
}
private class Bullet {
static final int RADIOUS = 5;
int x;
int y;
int color;
int speed;
public Bullet(int color, int x, int y, int speed) {
this.x = x;
this.y = y;
this.color = color;
this.speed = speed;
}
public void draw() {
game.fill(color);
// game.ellipseMode(PConstants.CENTER);
game.ellipse(x, y, 10, 10);
}
}
}
|