Android – how to make an enemy meter detector in libgdx?

In my top-down game, I have an enemy detector to detect whether the enemy is nearby. My question is how do I create a transition to my dashboard dynamically? I am new to this framework. Thank you and progress

When the player detects nearby enemies, the dashboard will be animated like the image screen below

No enemy

My code:

    //detector
    meter_bar = new Texture("meter_bar.png");
    myTextureRegion = new TextureRegion(meter_bar);
    myTexRegionDrawable = new TextureRegionDrawable(myTextureRegion);
    actormeter_bar = new Image(myTexRegionDrawable);
    actormeter_bar.setPosition(200,1005);
    stage.addActor(actormeter_bar);

    meter = new Texture("meter.png");
    myTextureRegion = new TextureRegion(meter);
    myTexRegionDrawable = new TextureRegionDrawable(myTextureRegion);
    actormetter = new Image(myTexRegionDrawable);
    actormetter.setPosition(190,990);
    stage.addActor( actormetter);

Player sport (forward):

//right_control
    right_paw = new Texture(Gdx.files.internal("right_paw.png"));
    myTextureRegion = new TextureRegion(right_paw);
    myTexRegionDrawable = new TextureRegionDrawable(myTextureRegion);
    moveForward = new ImageButton(myTexRegionDrawable); //Set the button up
    moveForward.getStyle().imageUp = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("right_paw.png"))));
    //the hover
    moveForward.getStyle().imageDown = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("right_paw-hover.png"))));
    moveForward.setPosition(520,110);
    stage.addActor(moveForward); //Add the button to the stage to perform rendering and take input.
    Gdx.input.setInputProcessor(stage);
    moveForward.addListener(new InputListener(){
        @Override
        public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
            System.out.println("Right Button Pressed");
            progressKnobX = progressKnobX + 4;

            actorprogressknob.setX(progressKnobX); // x-position to move to
            if(progressKnobX > 418 ) progressKnobX= 418 ;
            music.play();
            motionState=MotionState.NONE;
        }
        @Override
        public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
            motionState=MotionState.UP;
            return true;
        }
    });
    stage.addActor(moveForward);

Enemy elf movement:

 zombie = new Texture(Gdx.files.internal("enemy/ZombieSprite.png"));
    zombiesprite = new Sprite(zombie);
    zombieenemy = new Rectangle();
    zombieenemy.setWidth(zombiesprite.getWidth());

    zombieenemy = zombiesprite.getBoundingRectangle();
    zombieenemy.x = zombieX;
    zombieenemy.y = zombieY;
    TextureRegion[][] zom = TextureRegion.split(zombie, zombie.getWidth() / Zombie_FRAME_COLS, zombie.getHeight() / Zombie_FRAME_ROWS);
    TextureRegion[] zwalkFrames  = new TextureRegion[Zombie_FRAME_COLS * Zombie_FRAME_ROWS];
    index = 0;
    for (int i = 0; i < Zombie_FRAME_ROWS; i++) {
        for (int j = 0; j < Zombie_FRAME_COLS; j++) {
            zwalkFrames[index++] = zom[i][j];
        }
    }
    zombiewalkAnimation = new Animation<TextureRegion>(0.120f, zwalkFrames);

Moveforward button function:

//button functions
MotionState motionState=MotionState.NONE;
enum MotionState {
    NONE {
        @Override
        public boolean update(Rectangle player) {
            return true;
        }
    },
    UP {
        @Override
        public boolean update(Rectangle player) {
            player.y += 300 * Gdx.graphics.getDeltaTime();
            return false;
        }
    },
    DOWN{
        @Override
        public boolean update(Rectangle player) {
            player.y -= 300 * Gdx.graphics.getDeltaTime();
            return false;
        }
    },
    LEFT{
        @Override
        public boolean update(Rectangle player)  {
            player.x -= 100 * Gdx.graphics.getDeltaTime();
            return false;
        }
    },
    RIGHT{
        @Override
        public boolean update(Rectangle player) {
            player.x  += 100 * Gdx.graphics.getDeltaTime();
            return false;
        }
    };
    public abstract boolean update(Rectangle player);
}

give

    if(Gdx.input.isKeyPressed(Input.Keys.DOWN)) motionState = MotionState.DOWN;
    if(Gdx.input.isKeyPressed(Input.Keys.UP)) motionState=MotionState.UP;
    if(Gdx.input.isKeyPressed(Input.Keys.LEFT)) motionState=MotionState.LEFT;
    if(Gdx.input.isKeyPressed(Input.Keys.RIGHT)) motionState=MotionState.RIGHT;

    if(motionState.update(player)) motionState=MotionState.NONE;


    if(player.y> 1910){
        zombieenemy.y -= 60 * Gdx.graphics.getDeltaTime();
    }

resolvent:

Before I answer your preliminary questions, I would like to suggest that you learn to use libgdx extension Ashley, because things look very chaotic and it is difficult for you to maintain

My answer:

Hold an ArrayList < > or similar data model with all your internal game enemies, and then we will traverse each and get its position and calculate the distance from the player

Enemy closestEnemy = null;
float closestEnemyDist = -1;

for(Enemy enemy : ListOfEnemies){
    /* here we find the distance to the player */
    float distToPlayer = Math.sqrt((enemy.y-player.x)^2 + (enemy.y-player.y)^2);

    /* initiate closestEnemyDist for the first time */
    if(closestEnemyDist == -1){
         closestEnemyDist = distToPlayer;
    }

    /* we find the closest distance to the player */
    float minDist = Math.min(closestEnemyDist, distToPlayer);

    /* we make sure it is the closest and save the closest enemy as well */
    if(minDist <= closestEnemyDist){
        closestEnemyDist = minDist;
        closestEnemy = enemy;
    }
}

This gives you the distance between your nearest enemy and it. You can formulate its method or just the distance you want. You can run this code every time the game is updated

Distance meter:

Here we're going to do a little math. Let's say that your meter shows the percentage... From 0% to 100%. You must select the maximum distance that your meter thinks is 0% (which means the enemy calls it the maximum distance). It's easy for you to understand this imagination. The distance is 100. Look at this method

public int calcMeter(float closestEnemyDist, float maxDistance) {
    int meter = 0;

    /* calculate only if enemy is in range */
    if(closestEnemyDist < maxDistance){
        /* using rounding and multiply by 100f because we wanted 
         * values from 0 - 100, you can can modify this to work just 
         * with float from 0 - 1f 
         */
        meter = Math.round(
            /* 
             * here we flip the percentage so if the enemy is very
             * close then your meter will be higher 
             */
            (1f - (closestEnemyDist / maxDistance)) * 100f
        );
    }
    return meter;
}

I hope this will help you. Good luck:)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>