Create melee weapons system
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1 +1,2 @@
|
|||||||
build/
|
build/
|
||||||
|
data.trace.db
|
||||||
|
|||||||
BIN
data.mv.db
BIN
data.mv.db
Binary file not shown.
@@ -0,0 +1,32 @@
|
|||||||
|
package com.bartlomiejpluta.demo.database.dao;
|
||||||
|
|
||||||
|
import java.sql.*;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
import com.bartlomiejpluta.base.api.context.Context;
|
||||||
|
|
||||||
|
import com.bartlomiejpluta.demo.database.model.MeleeWeaponModel;
|
||||||
|
|
||||||
|
public class MeleeWeaponDAO {
|
||||||
|
private final Map<String, MeleeWeaponModel> items = new HashMap<>();
|
||||||
|
|
||||||
|
public void init(Context context) {
|
||||||
|
context.withDatabase(db -> {
|
||||||
|
var result = db.prepareStatement("SELECT * FROM `melee_weapon`").executeQuery();
|
||||||
|
|
||||||
|
while(result.next()) {
|
||||||
|
var weapon = MeleeWeaponModel.builder()
|
||||||
|
.id(result.getString("id"))
|
||||||
|
.name(result.getString("name"))
|
||||||
|
.damage(result.getString("damage"))
|
||||||
|
.cooldown(result.getInt("cooldown"))
|
||||||
|
.build();
|
||||||
|
items.put(result.getString("id"), weapon);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public MeleeWeaponModel get(String id) {
|
||||||
|
return items.get(id);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package com.bartlomiejpluta.demo.database.model;
|
||||||
|
|
||||||
|
import lombok.*;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
public class MeleeWeaponModel {
|
||||||
|
private final String id;
|
||||||
|
private final String name;
|
||||||
|
private final String damage;
|
||||||
|
private final int cooldown;
|
||||||
|
}
|
||||||
@@ -1,15 +1,53 @@
|
|||||||
package com.bartlomiejpluta.demo.entity;
|
package com.bartlomiejpluta.demo.entity;
|
||||||
|
|
||||||
import lombok.*;
|
import lombok.*;
|
||||||
|
import org.slf4j.*;
|
||||||
|
import org.joml.Vector2i;
|
||||||
import com.bartlomiejpluta.base.api.context.Context;
|
import com.bartlomiejpluta.base.api.context.Context;
|
||||||
import com.bartlomiejpluta.base.api.entity.Entity;
|
import com.bartlomiejpluta.base.api.entity.Entity;
|
||||||
import com.bartlomiejpluta.base.lib.entity.EntityDelegate;
|
import com.bartlomiejpluta.base.lib.entity.EntityDelegate;
|
||||||
|
|
||||||
|
import com.bartlomiejpluta.demo.world.weapon.MeleeWeapon;
|
||||||
|
|
||||||
public class Character extends EntityDelegate {
|
public class Character extends EntityDelegate {
|
||||||
|
private static final Logger log = LoggerFactory.getLogger(Character.class);
|
||||||
protected final Context context;
|
protected final Context context;
|
||||||
|
protected int attackCooldown = 0;
|
||||||
|
|
||||||
|
@Setter
|
||||||
|
private MeleeWeapon weapon;
|
||||||
|
|
||||||
public Character(@NonNull Context context, @NonNull Entity entity) {
|
public Character(@NonNull Context context, @NonNull Entity entity) {
|
||||||
super(entity);
|
super(entity);
|
||||||
this.context = context;
|
this.context = context;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void attack() {
|
||||||
|
if(weapon == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(attackCooldown >= weapon.getCooldown()) {
|
||||||
|
var facingNeighbour = getCoordinates().add(getFaceDirection().vector, new Vector2i());
|
||||||
|
for(var entity : getLayer().getEntities()) {
|
||||||
|
if(entity.getCoordinates().equals(facingNeighbour) && entity.isBlocking() && entity instanceof Character) {
|
||||||
|
weapon.attack((Character) entity);
|
||||||
|
attackCooldown = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void hit(int dmg) {
|
||||||
|
log.info(toString() + " received " + dmg + " damage");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void update(float dt) {
|
||||||
|
super.update(dt);
|
||||||
|
|
||||||
|
if(weapon != null && attackCooldown < weapon.getCooldown()) {
|
||||||
|
attackCooldown += (int) (dt * 1000f);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -13,6 +13,7 @@ import com.bartlomiejpluta.base.lib.camera.*;
|
|||||||
|
|
||||||
import com.bartlomiejpluta.demo.runner.DemoRunner;
|
import com.bartlomiejpluta.demo.runner.DemoRunner;
|
||||||
import com.bartlomiejpluta.demo.entity.Player;
|
import com.bartlomiejpluta.demo.entity.Player;
|
||||||
|
import com.bartlomiejpluta.demo.entity.Character;
|
||||||
|
|
||||||
public abstract class BaseMapHandler implements MapHandler {
|
public abstract class BaseMapHandler implements MapHandler {
|
||||||
protected Screen screen;
|
protected Screen screen;
|
||||||
@@ -41,6 +42,10 @@ public abstract class BaseMapHandler implements MapHandler {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(input.isKeyPressed(Key.KEY_SPACE)) {
|
||||||
|
player.attack();
|
||||||
|
}
|
||||||
|
|
||||||
if(input.isKeyPressed(Key.KEY_LEFT_CONTROL)) {
|
if(input.isKeyPressed(Key.KEY_LEFT_CONTROL)) {
|
||||||
if(input.isKeyPressed(Key.KEY_DOWN)) {
|
if(input.isKeyPressed(Key.KEY_DOWN)) {
|
||||||
player.setFaceDirection(Direction.DOWN);
|
player.setFaceDirection(Direction.DOWN);
|
||||||
|
|||||||
@@ -13,6 +13,9 @@ import com.bartlomiejpluta.base.api.runner.GameRunner;
|
|||||||
import com.bartlomiejpluta.demo.map.ForrestTempleHandler;
|
import com.bartlomiejpluta.demo.map.ForrestTempleHandler;
|
||||||
import com.bartlomiejpluta.demo.entity.Player;
|
import com.bartlomiejpluta.demo.entity.Player;
|
||||||
import com.bartlomiejpluta.demo.menu.MenuManager;
|
import com.bartlomiejpluta.demo.menu.MenuManager;
|
||||||
|
import com.bartlomiejpluta.demo.database.dao.MeleeWeaponDAO;
|
||||||
|
|
||||||
|
import com.bartlomiejpluta.demo.world.weapon.MeleeWeapon;
|
||||||
|
|
||||||
public class DemoRunner implements GameRunner {
|
public class DemoRunner implements GameRunner {
|
||||||
private static final Logger log = LoggerFactory.getLogger(DemoRunner.class);
|
private static final Logger log = LoggerFactory.getLogger(DemoRunner.class);
|
||||||
@@ -20,6 +23,9 @@ public class DemoRunner implements GameRunner {
|
|||||||
private Context context;
|
private Context context;
|
||||||
private MenuManager menu;
|
private MenuManager menu;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
private MeleeWeaponDAO meleeWeaponDAO = new MeleeWeaponDAO();
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
private Player player;
|
private Player player;
|
||||||
|
|
||||||
@@ -27,9 +33,10 @@ public class DemoRunner implements GameRunner {
|
|||||||
public void init(Context context) {
|
public void init(Context context) {
|
||||||
this.context = context;
|
this.context = context;
|
||||||
this.screen = context.getScreen();
|
this.screen = context.getScreen();
|
||||||
|
|
||||||
configureScreen();
|
configureScreen();
|
||||||
configureCamera();
|
configureCamera();
|
||||||
|
initDAOs();
|
||||||
initMenu();
|
initMenu();
|
||||||
initPlayer();
|
initPlayer();
|
||||||
|
|
||||||
@@ -37,17 +44,21 @@ public class DemoRunner implements GameRunner {
|
|||||||
|
|
||||||
screen.show();
|
screen.show();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void configureScreen() {
|
private void configureScreen() {
|
||||||
var resolution = screen.getCurrentResolution();
|
var resolution = screen.getCurrentResolution();
|
||||||
screen.setSize(800, 600);
|
screen.setSize(800, 600);
|
||||||
screen.setPosition((resolution.x() - 800)/2, (resolution.y() - 600)/2);
|
screen.setPosition((resolution.x() - 800)/2, (resolution.y() - 600)/2);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void configureCamera() {
|
private void configureCamera() {
|
||||||
context.getCamera().setScale(2f);
|
context.getCamera().setScale(2f);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void initDAOs() {
|
||||||
|
meleeWeaponDAO.init(context);
|
||||||
|
}
|
||||||
|
|
||||||
private void initMenu() {
|
private void initMenu() {
|
||||||
this.menu = new MenuManager(this, context);
|
this.menu = new MenuManager(this, context);
|
||||||
}
|
}
|
||||||
@@ -55,7 +66,7 @@ public class DemoRunner implements GameRunner {
|
|||||||
private void initPlayer() {
|
private void initPlayer() {
|
||||||
this.player = new Player(context, context.createEntity("815a5c5c-4979-42f5-a42a-ccbbff9a97e5"));
|
this.player = new Player(context, context.createEntity("815a5c5c-4979-42f5-a42a-ccbbff9a97e5"));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void resetPlayer() {
|
private void resetPlayer() {
|
||||||
this.player.changeEntitySet("815a5c5c-4979-42f5-a42a-ccbbff9a97e5");
|
this.player.changeEntitySet("815a5c5c-4979-42f5-a42a-ccbbff9a97e5");
|
||||||
this.player.setScale(1.0f);
|
this.player.setScale(1.0f);
|
||||||
@@ -63,8 +74,9 @@ public class DemoRunner implements GameRunner {
|
|||||||
this.player.setAnimationSpeed(0.005f);
|
this.player.setAnimationSpeed(0.005f);
|
||||||
this.player.setBlocking(true);
|
this.player.setBlocking(true);
|
||||||
this.player.setCoordinates(0, 11);
|
this.player.setCoordinates(0, 11);
|
||||||
|
this.player.setWeapon(new MeleeWeapon(meleeWeaponDAO.get("wooden_sword")));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void newGame() {
|
public void newGame() {
|
||||||
menu.closeAll();
|
menu.closeAll();
|
||||||
menu.enableGameMenu();
|
menu.enableGameMenu();
|
||||||
@@ -95,6 +107,4 @@ public class DemoRunner implements GameRunner {
|
|||||||
public void dispose() {
|
public void dispose() {
|
||||||
// Do something after game loop is end
|
// Do something after game loop is end
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
31
src/main/java/com/bartlomiejpluta/demo/util/DiceRoller.java
Normal file
31
src/main/java/com/bartlomiejpluta/demo/util/DiceRoller.java
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
package com.bartlomiejpluta.demo.util;
|
||||||
|
|
||||||
|
import lombok.*;
|
||||||
|
import java.util.*;
|
||||||
|
import java.util.regex.*;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class DiceRoller {
|
||||||
|
private static final Pattern CODE_PATTERN = Pattern.compile("(\\d+)d(\\d+)([+-]\\d+)?");
|
||||||
|
private final Random random = new Random();
|
||||||
|
private int rolls;
|
||||||
|
private int dice;
|
||||||
|
private int modifier;
|
||||||
|
|
||||||
|
public int roll() {
|
||||||
|
var sum = modifier;
|
||||||
|
|
||||||
|
for(int i=0; i<rolls; ++i) {
|
||||||
|
sum += random.nextInt(dice) + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Math.max(0, sum);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static DiceRoller of(String code) {
|
||||||
|
var matcher = CODE_PATTERN.matcher(code);
|
||||||
|
matcher.matches();
|
||||||
|
return new DiceRoller(Integer.valueOf(matcher.group(1)), Integer.valueOf(matcher.group(2)), Integer.valueOf(matcher.group(3)));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
package com.bartlomiejpluta.demo.world.weapon;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
import lombok.*;
|
||||||
|
import com.bartlomiejpluta.demo.database.model.MeleeWeaponModel;
|
||||||
|
import com.bartlomiejpluta.demo.entity.Character;
|
||||||
|
import com.bartlomiejpluta.demo.util.DiceRoller;
|
||||||
|
|
||||||
|
public class MeleeWeapon {
|
||||||
|
private final Random random = new Random();
|
||||||
|
private final DiceRoller roller;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
private int cooldown;
|
||||||
|
|
||||||
|
public MeleeWeapon(MeleeWeaponModel template) {
|
||||||
|
this.name = template.getName();
|
||||||
|
this.roller = DiceRoller.of(template.getDamage());
|
||||||
|
this.cooldown = template.getCooldown();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void attack(Character character) {
|
||||||
|
character.hit(roller.roll());
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user