📄 chip.java
字号:
Chip.chipImage[1][4],null//下蹲
);
}
if(this.isByHit){//被击中
this.img = this.moveHitImageChange.imageChange(this.hitLeftImage);
}
break;
case PublicVar.RIGHT:// 向右
if(this.byHit){
this.setLeftRightImage(PublicVar.isRight,moveHitImageChange,
this.moveHitRightImage,this.moveHitRightInBoxImage,
null,this.standHitRightImage,//没有箱子站立
null,this.standHitRightInBoxImage,//有箱子站立
null,this.skipHitRightImage,//跳跃
null,this.squatHitRightImage//下蹲
);
}
else{
this.setLeftRightImage(PublicVar.isRight,moveImageChange,
moveRightImage,moveRightInBoxImage,
Chip.chipImage[0][5],null,//没有箱子站立
Chip.chipImage[0][0],null,//有箱子站立
Chip.chipImage[0][6],null,//跳跃
Chip.chipImage[0][4],null//下蹲
);
}
if(this.isByHit){//被击中
this.img = this.moveHitImageChange.imageChange(this.hitRightImage);
}
break;
}
}
/**
*
* @param isMove
* @param imageChange
* @param moveImage
* @param moveInBoxImage
* @param standImage
* @param standHitImage
* @param standInBoxImage
* @param standInBoxHitImage
* @param skipImage
* @param skipHitImage
* @param squatImage
* @param squatHitImage
*/
private void setLeftRightImage(boolean isMove,MoveImageChange imageChange,//是否按下移动键
Image[] moveImage,//没有举箱子图片
Image[] moveInBoxImage,//举箱子图片
Image standImage,Image[] standHitImage,//没有箱子站立
Image standInBoxImage,Image[] standInBoxHitImage,//有箱子站立
Image skipImage,Image[] skipHitImage,//跳跃
Image squatImage,Image[] squatHitImage//下蹲
)//无敌
{
if (isSkipState || this.isChipDown) {//跳跃或下降状态
if(skipImage != null){
this.img = skipImage;// 向左站立
}
else{
this.img = imageChange.imageChange(skipHitImage);// 向左站立
}
} else if (this.isSquat) {//下蹲状态
if (this.box == null) {// 松鼠手上没有箱子
if(squatImage != null){
this.img = squatImage;// 向左站立
}
else{
this.img = imageChange.imageChange(squatHitImage);// 向左站立
}
}
}
else if (isMove) {//移动图片
if(this.box!=null){//举箱子
this.img = imageChange.imageChange(moveInBoxImage);
}
else{//没有箱子
this.img = imageChange.imageChange(moveImage);
}
}
else {//站立
if(this.box!=null){//举箱子
if(standInBoxImage != null){
this.img = standInBoxImage;
}
else{
this.img = imageChange.imageChange(standInBoxHitImage);// 向左站立
}
}
else{//没有箱子
if(standImage != null){
this.img = standImage;
}
else{
this.img = imageChange.imageChange(standHitImage);// 向左站立
}
}
}
}
/**
* 设置碰撞
*
*/
private void setHit() {
boolean isIntersect=false;//临时布尔值
isIntersect = this.setMapRectHit();//和地图区域碰撞
if(isIntersect){
this.setRoleRectHit();//和角色碰撞
}
else{
isIntersect = this.setRoleRectHit();//和角色碰撞
}
//如果松鼠没有和地图区域和箱子相碰,则下落
if(isIntersect==false && this.isSkipState == false){
this.isChipDown=true;
}
}
/**
* 设置角色和地图区域碰撞
* @return 是否碰撞
*/
private boolean setMapRectHit(){
boolean isIntersect = false;
//循环遍历地图区域
for (int i = 0; i < PublicVar.rectList.size(); i++) {
this.mapRect = PublicVar.rectList.get(i);
// 设置角色和底层地图区域碰撞
if(mapRect.isDown && this.tempHitRect.intersects(mapRect)){
this.setNotMove(mapRect);
}
//如果角色临时区域和地图区域相碰
if(this.tempMapRect.intersects(mapRect)){
isIntersect=true;
if(this.isChipDown){//当松鼠处于下降状态
this.restSkip();//跳跃复位
if(mapRect.isDown && this.y != mapRect.y-this.height){
this.y = mapRect.y-this.height;
}
}
if(mapRect.isDown){//是否是底层区域
this.isDownSkipable = false;
}
else{//不是底层区域,则可以向下跳跃
this.isDownSkipable = true;
}
}
}
/*进入Boss窗体*/
if(GameStartFrame.enterBoss!=null &&
this.rect.intersects(GameStartFrame.enterBoss)){
PublicVar.isBoss=true;
}
return isIntersect;
}
/**
* 设置松鼠和其它角色相碰
* @return 是否和箱子相碰
*/
private boolean setRoleRectHit(){
boolean isIntersect = false;
// 松鼠和角色碰撞
for(int i=0;i<PublicVar.roleList.size();i++){//遍历角色集合
BaseRole roles=PublicVar.roleList.get(i);
//松鼠和箱子相碰
if(roles.getClass() == Box.class){
if(this.tempMapRect.intersects(roles.rect)){//松鼠站上箱子
isIntersect=true;
this.isDownSkipable=false;
if(this.isChipDown){//当松鼠处于下降状态
this.restSkip();
this.y=roles.y-this.height;
}
}
if(this.tempHitRect.intersects(roles.rect)){//松鼠不能穿过箱子
this.setNotMove(roles.rect);
}
}
if(roles.getClass() == ON_OFF.class
&& this.rect.intersects(roles.rect)){//松鼠和开关相碰
((ON_OFF)roles).setOFF();
}
else if(roles.getClass() == WaterFall.class
&& this.tempHitRect.intersects(roles.rect)){//松鼠和瀑布相碰
this.setNotMove(roles.rect);
}
else if(roles.getClass() == Flower.class
&& this.rect.intersects(roles.rect)){//松鼠和花相碰
Music.playSingleMusic(Music.PLAY_PROP);
PublicVar.roleList.remove(roles);
PublicVar.flowerNumber++;//花的数量加1
PublicVar.source+=50;//分数增加
if(PublicVar.flowerNumber == 25){
PublicVar.chipNumber++;//松鼠个数加1
PublicVar.flowerNumber = 0;
}
break;
}
else if(roles.getClass() == Heart.class
&& this.rect.intersects(roles.rect)){//松鼠和心相碰
Music.playSingleMusic(Music.PLAY_PROP);
PublicVar.roleList.remove(roles);
PublicVar.chip.life++;//松鼠生命加1
PublicVar.source+=50;//分数增加
break;
}
if(this.byHit == false){//如果不是无敌状态
if(((roles instanceof EnemyRole && ((EnemyRole)roles).life>0)
|| roles.getClass() == EnemyBullet.class
|| roles.getClass() == Fire.class)
&& this.rect.intersects(roles.rect)){//松鼠和敌人或敌人子弹相碰
this.isByHit = true;
this.life--;
this.byHit = true;
}
}
}
return isIntersect;
}
/**
* 设置松鼠和底层区域碰撞时,松鼠不能移动
* @param rect 底层地图区域
*/
private void setNotMove(Rectangle rect){
if(this.state == PublicVar.RIGHT && this.x<rect.x){//向右
this.x=rect.x-this.width;
}
else if(this.state == PublicVar.LEFT && this.x>rect.x){//向左
this.x=rect.x+rect.width;
}
}
/**
* 设置地图滚动
*
*/
private void setMapMove(){
// 当还未走到边界,并且X坐标大于400时,水平方向
if (GameStartFrame.mapX > -3250 && this.x > 400 && GameStartFrame.isRightScroll) {
GameStartFrame.mapX -= this.speed;// 设置地图滚动
this.x = 400;//角色坐标不变
//地图区域随地图一起滚动
for (int i = 0; i < PublicVar.rectList.size(); i++) {
PublicVar.rectList.get(i).moveByMap(PublicVar.RIGHT, this.speed*-1);
}
for (int i = 0; i < PublicVar.roleList.size(); i++) {
BaseRole role=PublicVar.roleList.get(i);
if(role.getClass() != this.getClass()){
role.moveByMap(PublicVar.RIGHT, this.speed*-1);
}
}
} else if (GameStartFrame.mapX <= -3250) {
GameStartFrame.mapX = -3250;
GameStartFrame.isRightScroll=false;
if(GameStartFrame.mapY==-2200){
GameStartFrame.isUpScroll=true;
}
}
if (GameStartFrame.mapX <0 && this.x < 400
&& GameStartFrame.isRightScroll == false && GameStartFrame.mapY==-1100) {
GameStartFrame.mapX += this.speed;// 设置地图滚动
this.x = 400;//角色坐标不变
//地图区域随地图一起滚动
for (int i = 0; i < PublicVar.rectList.size(); i++) {
PublicVar.rectList.get(i).moveByMap(PublicVar.RIGHT, this.speed);
}
for (int i = 0; i < PublicVar.roleList.size(); i++) {
BaseRole role=PublicVar.roleList.get(i);
if(role.getClass() != this.getClass()){
role.moveByMap(PublicVar.RIGHT, this.speed);
}
}
}
else if(GameStartFrame.mapX >=0){
GameStartFrame.mapX=0;
if(GameStartFrame.mapY==-1100){
GameStartFrame.isUpScroll=true;
}
if(GameStartFrame.mapY==0){
GameStartFrame.isRightScroll=true;
}
}
/**
* 垂直方向
*/
VmapMove(0,-1100);
VmapMove(1,0);
}
/**
* 地图垂直方向移动
* @param vCount 垂直方向移动计数器
* @param mapY 地图Y坐标
*/
private void VmapMove(int vCount,int mapY){
if(GameStartFrame.isUpScroll){//如果地图能向上滚
if(mapUpCount==vCount){
if(GameStartFrame.mapY<mapY){
if(this.y<100 || (mapY-GameStartFrame.mapY)<100){
this.y=100;
GameStartFrame.mapY+=this.speed;
//地图区域随地图一起滚动
for (int i = 0; i < PublicVar.rectList.size(); i++) {
PublicVar.rectList.get(i).moveByMap(PublicVar.UP, this.speed);
}
//角色随地图一起滚动
for (int i = 0; i < PublicVar.roleList.size(); i++) {
BaseRole role=PublicVar.roleList.get(i);
if(role.getClass() != this.getClass()){
role.moveByMap(PublicVar.UP,this.speed);
}
}
}
}
else if(GameStartFrame.mapY>=mapY){
mapUpCount++;
GameStartFrame.isUpScroll=false;
}
}
}
}
/**
* 跳跃复位
*
*/
private void restSkip(){
this.isSkipState = false;
this.isChipDown = false;
this.skipCount = 0;
}
private boolean isSquteDown = true;
/**
* 设置区域
*/
@Override
public void setRect() {
super.setRect();
// 让角色的区域和角色图片一样大小
this.rect.width = width;
this.rect.height = height;
//让角色临时地图区域和角色一起移动
this.tempMapRect.x = x;
this.tempMapRect.y = y+height;
//让角色碰撞区域根据角色状态做移动
if(this.state == PublicVar.RIGHT){
this.tempHitRect.x = x+width;
}
else if(this.state == PublicVar.LEFT){
this.tempHitRect.x = x-10;
}
this.tempHitRect.y = y;
if (this.isSquat) {//当下蹲时,区域变化
if(this.box == null){//没有箱子时
if(this.height ==0){
this.height =40;
this.y-=40;
}
this.rect.y += 20;
this.rect.height -= 20;
isSquteDown=true;
}
else if(isSquteDown && this.isSkipState == false && this.isChipDown == false){
this.rect.y += 40;
this.rect.height = 0;
this.y+=40;
this.height = 0;
this.box.setState(Box.SQUAT);
isSquteDown=false;
}
else if(this.box!=null){
this.tempHitRect.y = y-40;
}
}
else if(this.box != null && this.height == 0){
this.setSquatRest();
}
}
private void setSquatRest(){
this.y-=40;
this.height = 40;
this.rect.y += 40;
isSquteDown=true;
this.box.setState(Box.ACTIVE);
}
/**
* 设置X坐标
* @param x 松鼠X坐标
*/
public void setX(int x){
this.x = x;
}
/**
* 获得X坐标
* @return 松鼠X坐标
*/
public int getX(){
return x;
}
/**
* 设置Y坐标
* @param x 松鼠Y坐标
*/
public void setY(int y){
this.y = y;
}
/**
* 获得Y坐标
* @return 松鼠Y坐标
*/
public int getY(){
return y;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -