Android Open Source - hackfmi-ragdoll-physics Player Object






From Project

Back to project page hackfmi-ragdoll-physics.

License

The source code is released under:

GNU General Public License

If you think the Android project hackfmi-ragdoll-physics listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.midtownmadness.bubblecombat.game;
/*from w  w w .  ja  v a2  s.  c o  m*/
import java.util.Random;

import org.jbox2d.collision.shapes.CircleShape;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.BodyType;
import org.jbox2d.dynamics.FixtureDef;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;

public class PlayerObject extends GameObject {
  private static final int radius = 5;
  private static final Random rand = new Random();
  private static final float MAX_HEALTH = 100;
  protected Paint paint;
  protected Paint healthBarPaint;
  
  protected float health;
  private Vec2 initialPosition;
  private RectF boundingBox;
  private Bitmap bitmap;
  private Rect bitmapBounds;
  private int playerId;
  
  public PlayerObject(Vec2 initialPosition, Bitmap bitmap, int playerId)
  {
    this.playerId = playerId;
    paint = new Paint();
    paint.setStyle(Paint.Style.FILL);
    paint.setColor(getRandomColor());
    this.initialPosition = initialPosition;
    
    healthBarPaint = new Paint();
    healthBarPaint.setStyle(Paint.Style.STROKE);
    healthBarPaint.setColor(Color.RED);
    healthBarPaint.setStrokeWidth(1f);
    
    health = MAX_HEALTH;
    boundingBox = new RectF();
    this.bitmap = bitmap;
    bitmapBounds = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
  }
  
  private int getRandomColor()
  {
    // Will produce only bright / light colors:
    int r = rand.nextInt(128) + 128;
    int g = rand.nextInt(128) + 128;
    int b = rand.nextInt(128) + 128;
    
    return Color.rgb(r, g, b);
  }
  
  @Override
  public void render(Canvas canvas) {
    if (body != null)
    {
      Vec2 position = body.getPosition();
      boundingBox.set(position.x - radius, position.y - radius, position.x + radius, position.y + radius);
      canvas.drawBitmap(bitmap, bitmapBounds, boundingBox, null);
      
      canvas.drawArc(boundingBox, 0, (float) (2 * 180 * (health / MAX_HEALTH)), false, healthBarPaint);
    }
  }
  
  @Override
  public FixtureDef buildFixtureDef() {
    FixtureDef def = new FixtureDef();
    CircleShape shape = new CircleShape();
    shape.setRadius(radius);
    def.shape = shape;
    def.friction = 0.2f;
    def.restitution = 0.7f;
    def.density = 0.001f;
    
    return def;
  }
  
  @Override
  public BodyDef buildBodyDef() {
    BodyDef def = new BodyDef();
    def.position = initialPosition;
    def.type = BodyType.DYNAMIC;
    def.bullet = true;
    def.fixedRotation = true;
    return def;
  }

  public void takeDamage(float dmg) {
    health = Math.max(0f, health - dmg);
  }

  public float getHealth() {
    return health;
  }
  
  public int getPlayerId() {
    return playerId;
  }

  public void setHealth(float health) {
    this.health = health;
  }
}




Java Source Code List

com.midtownmadness.bubblecombar.listeners.GameRoomListener.java
com.midtownmadness.bubblecombar.model.GameModel.java
com.midtownmadness.bubblecombar.model.GamesAdapter.java
com.midtownmadness.bubblecombar.model.MockAdapter.java
com.midtownmadness.bubblecombat.BaseActivity.java
com.midtownmadness.bubblecombat.BluetoothGamesAdapter.java
com.midtownmadness.bubblecombat.GameActivity.java
com.midtownmadness.bubblecombat.GameView.java
com.midtownmadness.bubblecombat.GlobalContext.java
com.midtownmadness.bubblecombat.MenuActivity.java
com.midtownmadness.bubblecombat.MockMultiplayerGame.java
com.midtownmadness.bubblecombat.Settings.java
com.midtownmadness.bubblecombat.game.DrawThread.java
com.midtownmadness.bubblecombat.game.GameObject.java
com.midtownmadness.bubblecombat.game.GameWallObject.java
com.midtownmadness.bubblecombat.game.LevelObject.java
com.midtownmadness.bubblecombat.game.PlayerObject.java
com.midtownmadness.bubblecombat.multiplay.BaseStrategy.java
com.midtownmadness.bubblecombat.multiplay.BluetoothConnectException.java
com.midtownmadness.bubblecombat.multiplay.BluetoothMessage.java
com.midtownmadness.bubblecombat.multiplay.Callback.java
com.midtownmadness.bubblecombat.multiplay.ClientStrategy.java
com.midtownmadness.bubblecombat.multiplay.HostStrategy.java
com.midtownmadness.bubblecombat.multiplay.LoggingListener.java
com.midtownmadness.bubblecombat.multiplay.LooperThread.java
com.midtownmadness.bubblecombat.multiplay.MessageType.java
com.midtownmadness.bubblecombat.multiplay.MultiplayEventListener.java
com.midtownmadness.bubblecombat.multiplay.MultiplayEvent.java
com.midtownmadness.bubblecombat.multiplay.MultiplayManager.java
com.midtownmadness.bubblecombat.multiplay.MultiplayStrategy.java
com.midtownmadness.bubblecombat.multiplay.MultiplayUtil.java
com.midtownmadness.bubblecombat.multiplay.MultiplayerGame.java
com.midtownmadness.bubblecombat.multiplay.commobjects.GoMessageObject.java
com.midtownmadness.bubblecombat.physics.BodyCreationRequest.java
com.midtownmadness.bubblecombat.physics.BodyUserData.java
com.midtownmadness.bubblecombat.physics.CentralHurdle.java
com.midtownmadness.bubblecombat.physics.CollisionListener.java
com.midtownmadness.bubblecombat.physics.DefaultLevelBuilder.java
com.midtownmadness.bubblecombat.physics.LevelBuilder.java
com.midtownmadness.bubblecombat.physics.MovementRequest.java
com.midtownmadness.bubblecombat.physics.MovementStateRequest.java
com.midtownmadness.bubblecombat.physics.PhysicsRequest.java
com.midtownmadness.bubblecombat.physics.PhysicsService.java
com.midtownmadness.bubblecombat.views.MenuGameView.java
com.midtownmadness.bubblecombat.views.MenuView.java