Skip to content

Commit

Permalink
Refactor to ShotsController
Browse files Browse the repository at this point in the history
  • Loading branch information
tonihele committed Jul 18, 2024
1 parent 624ef2d commit 6b33d02
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 14 deletions.
19 changes: 6 additions & 13 deletions src/toniarts/openkeeper/game/controller/GameWorldController.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@
import toniarts.openkeeper.tools.convert.map.KwdFile;
import toniarts.openkeeper.tools.convert.map.Player;
import toniarts.openkeeper.tools.convert.map.Room;
import toniarts.openkeeper.tools.convert.map.Shot;
import toniarts.openkeeper.tools.convert.map.Terrain;
import toniarts.openkeeper.tools.convert.map.Tile;
import toniarts.openkeeper.tools.convert.map.Trap;
Expand Down Expand Up @@ -108,6 +107,7 @@ public class GameWorldController implements IGameWorldController, IPlayerActions
private ICreaturesController creaturesController;
private IDoorsController doorsController;
private ITrapsController trapsController;
private IShotsController shotsController;
private IEntityPositionLookup entityPositionLookup;
private final Map<Short, IPlayerController> playerControllers;
private final SortedMap<Short, Keeper> players;
Expand Down Expand Up @@ -143,6 +143,9 @@ public void createNewGame(IGameController gameController, ILevelInfo levelInfo)
// Load the traps
trapsController = new TrapsController(kwdFile, entityData, gameSettings, gameController, levelInfo);

// Init handlers
shotsController = new ShotsController(kwdFile, entityData, gameSettings, gameTimer, gameController, mapController, levelInfo, objectsController, creaturesController);

// Setup player stuff
initPlayerMoney();
initPlayerRooms();
Expand Down Expand Up @@ -921,7 +924,7 @@ public void castKeeperSpell(short keeperSpellId, EntityId target, Point tile, Ve
}

