package org.ntlab.SproutServer.battles;
import framework.animation.Animation3D;
import framework.gameMain.OvergroundActor;
import framework.model3D.Object3D;
import framework.model3D.Position3D;
import framework.physics.PhysicsUtility;
import framework.physics.Solid3D;
import net.arnx.jsonic.JSONHint;
import org.ntlab.SproutServer.accounts.Accounts;
import javax.vecmath.Vector3d;
import java.net.URLDecoder;
/**
* プレイヤー
*
* @author matsumoto_k
*/
public class Player {
private OvergroundActor actor = null; // プレイヤーのオブジェクト
private int hp = BattlesConst.PLAYER_INIT_HP; // プレイヤーのHP
private boolean playerCollision = false;
private int userId = -1;
private int memberId = -1;
private String userName = "";
public Player(int userId, int memberId) {
this.userId = userId;
this.memberId = memberId;
this.userName = Accounts.getInstance().getUserNameById(this.userId);
actor = createPlayerObject(null);
setInitDirection(0.0, 0.0, 0.0);
// TODO:初期位置を設定する
setInitPosition(Math.random()*100, Math.random()*100, 0);
}
/**
* プレイヤーの情報を更新する
*
* @param position3D プレイヤーの位置
* @param vector3d プレイヤーの向き
*/
public void update(Position3D position3D, Vector3d vector3d) {
actor.setPosition(position3D); // プレイヤーの新しい位置をセット
actor.setDirection(vector3d); // プレイヤーの新しい向いている方向をセット
}
/**
* プレイヤーのオブジェクトを作成
*
* @param animation3D アニメーション
* @return プレイヤーのオブジェクト
*/
private OvergroundActor createPlayerObject(Animation3D animation3D) {
// TODO:プレイヤーの見た目を考える
String path = Battles.getInstance().getClass().getResource("pocha.stl").getPath();
try {
path = URLDecoder.decode(path, "utf-8");
} catch (Exception e) {
}
PlayerModel playerModel = new PlayerModel(path, null);
return new OvergroundActor(new Solid3D(playerModel.createObject()), null);
}
public String getUserName() {
return userName;
}
/**
* プレイヤーの初期位置を設定する
*
* @param x x座標
* @param y y座標
* @param z z座標
*/
public void setInitPosition(double x, double y, double z){
actor.setPosition(new Position3D(x, y, z));
}
/**
* プレイヤーの初期方向を設定する
*
* @param x x方向
* @param y y方向
* @param z z方向
*/
public void setInitDirection(double x, double y, double z) {
actor.setDirection(new Vector3d(x, y, z));
}
/**
* プレイヤーの位置を取得
*
* @return プレイヤーの位置
*/
public Position3D getPlayerPosition3d() {
return actor.getPosition();
}
/**
* プレイヤーの向きを取得
*
* @return プレイヤーの向き
*/
public Vector3d getPlayerVector3d() {
return actor.getDirection();
}
/**
* プレイヤーのHPを取得
*
* @return プレイヤーのHP
*/
public int getHp() {
return hp;
}
/**
* プレイヤーにダメージを与える
*
* @param damage 与えるダメージ
*/
public void setDamage(int damage) {
hp -= damage;
}
/**
* プレイヤーが生存しているかどうか判定
*
* @return 生きている:true, 死んでいる:false
*/
@JSONHint(ignore = true)
public boolean isAlive() {
if (hp < 0) {
return false;
}
return true;
}
/**
* プレイヤー同士の衝突判定
*
* @param player 判定するプレイヤー
* @return 衝突している時はtrue, していない時はfalse
*/
public boolean checkCollision(Player player) {
if (PhysicsUtility.checkCollision(getBody(), null, player.getBody(), null) != null) {
return true;
}
return false;
}
public boolean isPlayerCollision() {
return playerCollision;
}
/**
* プレイヤー同士の衝突フラグ
*
* @param playerCollision
*/
public void setPlayerCollision(boolean playerCollision) {
this.playerCollision = playerCollision;
}
/**
* プレイヤーのオブジェクトを取得する
*
* @return プレイヤーのオブジェクト
*/
@JSONHint(ignore = true)
public Object3D getBody() {
return actor.body;
}
/**
* プレイヤーのuserIdを取得する
*
* @return userId
*/
@JSONHint(ignore = true)
public int getUserId() {
return userId;
}
/**
* プレイヤーのmemberIdを取得する
*
* @return memberId
*/
@JSONHint(ignore = true)
public int getMemberId() {
return memberId;
}
}