手把手一步一步教你使用Java开发一个大型街机动作闯关类游戏19

2022-01-12  本文已影响0人  __豆约翰__

完善AttackAbleObject.java

AttackAbleObject添加isHit方法判断,当enemy未死时,是否被player攻击,考察2个条件:
dyn.hasHitBoxCollide(this):player的hitbox是否和enemy的hurtbox发生了重叠
dyn.isHitting():player是否正在攻击(player和enemy离得很近,但没有按下攻击键,也会触发第一个条件)

public abstract class AttackAbleObject extends GameObject{
    private Transform _transform;

    private int _cx1;
    private int _cx2;
    private int _cy1;
    private int _cy2;

    private boolean _hitting = false;
    private int _hp = 0;
    private int _strength;

    public AttackAbleObject(Transform trans, int hp, int strength,
                            int hurt_x, int hurt_y, int hurtscale_x, int hurtscale_y, int hitoffset_x1,
                            int hitoffset_y1, int hitoffset_x2, int hitoffset_y2) {
        super(trans,hurt_x,hurt_y,hurtscale_x, hurtscale_y);
        _hp = hp;
        _strength = strength;
        _transform = trans;
        this.setHitBoxOffsets(hitoffset_x1,hitoffset_y1,hitoffset_x2,hitoffset_y2);
    }

    public Shape getHitBox(){
        AffineTransform as = new AffineTransform();
        as.setTransform(_transform.getTransform());
        return as.createTransformedShape(new Rectangle(_cx1,_cy1,_cx2,_cy2));
    }
    public void setHitBoxOffsets(int x1,int y1, int x2, int y2){
        _cx1 = x1;
        _cx2 = x2;
        _cy1 = y1;
        _cy2 = y2;
    }

    public void setStrength(int strength){
        _strength = strength;
    }

    public int getStrength(){
        return _strength;
    }

    public void setHp(int hp){
        _hp = hp;
    }


    public int getHp(){
        return _hp;
    }

    public void takeDamage(AttackAbleObject dyn){
        setHp(getHp()-dyn.getStrength());
    }

    public boolean isHitting(){
        return _hitting;
    }

    public void setHitting(boolean hit){
        _hitting = hit;
    }

    public boolean hasHitBoxCollide(GameObject other){
        Area areaA = new Area(getHitBox());
        areaA.intersect(new Area(other.getHurtBox()));
        return !areaA.isEmpty() && Math.abs(getZ()-other.getZ())< Config.Z_FIGHT_DIST;
    }
    public boolean isDead(){
        return getHp()<=0;
    }
    public boolean isHit(AttackAbleObject dyn){
        if(!isDead()){
                if(dyn.hasHitBoxCollide(this) && dyn.isHitting()){
                    return true;
                }
                else{
                    return false;
                }
        }
        return false;
    }
}

enemy的update方法中判断是否被player攻击,如果是,减血(this.takeDamage(_player))

public class Enemy extends AttackAbleObject{

    @Override
    public void update(Graphics2D g){
        _animator.show(g);
        followPath();
        if(!this.isDead()){
            _animator.show(g);
            followPath();
            if(this.isHit(_player)){
                this.takeDamage(_player);
            }
        }
    }

同时player的handleAttack()方法中要设置正在攻击(setHitting(true))

public class Player extends AttackAbleObject{
    
    void handleAttack(){
        if(_attack){
            _hitDelayTimer -= Config.TIMER_DIF;
            if(_hitDelayTimer<=_hitBoxDelay){
                setHitting(true);
            }
            if(_hitDelayTimer<=0){
                _rdyToAttack = true;
                _attack = false;
        }

如果您迷路了,请参考完整源码:

项目源码

项目源码

上一篇下一篇

猜你喜欢

热点阅读