// Where allowed to cast
boolean isSolid = kwdFile.getTerrain(mapTile.getTerrainId()).getFlags().contains(Terrain.TerrainFlag.SOLID);
boolean isSolid = mapController.isSolid(tile);
switch (keeperSpell.getCastRule()) {
case OWN_LAND: {
if (isSolid || mapTile.getOwnerId() != playerId) {
Expand Down Expand Up @@ -993,20 +996,10 @@ public void castKeeperSpell(short keeperSpellId, EntityId target, Point tile, Ve
playerControllers.get(playerId).getManaControl().updateMana(0, keeperSpell.getManaCost());

// Cast the spell
Shot shot = kwdFile.getShotById(keeperSpell.getShotTypeId());
boolean spellUpgraded = researchableEntity.isUpgraded();
int shotData1 = spellUpgraded ? keeperSpell.getBonusShotData1() : keeperSpell.getShotData1();
int shotData2 = spellUpgraded ? keeperSpell.getBonusShotData2() : keeperSpell.getShotData2();
switch (shot.getProcessType()) {
case CREATE_CREATURE -> {
creaturesController.spawnCreature((short) shotData1, playerId, shotData2, position, ICreaturesController.SpawnType.CONJURE);
}
case MODIFY_HEALTH -> {

}
default ->
logger.log(Level.WARNING, "Shot type {0} not implemented", shot.getProcessType());
}
shotsController.createShot(keeperSpell.getShotTypeId(), shotData1, shotData2, playerId, position, target);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +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;
Expand All @@ -37,6 +38,8 @@ public interface IObjectsController extends IEntityWrapper<IObjectController> {

public EntityId loadObject(short objectId, short ownerId, Vector3f pos, float rotation);

public EntityId loadObject(short objectId, short ownerId, Vector2f pos, float rotation);

public EntityId loadObject(short objectId, short ownerId, int x, int y, Integer money, ResearchableType researchableType, Short researchTypeId);

public EntityId addRoomGold(short ownerId, int x, int y, int money, int maxMoney);
Expand Down
42 changes: 42 additions & 0 deletions src/toniarts/openkeeper/game/controller/IShotsController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (C) 2014-2024 OpenKeeper
*
* OpenKeeper is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenKeeper is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenKeeper. If not, see <http://www.gnu.org/licenses/>.
*/
package toniarts.openkeeper.game.controller;

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

/**
* Handles shots. Shots are spells and weapons cast by traps, creatures and
* keepers
*
* @author Toni Helenius <[email protected]>
*/
public interface IShotsController {

/**
* Creates a shot
*
* @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 target shot target, can be null
*/
public void createShot(short shotTypeId, int shotData1, int shotData2, short playerId, Vector2f position, EntityId target);

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +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;
Expand Down Expand Up @@ -56,7 +57,6 @@
import toniarts.openkeeper.tools.convert.map.Thing;
import toniarts.openkeeper.tools.convert.map.Variable;
import toniarts.openkeeper.utils.WorldUtils;
import toniarts.openkeeper.view.map.MapViewController;

/**
* This is a controller that controls all the game objects in the world TODO:
Expand Down Expand Up @@ -158,6 +158,11 @@ public EntityId loadObject(short objectId, short ownerId, Vector3f pos, float ro
return loadObject(objectId, ownerId, pos, rotation, null, null, null, null, null);
}

@Override
public EntityId loadObject(short objectId, short ownerId, Vector2f pos, float rotation) {
return loadObject(objectId, ownerId, new Vector3f(pos.x, WorldUtils.FLOOR_HEIGHT, pos.y), rotation);
}

@Override
public EntityId loadObject(short objectId, short ownerId, int x, int y, Integer money, ResearchableType researchableType, Short researchTypeId) {
return loadObject(objectId, ownerId, x, y, 0, money, researchableType, researchTypeId, null, null);
Expand Down
84 changes: 84 additions & 0 deletions src/toniarts/openkeeper/game/controller/ShotsController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright (C) 2014-2024 OpenKeeper
*
* OpenKeeper is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenKeeper is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenKeeper. If not, see <http://www.gnu.org/licenses/>.
*/
package toniarts.openkeeper.game.controller;

import com.jme3.math.Vector2f;
import com.simsilica.es.EntityData;
import com.simsilica.es.EntityId;
import java.lang.System.Logger;
import java.util.Map;
import toniarts.openkeeper.tools.convert.map.KwdFile;
import toniarts.openkeeper.tools.convert.map.Shot;
import static toniarts.openkeeper.tools.convert.map.Shot.ProcessType.CREATE_CREATURE;
import static toniarts.openkeeper.tools.convert.map.Shot.ProcessType.CREATE_OBJECT;
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;

/**
*
* @author Toni Helenius <[email protected]>
*/
public class ShotsController implements IShotsController {

private static final Logger logger = System.getLogger(ShotsController.class.getName());

private final KwdFile kwdFile;
private final EntityData entityData;
private final Map<Variable.MiscVariable.MiscType, Variable.MiscVariable> gameSettings;
private final IGameTimer gameTimer;
private final IGameController gameController;
private final IMapController mapController;
private final ILevelInfo levelInfo;
private final IObjectsController objectsController;
private final ICreaturesController creaturesController;

public ShotsController(KwdFile kwdFile, EntityData entityData, Map<Variable.MiscVariable.MiscType, Variable.MiscVariable> gameSettings, IGameTimer gameTimer,
IGameController gameController, IMapController mapController, ILevelInfo levelInfo, IObjectsController objectsController, ICreaturesController creaturesController) {
this.kwdFile = kwdFile;
this.entityData = entityData;
this.gameSettings = gameSettings;
this.gameTimer = gameTimer;
this.gameController = gameController;
this.mapController = mapController;
this.levelInfo = levelInfo;
this.objectsController = objectsController;
this.creaturesController = creaturesController;
}

@Override
public void createShot(short shotTypeId, int shotData1, int shotData2, short playerId, Vector2f position, EntityId target) {
Shot shot = kwdFile.getShotById(shotTypeId);
switch (shot.getProcessType()) {
case CREATE_CREATURE -> {
creaturesController.spawnCreature((short) shotData1, playerId, shotData2, position, ICreaturesController.SpawnType.CONJURE);
}
case CREATE_OBJECT -> {
objectsController.loadObject((short) shotData1, playerId, position, 0);
}
case POSSESS_CREATURE -> {

}
case MODIFY_HEALTH -> {

}
default ->
logger.log(Logger.Level.WARNING, "Shot type {0} not implemented", shot.getProcessType());
}
}

}

0 comments on commit 6b33d02

Please sign in to comment.