Perform general code cleaning & refactoring

This commit is contained in:
2022-08-25 12:27:24 +02:00
parent 4f7c6c6dc3
commit 8946297bd9
28 changed files with 1056 additions and 1091 deletions

View File

@@ -1,62 +1,62 @@
package com.bartlomiejpluta.demo.world.weapon;
import com.bartlomiejpluta.base.api.context.*;
import com.bartlomiejpluta.base.api.context.Context;
import com.bartlomiejpluta.base.api.context.ContextHolder;
import com.bartlomiejpluta.base.api.icon.Icon;
import com.bartlomiejpluta.base.lib.animation.*;
import com.bartlomiejpluta.base.lib.animation.AnimationRunner;
import com.bartlomiejpluta.base.lib.animation.RandomAnimationsRunner;
import com.bartlomiejpluta.base.util.random.DiceRoller;
import com.bartlomiejpluta.demo.entity.Creature;
import com.bartlomiejpluta.demo.event.HitEvent;
import lombok.*;
import lombok.Getter;
import lombok.NonNull;
import org.joml.Vector2i;
import java.util.Random;
public class MeleeWeapon implements Weapon {
private final Random random = new Random();
private final Context context;
private final DiceRoller roller;
private final AnimationRunner animation;
private final String sound;
private final Random random = new Random();
private final Context context;
private final DiceRoller roller;
private final AnimationRunner animation;
private final String sound;
@Getter
private final Icon icon;
@Getter
private final String name;
@Getter
private final int cooldown;
@Getter
private String name;
public MeleeWeapon(@NonNull String id) {
this(DB.dao.melee_weapon.find(id));
}
@Getter
private final Icon icon;
public MeleeWeapon(@NonNull DB.model.MeleeWeaponModel template) {
this.context = ContextHolder.INSTANCE.getContext();
this.name = template.getName();
this.roller = DiceRoller.of(template.getDamage());
this.cooldown = template.getCooldown();
this.animation = new RandomAnimationsRunner(2).nRange(0, 2f).nScale(0.2f, 0.15f).uAnimationSpeed(0.5f, 1f).nRotation(0, 10).offset(0, -10).uDelay(250, 500).with(A.animations.get(template.getAnimation()).uid);
this.sound = A.sounds.get(template.getSound()).uid;
var icons = template.getIcon().split(",");
this.icon = context.createIcon(A.iconsets.get(icons[0]).uid, Integer.valueOf(icons[1]), Integer.valueOf(icons[2]));
}
@Getter
private int cooldown;
@Override
public boolean attack(Creature attacker) {
var facingNeighbour = attacker.getCoordinates().add(attacker.getFaceDirection().vector, new Vector2i());
for (var entity : attacker.getLayer().getEntities()) {
if (entity.getCoordinates().equals(facingNeighbour) && entity.isBlocking() && entity instanceof Creature) {
var character = (Creature) entity;
var damage = roller.roll();
character.hit(attacker, damage);
animation.run(context, character.getLayer(), character);
context.playSound(sound);
context.fireEvent(new HitEvent(attacker, character, damage));
return true;
}
}
public MeleeWeapon(@NonNull String id) {
this(DB.dao.melee_weapon.find(id));
}
public MeleeWeapon(@NonNull DB.model.MeleeWeaponModel template) {
this.context = ContextHolder.INSTANCE.getContext();
this.name = template.getName();
this.roller = DiceRoller.of(template.getDamage());
this.cooldown = template.getCooldown();
this.animation = new RandomAnimationsRunner(2).nRange(0, 2f).nScale(0.2f, 0.15f).uAnimationSpeed(0.5f, 1f).nRotation(0, 10).offset(0, -10).uDelay(250, 500).with(A.animations.get(template.getAnimation()).uid);
this.sound = A.sounds.get(template.getSound()).uid;
var icons = template.getIcon().split(",");
this.icon = context.createIcon(A.iconsets.get(icons[0]).uid, Integer.valueOf(icons[1]), Integer.valueOf(icons[2]));
}
@Override
public boolean attack(Creature attacker) {
var facingNeighbour = attacker.getCoordinates().add(attacker.getFaceDirection().vector, new Vector2i());
for (var entity : attacker.getLayer().getEntities()) {
if (entity.getCoordinates().equals(facingNeighbour) && entity.isBlocking() && entity instanceof Creature) {
var character = (Creature) entity;
var damage = roller.roll();
character.hit(attacker, damage);
animation.run(context, character.getLayer(), character);
context.playSound(sound);
context.fireEvent(new HitEvent(attacker, character, damage));
return true;
}
}
return false;
}
return false;
}
}

View File

