package Business.PersonFile;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.ImageIcon;
import Business.PanelManager.JMainGamePanel;
import Business.ResourceManager.ResourceManager;
import Business.WeaponBehavior.AxeWeapon;
import Business.WeaponBehavior.ChangeWeapon;
import Business.WeaponBehavior.HammerWeapon;
import Business.WeaponBehavior.KnifeWeapon;
import Business.WeaponBehavior.Weapon;
public class Knight extends Person{
/**
* 这是骑士类,确定骑士特有的属性和方法
*/
//***********************************************//
private final static int MaxXMiles = 650; //人物向右移动的最大距离
private final static int MinXMiles = 0; //人物向右移动的最大距离
private final static int MaxYMiles = 290; //人物向右移动的最大距离
private final static int MinYMiles = 130; //人物向右移动的最大距离
private final static int R = 1; //人物向右标志量
private final static int L = 0; //人物向左标志量
// private final static int weaponDistanceX = 15;
// private final static int weaponDistanceY = 30;
private static int n = 0;
private static int personNum = 2;
private static boolean isFight = false; //是否处于攻击状态
private static boolean isGethurt = false;//是否受伤
private static boolean isVictory = false;
private static int fightNum = 1;
//***********************************************//
private int direction = R; //人物移动的方向
private int action = 4; //骑士走路时加载不同的png图,action为哪个动作
private int staticstate = 0;
private static int diednum = 22;
private boolean isOver = false;
private boolean isPersonsub = false;
private int step = 0; //当人物移动4 step时,换行走的png图
private static int demonNum = 0;
private static int hurtstep = 0;
private int fullvit = 100;
private int score = 0;
private String Placestr[] = {"肮脏的森林","罪恶的沙滩","风雨魔道","沙漠之心","地狱城市","火图之地","终极烈焰"};
private String person = "c6.png"; //加载人物png图的文件名
private String personChoice = "c"; //选择那个骑士
private String weapons;
private Timer timer;
private Timer timer2;
// private Timer timer3;
//***********************************************//
private int level = 0; //骑士的等级
//***********************************************//
JMainGamePanel panel;
ChangeWeapon chWeapon;
Weapon weapon;
Demon[] demon = new Demon[15];
DemonFactory factory = new DemonFactory();
//***********************************************//
/**
* 默认构造方法
*/
public Knight() {
// TODO Auto-generated constructor stub
}
/**
* 带参构造方法(初始化)
* @param panel
*/
public Knight(JMainGamePanel panel) {
this.panel = panel;
vit = 100;
dfe = 80;
atk = 60;
PosX = 300;
PosY = 200;
chWeapon = new KnifeWeapon(panel,direction,PosX,PosY);
timer = new Timer();
timer2 = new Timer();
timer2.schedule(new TimerTask() {
@Override
public void run() {
StateRun();
}
},100,200);
}
/**
* 人物右移
*/
public void MoveRight(int x){
if(!isOver){
if (PosX < MaxXMiles)
PosX += x;
direction = R;
actionChanged();
}
}
/**
* 人物左移
*/
public void MoveLeft(int x){
if(!isOver){
if (PosX > MinXMiles)
PosX -= x;
direction = L;
actionChanged();
}
}
/**
* 人物上移
*/
public void MoveUp(int y){
if(!isOver){
if(PosY > MinYMiles)
PosY -= y;
actionChanged();
}
}
/**
* 人物下移
*/
public void MoveDown(int y){
if(!isOver){
if(PosY < MaxYMiles)
PosY += y;
actionChanged();
}
}
/**
* 人物移动,改变显示图片,并设置步长
*/
public void actionChanged(){
chWeapon.setXY(PosX, PosY,direction);
if (step % 4 == 0)//每移动四个像素点,更换一张走路图片
action = (action + 1) % 4+4;
step = (step+1)%4;
}
/**
* 绘制骑士
* @param g
*/
public void drawPerson(Graphics g){
if (!isFight) {
person = personChoice + action + ".png"; // 加载相应的骑士png图
}
// 调用资源管理类中的加载图片资源方法,来加载背景图片
ResourceManager imageResource = new ResourceManager();
ImageIcon icon_person = new ImageIcon();
icon_person = imageResource.GetImage(person);
//绘制骑士
if (direction == L) { //方向向左
AffineTransform transform = new AffineTransform();
Graphics2D g2 = (Graphics2D) g;
transform = flip(icon_person,PosX,PosY);
g2.drawImage(icon_person.getImage(), transform, panel);
} else {//方向向右
g.drawImage(icon_person.getImage(), PosX, PosY,
icon_person.getIconWidth(), icon_person.getIconHeight(),
panel);
}
}
/**
* 实现图像的水平翻转
* @param icon ImageIcon类对象
* @param x x坐标
* @param y y坐标
* @return AffineTransform类对象
*/
public AffineTransform flip(ImageIcon icon,int x,int y) {
Image img = icon.getImage();
AffineTransform transform = new AffineTransform();
transform.scale(-1, 1);
transform.translate(-2 * img.getWidth(null) / 2,
0 * img.getHeight(null) / 2);
transform.translate((double) -x, (double) y);
return transform;
}
/**
* 绘制攻击画面
* @param g
*/
public void drawFight(Graphics g){
person = personChoice+"9.png"; //加载相应的骑士png图
//调用资源管理类中的加载图片资源方法,来加载背景图片
ResourceManager imageResource = new ResourceManager();
ImageIcon icon_person = new ImageIcon();
icon_person = imageResource.GetImage(person);
//绘制骑士
if (direction == L) {
g.drawImage(icon_person.getImage(), PosX, PosY,
icon_person.getIconWidth(), icon_person.getIconHeight(),
panel);
} else {
g.drawImage(icon_person.getImage(), PosX, PosY,
icon_person.getIconWidth(), icon_person.getIconHeight(),
panel);
}
}
/**
* 是否受伤
*/
public void getHurt() {
for(int i = 0; i < demonNum;i++){
int x = demon[i].getPosX();
int y = demon[i].getPosY();
hurtstep = (hurtstep + 1)% 10;
if(hurtstep == 9)
subVit();
ResourceManager imageResource = new ResourceManager();
int width = imageResource.GetImage(person).getIconWidth();
int height = imageResource.GetImage(person).getIconHeight();
if (direction == R) {
if (PosY - y < 4
&& PosY - y > -10
&& (x < PosX && PosX < x + width - 7 || PosX < x
&& x < PosX + width - 7)) {
isGethurt = true;
}
} else {
if (PosY - y < 4
&& PosY - y > -10
&& (x + 50 < PosX && PosX < x + 150 + width - 7 || PosX < x + 50
&& x + 150 < PosX + width - 7)) {
isGethurt = true;
}
}
}
}
/**
* 绘制受伤状态的png图
*
* @param g
*/
public void drawGetHurt(Graphics g) {
person = "c22.png"; // 受伤状态的png图名称
// 调用资源管理类中的加载图片资源方法,来加载背景图片
ResourceManager imageResource = new ResourceManager();
ImageIcon icon_person = new ImageIcon();
icon_person = imageResource.GetImage(person);
// 绘制受伤的骑士
if (direction == L) { //方向向左,翻转图片
AffineTransform transform = new AffineTransform();
Graphics2D g2 = (Graphics2D) g;
transform = flip(icon_person, PosX, PosY);
g2.drawImage(icon_person.getImage(), transform, panel);
} else {//方向向右
g.drawImage(icon_person.getImage(), PosX, PosY,
icon_person.getIconWidth(), icon_person.getIconHeight(),
panel);
}
}
/**
* 绘制死亡状态图
* @param g
*/
public void drawDied(Graphics g) {
diednum++;
if(diednum > 25){
diednum = 25;
}
person = personChoice + diednum + ".png"; // 加载相应的骑士png图
// 调用资源管理类中的加载图片资源方法,来加载背景图片
ResourceManager imageResource
- 1
- 2
前往页