-
Notifications
You must be signed in to change notification settings - Fork 1
Player Interaction Sprint 1
Vermouth10 edited this page Aug 31, 2021
·
45 revisions
The Player can interact with other Entities in the game.
- Touch enemies -- If the player touches an enemy, the player will receive a health deduction.
- Player attack animation -- When the space button is pressed, the player texture will be switch to attack animation. And after the space key go up the animation will switch to the origanl player.
- Attack enemy -- The player can try to attack the nearby enemies by pressing the space button. If the distance between the player and the nearest enemy is smaller than the threshold, the enemy can be successfully destroyed.
- Simple enemies(ghost) -- Players can kill them by one attack.
- Difficult enemies(ghost king) -- Players cannot destroy these enemies at this stage.
- Destroy obstacles -- The player can try to destroy the nearby obstacles by pressing the space button. If the distance between the player and the nearest obstacle is smaller than the threshold, the obstacle can be successfully destroyed.
- The judgment distance is set to be different. When player is facing enemies, the judgment distance is set to be longer, and for obstacles, it set to be shorter.
- Setting the judgment distance:
private Entity findNearestTargets(Array<Entity> entities) {
Entity result = null;
float minDstEnemy = 2.0f;
float minDstObstacle = 1.1f;
for (Entity en: entities) {
if (en.getType() == Entity.Type.GHOST || en.getType() == Entity.Type.GHOSTKING) {
float dst = entity.getPosition().dst(en.getPosition());
if (minDstEnemy > dst) {
minDstEnemy = dst;
result = en;
}
} else if (en.getType() == Entity.Type.OBSTACLE) {
float dst = entity.getPosition().dst(en.getPosition());
if (minDstObstacle > dst) {
minDstObstacle = dst;
result = en;
}
}
}
return result;
}
- Attack enemies and obstacles:
void attack() {
logger.info("attack");
Array<Entity> entities = ServiceLocator.getEntityService().getEntities();
Entity nearest = findNearestTargets(entities);
logger.info("attack nearest--{}", nearest);
if (nearest != null) {
if (nearest.getType().equals(Entity.Type.GHOSTKING)) {
logger.info ("nearest.getType()--{}", nearest.getType());
nearest.attack();
} else {
logger.info ("nearest.getType()--{}", nearest.getType());
nearest.dispose();
}
}
Sound attackSound = ServiceLocator.getResourceService().getAsset("sounds/Impact4.ogg", Sound.class);
attackSound.play();
animator.startAnimation("attack");
}
- Attack animation:
public static Entity createPlayer() {
InputComponent inputComponent =
ServiceLocator.getInputService().getInputFactory().createForPlayer();
AnimationRenderComponent animator =
new AnimationRenderComponent(
ServiceLocator.getResourceService()
.getAsset("images/attack.atlas",
TextureAtlas.class));
animator.addAnimation("attack", 10f, Animation.PlayMode.LOOP); (Animation)
}
void unAttack(){
animator.stopAnimation();
} //new function connceted to the space key to stop attack animation
- Attack animation design: When the space button is pressed, the player texture will be switch to attack animation. And after the space key go up the animation will switch to the origanl player.use sword and let the player wave his hands to stimulate the attack action.