@@ -1,81 +1,82 @@
package com.bartlomiejpluta.demo.world.weapon;
import com.bartlomiejpluta.base.api.animation.Animation;
import com.bartlomiejpluta.base.api.context.*;
import com.bartlomiejpluta.base.api.context.Context;
import com.bartlomiejpluta.base.api.context.ContextHolder;
import com.bartlomiejpluta.base.api.entity.Entity;
import com.bartlomiejpluta.base.api.icon.Icon;
import com.bartlomiejpluta.base.api.move.*;
import com.bartlomiejpluta.base.lib.animation.*;
import com.bartlomiejpluta.base.api.move.Movable;
import com.bartlomiejpluta.base.lib.animation.AnimationRunner;
import com.bartlomiejpluta.base.lib.animation.BulletAnimationRunner;
import com.bartlomiejpluta.base.lib.animation.SimpleAnimationRunner;
import com.bartlomiejpluta.base.util.random.DiceRoller;
import com.bartlomiejpluta.demo.entity.Creature;
import com.bartlomiejpluta.demo.event.HitEvent;
import lombok.*;
import lombok.Getter;
import lombok.NonNull;
import java.util.Random;
public class RangedWeapon implements Weapon {
private final Random random = new Random();
private final Context context;
private final DiceRoller dmgRoller;
private final DiceRoller rangeRoller;
private final BulletAnimationRunner animation;
private final String sound;
private final AnimationRunner punchAnimation;
private final String punchSound;
private final AnimationRunner missAnimation;
private final String missSound;
private final Random random = new Random();
private final Context context;
private final DiceRoller dmgRoller;
private final DiceRoller rangeRoller;
private final BulletAnimationRunner animation;
private final String sound;
private final AnimationRunner punchAnimation;
private final String punchSound;
private final AnimationRunner missAnimation;
private final String missSound;
@Getter
private final Icon icon;
@Getter
private final String name;
@Getter
private final int cooldown;
@Getter
private String name;
public RangedWeapon(@NonNull String id) {
this(DB.dao.ranged_weapon.find(id));
}
@Getter
private final Icon icon;
public RangedWeapon(@NonNull DB.model.RangedWeaponModel template) {
this.context = ContextHolder.INSTANCE.getContext();
this.name = template.getName();
this.dmgRoller = DiceRoller.of(template.getDamage());
this.rangeRoller = DiceRoller.of(template.getRange());
this.cooldown = template.getCooldown();
this.animation = new BulletAnimationRunner(A.animations.get(template.getAnimation()).uid).infinite().offset(0, -15).onHit(this::onHit).onMiss(this::onMiss).speed(7f).animationSpeed(4f).scale(0.6f);
this.sound = A.sounds.get(template.getSound()).uid;
this.punchAnimation = new SimpleAnimationRunner(A.animations.get(template.getPunchAnimation()).uid);
this.punchSound = A.sounds.get(template.getPunchSound()).uid;
this.missAnimation = new SimpleAnimationRunner(A.animations.get(template.getMissAnimation()).uid).scale(0.4f);
this.missSound = A.sounds.get(template.getMissSound()).uid;
var icons = template.getIcon().split(",");
this.icon = context.createIcon(A.iconsets.get(icons[0]).uid, Integer.valueOf(icons[1]), Integer.valueOf(icons[2]));
}
@Getter
private int cooldown;
private void onHit(Movable attacker, Entity target) {
if (target.isBlocking() && target instanceof Creature) {
var namedAttacker = (Creature) attacker;
var character = (Creature) target;
var damage = dmgRoller.roll();
character.hit(namedAttacker, damage);
punchAnimation.run(context, character.getLayer(), character);
context.playSound(punchSound);
context.fireEvent(new HitEvent(namedAttacker, character, damage));
}
}
public RangedWeapon(@NonNull String id) {
this(DB.dao.ranged_weapon.find(id));
}
private void onMiss(Movable attacker, Animation animation) {
missAnimation.run(context, ((Creature) attacker).getLayer(), animation.getPosition());
context.playSound(missSound);
}
public RangedWeapon(@NonNull DB.model.RangedWeaponModel template) {
this.context = ContextHolder.INSTANCE.getContext();
this.name = template.getName();
this.dmgRoller = DiceRoller.of(template.getDamage());
this.rangeRoller = DiceRoller.of(template.getRange());
this.cooldown = template.getCooldown();
this.animation = new BulletAnimationRunner(A.animations.get(template.getAnimation()).uid).infinite().offset(0, -15).onHit(this::onHit).onMiss(this::onMiss).speed(7f).animationSpeed(4f).scale(0.6f);
this.sound = A.sounds.get(template.getSound()).uid;
this.punchAnimation = new SimpleAnimationRunner(A.animations.get(template.getPunchAnimation()).uid);
this.punchSound = A.sounds.get(template.getPunchSound()).uid;
this.missAnimation = new SimpleAnimationRunner(A.animations.get(template.getMissAnimation()).uid).scale(0.4f);
this.missSound = A.sounds.get(template.getMissSound()).uid;
var icons = template.getIcon().split(",");
this.icon = context.createIcon(A.iconsets.get(icons[0]).uid, Integer.valueOf(icons[1]), Integer.valueOf(icons[2]));
}
private void onHit(Movable attacker, Entity target) {
if (target.isBlocking() && target instanceof Creature) {
var namedAttacker = (Creature) attacker;
var character = (Creature) target;
var damage = dmgRoller.roll();
character.hit(namedAttacker, damage);
punchAnimation.run(context, character.getLayer(), character);
context.playSound(punchSound);
context.fireEvent(new HitEvent(namedAttacker, character, damage));
}
}
private void onMiss(Movable attacker, Animation animation) {
missAnimation.run(context, ((Creature) attacker).getLayer(), animation.getPosition());
context.playSound(missSound);
}
@Override
public boolean attack(Creature attacker) {
var direction = attacker.getFaceDirection();
context.playSound(sound);
animation.range(rangeRoller.roll()).direction(direction).rotation(direction.xAngle - 180).run(context, attacker.getLayer(), attacker);
return true;
}
@Override
public boolean attack(Creature attacker) {
var direction = attacker.getFaceDirection();
context.playSound(sound);
animation.range(rangeRoller.roll()).direction(direction).rotation(direction.xAngle - 180).run(context, attacker.getLayer(), attacker);
return true;
}
}

View File

@@ -4,11 +4,11 @@ import com.bartlomiejpluta.base.api.icon.Icon;
import com.bartlomiejpluta.demo.entity.Creature;
public interface Weapon {
String getName();
String getName();
Icon getIcon();
Icon getIcon();
int getCooldown();
int getCooldown();
boolean attack(Creature attacker);
boolean attack(Creature attacker);
}