Player interaction Sprint2 - UQdeco2800/2021-ext-studio-1 GitHub Wiki
The Player can interact with other Entities in the game.
Animation of buff
- Animation of buff -- When the player touches "food" and "firstAidKit", they will gain buffs. Animation will be triggered at this time.
Animation of debuff
- Animation of debuff -- When the player touches something debuff, such as "snake" and "fire", they will lose buffs. Animation will be triggered at this time.
Sound of buff
- sound of buff -- When the player touches "food" and "firstAidKit", they will gain buffs. Sound will be triggered at this time.
Sound of debuff
- Animation of debuff -- When the player touches something debuff, such as "snake" and "fire", they will lose buffs. Sound will be triggered at this time.
Code Usage
- Increase Animations of buff and debuff:
AnimationRenderComponent2 animator2 =
new AnimationRenderComponent2(
ServiceLocator.getResourceService()
.getAsset("images/touch.atlas",
TextureAtlas.class));
animator2.addAnimation("touch", 0.1f, Animation.PlayMode.NORMAL);
AnimationRenderComponent animator =
new AnimationRenderComponent(
ServiceLocator.getResourceService()
.getAsset("images/attack.atlas",
TextureAtlas.class));
animator.addAnimation("attack", 0.03f, Animation.PlayMode.NORMAL);
AnimationRenderComponent3 animator3 =
new AnimationRenderComponent3(
ServiceLocator.getResourceService()
.getAsset("images/posipuff.atlas",
TextureAtlas.class));
animator3.addAnimation("buff", 0.03f, Animation.PlayMode.NORMAL);
AnimationRenderComponent4 animator4 =
new AnimationRenderComponent4(
ServiceLocator.getResourceService()
.getAsset("images/negbuff.atlas",
TextureAtlas.class));
animator4.addAnimation("deBuff", 0.03f, Animation.PlayMode.NORMAL);
- After the five animation become one, the code will change to:
AnimationRenderComponent animator =
new AnimationRenderComponent(
ServiceLocator.getResourceService()
.getAsset("images/player.atlas",
TextureAtlas.class));
animator.addAnimation("run", 0.03f, Animation.PlayMode.NORMAL);
animator.addAnimation("attack", 0.03f, Animation.PlayMode.NORMAL);
animator.addAnimation("touch", 0.03f, Animation.PlayMode.NORMAL);
animator.addAnimation("buff", 0.03f, Animation.PlayMode.NORMAL);
animator.addAnimation("deBuff", 0.03f, Animation.PlayMode.NORMAL);
- Implement animations and sounds of buff and debuff.
public void hit(CombatStatsComponent attacker) {
try {
if (ServiceLocator.getTimeSource().getTimeSince(invincibleStart) < 1000L) {
return;
}
if (attacker.getEntity().getType() == Entity.Type.PLAYER) {
logger.error("attacker--{}", attacker.getEntity().getType());
AnimationRenderComponent2 animator = attacker.getEntity().getComponent(AnimationRenderComponent2.class);
animator.startAnimation("touch");
Sound attackSound = ServiceLocator.getResourceService().getAsset("sounds/e.ogg", Sound.class);
attackSound.play();
logger.error("--end--attacker--{}",attacker.getEntity().getType());
}
if (armour > 0){
int newArmour = getArmour() - attacker.getBaseAttack();
setArmour(newArmour);
invincibleStart = ServiceLocator.getTimeSource().getTime();
}
else{
int newHealth = getHealth() - attacker.getBaseAttack();
setHealth(newHealth);
invincibleStart = ServiceLocator.getTimeSource().getTime();
}
} catch (NullPointerException e) {
int newHealth = getHealth() - attacker.getBaseAttack();
setHealth(newHealth);
}
}
public void hitBuff(CombatStatsComponent attacker) {
try {
if (ServiceLocator.getTimeSource().getTimeSince(invincibleStart) < 1000L) {
return;
}
if (attacker.getEntity().getType() == Entity.Type.PLAYER) {
logger.error("attacker--{}", attacker.getEntity().getType());
AnimationRenderComponent3 animator =
attacker.getEntity().getComponent(AnimationRenderComponent3.class);
animator.startAnimation("buff");
Sound attackSound = ServiceLocator.getResourceService().getAsset(
"sounds/buff.ogg", Sound.class);
attackSound.play();
logger.error("--end--attacker--{}",attacker.getEntity().getType());
}
if (armour > 0){
int newArmour = getArmour() - attacker.getBaseAttack();
setArmour(newArmour);
invincibleStart = ServiceLocator.getTimeSource().getTime();
}
else{
int newHealth = getHealth() - attacker.getBaseAttack();
setHealth(newHealth);
invincibleStart = ServiceLocator.getTimeSource().getTime();
}
} catch (NullPointerException e) {
int newHealth = getHealth() - attacker.getBaseAttack();
setHealth(newHealth);
}
}
public void hitDeBuff(CombatStatsComponent attacker) {
try {
if (ServiceLocator.getTimeSource().getTimeSince(invincibleStart) < 1000L) {
return;
}
if (attacker.getEntity().getType() == Entity.Type.PLAYER) {
logger.error("attacker--{}", attacker.getEntity().getType());
AnimationRenderComponent4 animator =
attacker.getEntity().getComponent(AnimationRenderComponent4.class);
animator.startAnimation("deBuff");
Sound attackSound = ServiceLocator.getResourceService().getAsset(
"sounds/buff2.ogg", Sound.class);
attackSound.play();
logger.error("--end--attacker--{}",attacker.getEntity().getType());
}
if (armour > 0){
int newArmour = getArmour() - attacker.getBaseAttack();
setArmour(newArmour);
invincibleStart = ServiceLocator.getTimeSource().getTime();
}
else{
int newHealth = getHealth() - attacker.getBaseAttack();
setHealth(newHealth);
invincibleStart = ServiceLocator.getTimeSource().getTime();
}
} catch (NullPointerException e) {
int newHealth = getHealth() - attacker.getBaseAttack();
setHealth(newHealth);
}
}
Entity target = ((BodyUserData) other.getBody().getUserData()).entity;
CombatStatsComponent targetStats = target.getComponent(CombatStatsComponent.class);
if (targetStats != null) {
if(targetStats.getEntity().getType() == Entity.Type.BUFF){
targetStats.hitBuff(combatStats);
}
else if(targetStats.getEntity().getType() == Entity.Type.DEBUFF){
targetStats.hitDeBuff(combatStats);
}
else{
targetStats.hit(combatStats);
}
}