📄 player.java
字号:
package pp.actor;
import java.awt.*;
import java.util.*;
import java.awt.geom.*;
import like.actor2D.*;
import like.graphics.*;
import pp.scence.*;
public class Player extends Actor2D
{
PlayingScence pScence;
private boolean needChange;
private AnimationStrip next;
private int nextMode;
//角色状态(1,2,4已经定义)
public final int STATE_CONVERSE = 8;
private boolean deading = false;
private boolean dead = false;
private boolean destryed = false ;
//角色属性
private int team;
private String name;
private int speed;
private int pNumbers;
private int pLength;
private int speedMAX;
private int pNMAX;
private int pLenMax;
private int pn;
private int id;
//当前格状态
public int currIGe;
public int currJGe;
public int [] currHoldIGes = new int[4];
public int [] currHoldJGes = new int[4];
private int currDirection;
//get,set函数
public void setDeading(boolean deading) {this.deading = deading;}
public void setNeedChange(boolean change) {this.needChange = change;}
public void setID(int id) {this.id = id;}
public int getID() {return this.id;}
public AnimationStrip getNext() {return this.next;}
public String getName() {return this.name;}
public boolean getNeedChange() {return this.needChange;}
public boolean getDestroyed() {return this.destryed;}
public int getTeam() {return this.team;}
public int getCurrIGe() {return this.currIGe;}
public int getCurrJGe() {return this.currJGe;}
public int getPLength() {return this.pLength;}
public int getPNumbers() {return this.pNumbers;}
public boolean getDeading() {return this.deading;}
public boolean getDead() {return this.dead;}
public Player(PlayerGroup playerGroup,PlayingScence pScence,String name,int team)
{
super(playerGroup);
this.next = null;
this.needChange = false;
this.ruChang();
//this.setAnimateMode(Actor2D.AnimateMode_WAIT);
this.pScence = pScence;
this.name = name;
this.team = team;
initAttribute(name);
}
public void ruChang()
{
this.currAnimation = group.getAnimationStrip(PlayerGroup.RuChang);
this.currAnimator = this.currAnimation.createNewAnimator();
this.setAnimateMode(this.AnimateMode_AUTOPLAY);
this.setBufferTimes(1);
this.setAnimWait(4);
}
protected void initAttribute(String name)
{
speed = 1;
pNumbers = 1;
pLength = 1;
speedMAX = 6;
pNMAX = 8;
pLenMax = 8;
this.updateVel();
}
public void deading()
{
this.deading = true;
this.currAnimation = group.getAnimationStrip(PlayerGroup.Deading);
this.currAnimator = this.currAnimation.createNewAnimator();
this.setAnimateMode(Actor2D.AnimateMode_AUTOPLAY);
this.setBufferTimes(7);
this.setAnimWait(15);
this.setVel(0,0);
}
public void save()
{
this.setDeading(false);
this.setAnimateMode(Actor2D.AnimateMode_WAIT);
}
public void dead()
{
this.pScence.getResourceGroup().getAudio(ResourceGroup.Audio_dead).play();
this.dead = true;
this.currAnimation = group.getAnimationStrip(PlayerGroup.Dead);
this.currAnimator = this.currAnimation.createNewAnimator();
this.setAnimateMode(Actor2D.AnimateMode_AUTOPLAY);
this.setBufferTimes(1);
this.setAnimWait(3);
}
public void putPaoPao()
{
if(pScence.fraises[currIGe][currJGe]!=null) return;
if(++pn<=this.pNumbers)
this.pScence.fraises[currIGe][currJGe] = new PaoPao(this);
else pn = pNumbers;
}
public void addPn()
{
pn--;
}
public void addPLength(int l)
{
pLength += l;
if(pLength>=pLenMax) pLength = pLenMax;
}
public void addPNumbers(int n)
{
pNumbers += n;
if(pNumbers>pNMAX) pNumbers = pNMAX;
}
public void addSpeed(int speed)
{
this.speed += speed;
if(speed>speedMAX) this.speed=speedMAX;
else if(speed<=1) speed = 1;
}
public void updateVel()
{
if(this.getNotFazeMe()) return;
int a,b;
if(currAnimation==group.getAnimationStrip(PlayerGroup.WALKING_DOWN))
{a=0;b=1;}
else if(currAnimation==group.getAnimationStrip(PlayerGroup.WALKING_LEFT))
{a=-1;b=0;}
else if(currAnimation==group.getAnimationStrip(PlayerGroup.WALKING_RIGHT))
{a=1;b=0;}
else {a=0;b=-1;}
switch(speed)
{
case 1:
setVel(3*a,3*b);
this.setAnimWait(1);
break;
case 2:
setVel(4*a,4*b);
this.setAnimWait(1);
break;
case 3:
setVel(5*a,5*b);
this.setAnimWait(1);
break;
case 4:
setVel(6*a,6*b);
this.setAnimWait(1);
break;
case 5:
setVel(7*a,7*b);
this.setAnimWait(1);
break;
case 6:
setVel(8*a,8*b);
this.setAnimWait(1);
break;
default:
setVel(8*a,8*b);
this.setAnimWait(1);
break;
}
}
public void update()
{
if(animate())
{
this.updatePos();
}
xform.setToTranslation(pos.getX()+excursion.getX(),pos.getY()+excursion.getY());
}
public void paint(Graphics2D g2d)
{
Composite c = g2d.getComposite();
g2d.setComposite(AlphaComposite.getInstance( AlphaComposite
.SRC_OVER,0.60f ));
g2d.drawImage(this.pScence.getResourceGroup().getPlayerShadow(),
(int)(pos.getX()+2),
(int)(pos.getY()+20),AnimationStrip.observer);
g2d.setComposite(c);
g2d.setColor(Color.white);
g2d.setFont(new Font(null,Font.PLAIN,12));
g2d.drawString(this.name,
(int)(pos.getX()/*name.length()*g2d.getFont().getSize()/2-15*/),
(int)(pos.getY()+excursion.getY()-3));
super.paint(g2d);
}
public void updateDirection()
{
//得到当前方向
for(int i=0;i<=3;i++)
{
if(this.currAnimation==group.getAnimationStrip(i))
{this.currDirection = i;break;}
}
}
public void updatePos()
{
updateBounds();
checkBounds();
updateGe();
//System.out.println(this.currIGe+","+this.currJGe);
//把当前位置写到变换里
}
//冲突判断,最后敲定用格子法,初步预测比较完美!
public void checkBounds()
{
super.checkBounds();
this.updateGe();
this.updateDirection();
for(int i=0;i<pScence.getPlayerNum();i++)
{
if(i!=id && !pScence.getPlayer(i).getDead()
&& pScence.getPlayer(i).getDeading()
&& this.collidesWith(pScence.getPlayer(i)))
{
if(this.team == pScence.getPlayer(i).getTeam())
pScence.getPlayer(i).save();
else
pScence.getPlayer(i).dead();
}
}
//对当前占领格处理
for(int i=0;i<4;i++)
{
Fraise f = pScence.fraises[currHoldIGes[i]][currHoldJGes[i]];
int m;
for(m=0;m<i;m++)
{
if(pScence.fraises[currHoldIGes[m]][currHoldJGes[m]]==f)
break;
}
if(m<i) continue;
if(f!=null)
{
if(f.ifPP && ((PaoPao)f).psIn[id])
{
//不做任何处理
}
else if(f.canMove==false)
{
if(f.temp)
{
if(this.collidesWith(f))
this.goBack(i,this.currDirection);
}
else
goSide(i,currDirection);
}
else if(f.moving)
{
if(collidesWith(f))
{
goBack(i,currDirection);
}
}
else
{
if(goSide(i,this.currDirection))
{
if(canMove(currHoldIGes[i],currHoldJGes[i],currDirection))
{
if(f.move(currDirection))
{
//0上,1左,2下,3右
switch(currDirection)
{
case 0:
if(currHoldIGes[i]-1>=0)
pScence.fraises[currHoldIGes[i]-1][currHoldJGes[i]]
= new Fraise(f.getGroup(),20+40*(currHoldJGes[i]),40+40*(currHoldIGes[i]-1));
break;
case 1:
if(currHoldJGes[i]-1>=0)
pScence.fraises[currHoldIGes[i]][currHoldJGes[i]-1]
= new Fraise(f.getGroup(),20+40*(currHoldJGes[i]-1),40+40*(currHoldIGes[i]));
break;
case 2:
if(currHoldIGes[i]+1<13)
pScence.fraises[currHoldIGes[i]+1][currHoldJGes[i]]
= new Fraise(f.getGroup(),20+40*(currHoldJGes[i]),40+40*(currHoldIGes[i]+1));
break;
case 3:
if(currHoldJGes[i]+1<15)
pScence.fraises[currHoldIGes[i]][currHoldJGes[i]+1]
= new Fraise(f.getGroup(),20+40*(currHoldJGes[i]+1),40+40*(currHoldIGes[i]));
break;
}
}
}
}//end if
}//end else
}//end if
}//end for
}//end method checkBounds
public boolean canMove(int i,int j,int t)
{
if(pScence.fraises[i][j].getDestroyed()) return false;
int m;
switch(t)
{
case 0:
if(i-1<0 || pScence.fraises[i-1][j]!=null)
{return false;}
else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -