Add icons for weapons

This commit is contained in:
2022-08-24 23:14:21 +02:00
parent 60acb5749d
commit b157d7bf26
12 changed files with 422 additions and 435 deletions

View File

@@ -1,65 +1,62 @@
package com.bartlomiejpluta.demo.world.weapon;
import com.bartlomiejpluta.base.api.context.*;
import com.bartlomiejpluta.base.api.icon.Icon;
import com.bartlomiejpluta.base.lib.animation.*;
import com.bartlomiejpluta.base.util.random.DiceRoller;
import com.bartlomiejpluta.demo.entity.Creature;
import com.bartlomiejpluta.demo.event.HitEvent;
import lombok.*;
import org.joml.Vector2i;
import java.util.Random;
import lombok.*;
import org.joml.Vector2i;
import com.bartlomiejpluta.base.api.context.*;
import com.bartlomiejpluta.base.lib.animation.*;
import com.bartlomiejpluta.base.util.random.DiceRoller;
import com.bartlomiejpluta.demo.entity.Creature;
import com.bartlomiejpluta.demo.event.HitEvent;
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 String name;
@Getter
private String name;
@Getter
private int cooldown;
@Getter
private final Icon icon;
public MeleeWeapon(@NonNull String id) {
this(DB.dao.melee_weapon.find(id));
}
@Getter
private int cooldown;
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;
}
public MeleeWeapon(@NonNull String id) {
this(DB.dao.melee_weapon.find(id));
}
@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 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]));
}
return false;
}
@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;
}
}

View File

@@ -1,89 +1,81 @@
package com.bartlomiejpluta.demo.world.weapon;
import java.util.Random;
import lombok.*;
import com.bartlomiejpluta.base.api.animation.Animation;
import com.bartlomiejpluta.base.api.context.*;
import com.bartlomiejpluta.base.api.entity.Entity;
import com.bartlomiejpluta.base.api.animation.Animation;
import com.bartlomiejpluta.base.api.icon.Icon;
import com.bartlomiejpluta.base.api.move.*;
import com.bartlomiejpluta.base.lib.animation.*;
import com.bartlomiejpluta.base.util.random.DiceRoller;
import com.bartlomiejpluta.demo.entity.Creature;
import com.bartlomiejpluta.demo.event.HitEvent;
import lombok.*;
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 String name;
@Getter
private String name;
@Getter
private int cooldown;
@Getter
private final Icon icon;
public RangedWeapon(@NonNull String id) {
this(DB.dao.ranged_weapon.find(id));
}
@Getter
private int cooldown;
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;
}
public RangedWeapon(@NonNull String id) {
this(DB.dao.ranged_weapon.find(id));
}
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 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 onMiss(Movable attacker, Animation animation) {
missAnimation.run(context, ((Creature) attacker).getLayer(), animation.getPosition());
context.playSound(missSound);
}
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));
}
}
@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;
}
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;
}
}

View File

@@ -1,9 +1,14 @@
package com.bartlomiejpluta.demo.world.weapon;
import com.bartlomiejpluta.base.api.icon.Icon;
import com.bartlomiejpluta.demo.entity.Creature;
public interface Weapon {
String getName();
int getCooldown();
boolean attack(Creature attacker);
String getName();
Icon getIcon();
int getCooldown();
boolean attack(Creature attacker);
}