package com.openlab.day09.demo.games;
public class Hero implements IHero{
private String name;
private String weapon;
private int life;
private int Maxlife;
private int exp;
private boolean isLive;
private int attacker;
private int defender;
private int level;
private int counteravoid;
private boolean t = false;
private boolean t1;
@Override
public void fight(Monster monster) {
if(!this.isLive) {System.out.println("you are dead!");return;}
if(!monster.isLive()) {return;}
System.out.println(this.name+"挥舞着"+this.weapon+"杀向了"+monster.getName()+"的怪物");
monster.hurt(this);
show();
}
public void hurt(Monster monster) {
t1 = t;
//几率触法躲闪
this.show();
if(this.getRandom()<=this.counteravoid) {
System.out.println("我有敏捷的身形,你这两招还伤不到我!(闪避)");
this.setT(!this.isT());
}
//被攻击(无躲闪时)
else {
System.out.println("放肆,竟敢打我!");
int baseLose = 5;
int temp = monster.getAttacker()-this.defender;
if(temp<=0)this.life-=baseLose;
else
this.life -= temp + baseLose;
//判断是否存活
if(this.life<=0) {
System.out.println("我死了,看来光明竟不能战胜黑暗!");
dead();
}
}
this.show();
if(t!=t1) {
t1=true;
t=false;
}
}
@Override
public void show() {
System.out.println("Hero [name=" + name + ", weapon=" + weapon + ", life=" + life + ", exp=" + exp + ", isLive=" + isLive
+ ", attacker=" + attacker + ", defender=" + defender + ", level=" + level + "]");
}
@Override
public void dead() {
// TODO Auto-generated method stub
if(this.isLive)this.isLive = false;
}
//获取躲避的随机数
public int getRandom() {
int rd = (int)(Math.random()*100);
return rd;
}
//判断是否要升级
public void upLevel() {
show();
int needExp = 0;
for(int i = 1 ; i <= this.level ; i++) {
needExp += 50*i;
}
while(this.exp >= needExp) {
System.out.println("我升级了!升到"+(this.level+1)+"级了");
this.exp -= needExp;
this.life = this.Maxlife + 20;
this.Maxlife = this.life;
this.attacker += 3;
this.defender += 3;
this.level++;
}
}
public Hero() {}
public Hero(String name, String weapon,int counteravoid) {
this.name = name;
this.weapon = weapon;
this.life = 100;
this.exp = 0;
this.isLive = true;
this.attacker = 5;
this.defender = 2;
this.level = 1;
this.Maxlife = this.life;
this.counteravoid = counteravoid;
this.t1 = t;
}
public int getMaxlife() {
return Maxlife;
}
public void setMaxlife(int maxlife) {
Maxlife = maxlife;
}
public int getCounteravoid() {
return counteravoid;
}
public void setCounteravoid(int counteravoid) {
this.counteravoid = counteravoid;
}
public boolean isT1() {
return t1;
}
public void setT1(boolean t1) {
this.t1 = t1;
}
public boolean isT() {
return t;
}
public void setT(boolean t) {
this.t = t;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getWeapon() {
return weapon;
}
public void setWeapon(String weapon) {
this.weapon = weapon;
}
public int getLife() {
return life;
}
public void setLife(int life) {
this.life = life;
}
public int getExp() {
return exp;
}
public void setExp(int exp) {
this.exp = exp;
}
public boolean isLive() {
return isLive;
}
public void setLive(boolean isLive) {
this.isLive = isLive;
}
public int getAttacker() {
return attacker;
}
public void setAttacker(int attacker) {
this.attacker = attacker;
}
public int getDefender() {
return defender;
}
public void setDefender(int defender) {
this.defender = defender;
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
}