Skip to content

Commit

Permalink
Hook the creature attacks to the shot system
Browse files Browse the repository at this point in the history
  • Loading branch information
tonihele committed Oct 19, 2024
1 parent 7934547 commit f449103
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ public ICreatureController createController(EntityId entityId) {
}

private ICreatureController createCreatureController(EntityId id, CreatureComponent creatureComponent) {
return new CreatureController(id, entityData, kwdFile.getCreature(creatureComponent.creatureId), gameController.getNavigationService(), gameController.getTaskManager(), gameTimer, gameSettings, this, gameController.getEntityLookupService(), mapController, levelInfo, gameController.getGameWorldController().getObjectsController());
return new CreatureController(id, entityData, kwdFile.getCreature(creatureComponent.creatureId), gameController.getNavigationService(), gameController.getTaskManager(), gameTimer, gameSettings, this, gameController.getEntityLookupService(), mapController, levelInfo, gameController.getGameWorldController().getObjectsController(), gameController.getGameWorldController().getShotsController());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -928,7 +928,7 @@ public void castKeeperSpell(short keeperSpellId, EntityId target, Point tile, Ve
boolean spellUpgraded = researchableEntity.isUpgraded();
int shotData1 = spellUpgraded ? keeperSpell.getBonusShotData1() : keeperSpell.getShotData1();
int shotData2 = spellUpgraded ? keeperSpell.getBonusShotData2() : keeperSpell.getShotData2();
shotsController.createShot(keeperSpell.getShotTypeId(), shotData1, shotData2, playerId, position, target);
shotsController.createShot(keeperSpell.getShotTypeId(), shotData1, shotData2, playerId, WorldUtils.vector2fToVector3(position), target);
}

@Override
Expand Down Expand Up @@ -995,6 +995,11 @@ public ITrapsController getTrapsController() {
return trapsController;
}

@Override
public IShotsController getShotsController() {
return shotsController;
}

public void setEntityPositionLookup(IEntityPositionLookup entityPositionLookup) {
this.entityPositionLookup = entityPositionLookup;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,13 @@ public interface IGameWorldController {
*/
public IObjectsController getObjectsController();

/**
* Get the shots controller
*
* @return shots controller
*/
public IShotsController getShotsController();

/**
* Cast a keeper spell on target / location
*
Expand Down
8 changes: 4 additions & 4 deletions src/toniarts/openkeeper/game/controller/IShotsController.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
package toniarts.openkeeper.game.controller;

import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f;
import com.simsilica.es.EntityId;

/**
Expand All @@ -33,10 +33,10 @@ public interface IShotsController {
* @param shotTypeId shot type to create
* @param shotData1 arbitrary value, interpreted per shot
* @param shotData2 arbitrary value, interpreted per shot
* @param playerId owher of the shot
* @param position 2D coordinate of the shot origin
* @param playerId owner of the shot
* @param position coordinate of the shot origin
* @param target shot target, can be null
*/
public void createShot(short shotTypeId, int shotData1, int shotData2, short playerId, Vector2f position, EntityId target);
public void createShot(short shotTypeId, int shotData1, int shotData2, short playerId, Vector3f position, EntityId target);

}
7 changes: 4 additions & 3 deletions src/toniarts/openkeeper/game/controller/ShotsController.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
package toniarts.openkeeper.game.controller;

import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f;
import com.simsilica.es.EntityData;
import com.simsilica.es.EntityId;
import java.lang.System.Logger;
Expand All @@ -29,6 +29,7 @@
import static toniarts.openkeeper.tools.convert.map.Shot.ProcessType.MODIFY_HEALTH;
import static toniarts.openkeeper.tools.convert.map.Shot.ProcessType.POSSESS_CREATURE;
import toniarts.openkeeper.tools.convert.map.Variable;
import toniarts.openkeeper.utils.WorldUtils;

/**
*
Expand Down Expand Up @@ -62,11 +63,11 @@ public ShotsController(KwdFile kwdFile, EntityData entityData, Map<Variable.Misc
}

@Override
public void createShot(short shotTypeId, int shotData1, int shotData2, short playerId, Vector2f position, EntityId target) {
public void createShot(short shotTypeId, int shotData1, int shotData2, short playerId, Vector3f position, EntityId target) {
Shot shot = kwdFile.getShotById(shotTypeId);
switch (shot.getProcessType()) {
case CREATE_CREATURE -> {
creaturesController.spawnCreature((short) shotData1, playerId, shotData2, position, ICreaturesController.SpawnType.CONJURE);
creaturesController.spawnCreature((short) shotData1, playerId, shotData2, WorldUtils.vector3fToVector2f(position), ICreaturesController.SpawnType.CONJURE);
}
case CREATE_OBJECT -> {
objectsController.loadObject((short) shotData1, playerId, position, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
import toniarts.openkeeper.game.controller.ILevelInfo;
import toniarts.openkeeper.game.controller.IMapController;
import toniarts.openkeeper.game.controller.IObjectsController;
import toniarts.openkeeper.game.controller.IShotsController;
import static toniarts.openkeeper.game.controller.creature.CreatureState.MELEE_ATTACK;
import toniarts.openkeeper.game.controller.entity.EntityController;
import toniarts.openkeeper.game.controller.entity.IEntityController;
Expand Down Expand Up @@ -112,6 +113,7 @@ public class CreatureController extends EntityController implements ICreatureCon
private final ICreaturesController creaturesController;
private final IEntityPositionLookup entityPositionLookup;
private final ILevelInfo levelInfo;
private final IShotsController shotsController;
// TODO: All the data is not supposed to be on entities as they become too big, but I don't want these here either
private final Creature creature;
private final StateMachine<ICreatureController, CreatureState> stateMachine;
Expand All @@ -120,7 +122,7 @@ public class CreatureController extends EntityController implements ICreatureCon
public CreatureController(EntityId entityId, EntityData entityData, Creature creature, INavigationService navigationService,
ITaskManager taskManager, IGameTimer gameTimer, Map<Variable.MiscVariable.MiscType, Variable.MiscVariable> gameSettings,
ICreaturesController creaturesController, IEntityPositionLookup entityPositionLookup, IMapController mapController,
ILevelInfo levelInfo, IObjectsController objectsController) {
ILevelInfo levelInfo, IObjectsController objectsController, IShotsController shotsController) {
super(entityId, entityData, objectsController, mapController);
this.navigationService = navigationService;
this.taskManager = taskManager;
Expand All @@ -130,6 +132,7 @@ public CreatureController(EntityId entityId, EntityData entityData, Creature cre
this.creaturesController = creaturesController;
this.entityPositionLookup = entityPositionLookup;
this.levelInfo = levelInfo;
this.shotsController = shotsController;
this.stateMachine = new DefaultStateMachine<>(this);
}

Expand Down Expand Up @@ -662,9 +665,13 @@ public void executeAttack(EntityId attackTarget) {
}
stateMachine.changeState(CreatureState.CAST_SPELL);

// TODO: What is the alternative shot all about?
toniarts.openkeeper.tools.convert.map.CreatureSpell creatureSpell = levelInfo.getLevelData().getCreatureSpellById(attack.get().creatureSpellId);
shotsController.createShot(creatureSpell.getShotTypeId(), creatureSpell.getShotData1(), creatureSpell.getShotData2(), getOwnerId(), getPosition(), attackTarget);

// TODO: Of course not like that, the shot is created and it does what it does
// Set the damage
setDamage(attackTarget, levelInfo.getLevelData().getCreatureSpellById(attack.get().creatureSpellId).getShotData1());
setDamage(attackTarget, creatureSpell.getShotData1());
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/toniarts/openkeeper/utils/WorldUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,14 @@ public static Vector2 vector3fToVector2(Vector3f v) {
return new Vector2(v.x, v.z);
}

public static Vector3f vector2fToVector3(Vector2f v) {
return new Vector3f(v.x, TILE_HEIGHT / 2f, v.y);
}

public static Vector2f vector3fToVector2f(Vector3f v) {
return new Vector2f(v.x, v.z);
}

/**
* Get surrounding tile coordinates
*
Expand Down

0 comments on commit f449103

Please sign in to comment.