SimpleGame.java :  » UnTagged » anmotion » org » nyu » mocap » anmotion » processing » simplegame » Android Open Source

Android Open Source » UnTagged » anmotion 
anmotion » org » nyu » mocap » anmotion » processing » simplegame » SimpleGame.java
package org.nyu.mocap.anmotion.processing.simplegame;

import java.util.ArrayList;

import org.nyu.mocap.anmotion.java.Player;
import org.nyu.mocap.anmotion.processing.PAppletGame;

import processing.core.PFont;

public class SimpleGame extends PAppletGame{
  private final static int  MAX_BALL_COUNT = 4;
  private int w;
  private int h;
  private ArrayList<SimpleGamePlayer> players;
  private ArrayList<MonsterBall> balls;
  private PFont infoFont;
  
  private Player testPlayer;
  
  //test mouse event
  private int x;
  private int y;
  private int pX;
  private int pY;
  
  public SimpleGame(int w,int h){
    this.w = w;
    this.h = h;
    
    players = new ArrayList<SimpleGamePlayer>();
    balls = new ArrayList<MonsterBall>();
    
    testPlayer = this.addPlayer("erhuaming");
    
  }
  public SimpleGame(){
    this(800,600);
  }
  
  @Override
  public void mouseMoved() {
    // TODO Auto-generated method stub
    testPlayer.moveBy(mouseX-pX, mouseY-pY);
    pX = mouseX;
    pY = mouseY;
  }


  public void setup(){
    size(this.w,this.h);
    smooth();
    frameRate(30);
    
    infoFont = loadFont("BankGothic-Medium-24.vlw");
    this.textFont(infoFont);
  }
  
  public void draw(){
    background(0);
    for(SimpleGamePlayer p:players){
      p.draw();
      p.shoot();
    }
    updateBalls();
    for(MonsterBall b:balls){
      b.draw();
    }
  }
  
  public void updateBalls(){
    for(int i=0;i<balls.size();i++){
      MonsterBall b = balls.get(i);
      if(!b.isAlive()){
        balls.remove(i);
      }else{
        b.move();
      }
      
      if(b.getY()>h-b.getRadious()){
        //ball escaped
        balls.remove(i);
        //players lose one life
        onBallEscaped();
      }
    }
    
    if(balls.size()<MAX_BALL_COUNT){
      //add new balls
      for(int i=0;i<MAX_BALL_COUNT-balls.size();i++){
        MonsterBall ball = new MonsterBall(this,(int)(this.random(20)+25),(int)this.random(w),0);
        balls.add(ball);
      }
    }
  }
  
  public void onBallEscaped(){
    for(SimpleGamePlayer p:players){
      if(p.isAlive())
        p.setLives(p.getLives()-1);
    }
    updatePlayers();
    synth.spacey(1);
  }
  
  public void updatePlayers(){
    //remove dead players
    
  }
  
  @Override
  public Player addPlayer(String name){
    SimpleGamePlayer player = new SimpleGamePlayer(this,name);
    players.add(player);
    return player;
  }



  public ArrayList<MonsterBall> getBalls() {
    return balls;
  }



  public void setBalls(ArrayList<MonsterBall> balls) {
    this.balls = balls;
  }
  public int getW() {
    return w;
  }
  public void setW(int w) {
    this.w = w;
  }
  public int getH() {
    return h;
  }
  public void setH(int h) {
    this.h = h;
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.