|
| 1 | +/* |
| 2 | + * Copyright (C) 2014-2024 OpenKeeper |
| 3 | + * |
| 4 | + * OpenKeeper is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation, either version 3 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * OpenKeeper is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with OpenKeeper. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | +package toniarts.openkeeper.game.logic; |
| 18 | + |
| 19 | +import com.simsilica.es.Entity; |
| 20 | +import com.simsilica.es.EntityData; |
| 21 | +import com.simsilica.es.EntitySet; |
| 22 | +import java.util.Collection; |
| 23 | +import java.util.HashMap; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.Set; |
| 26 | +import toniarts.openkeeper.game.component.Owner; |
| 27 | +import toniarts.openkeeper.game.component.Possessed; |
| 28 | +import toniarts.openkeeper.game.controller.IGameController; |
| 29 | +import toniarts.openkeeper.game.controller.IPlayerController; |
| 30 | +import toniarts.openkeeper.game.controller.player.PlayerManaControl; |
| 31 | + |
| 32 | +/** |
| 33 | + * Maintains possessed state, sees when we need to stop possessing |
| 34 | + * |
| 35 | + * @author Toni Helenius <[email protected]> |
| 36 | + */ |
| 37 | +public class PossessedSystem implements IGameLogicUpdatable { |
| 38 | + |
| 39 | + private final EntitySet possessedEntities; |
| 40 | + private final Map<Short, PlayerManaControl> manaControls; |
| 41 | + private final EntityData entityData; |
| 42 | + private final IGameController gameController; |
| 43 | + |
| 44 | + public PossessedSystem(Collection<IPlayerController> playerControllers, EntityData entityData, IGameController gameController) { |
| 45 | + this.gameController = gameController; |
| 46 | + this.entityData = entityData; |
| 47 | + manaControls = HashMap.newHashMap(playerControllers.size()); |
| 48 | + for (IPlayerController playerController : playerControllers) { |
| 49 | + PlayerManaControl manaControl = playerController.getManaControl(); |
| 50 | + if (manaControl != null) { |
| 51 | + manaControls.put(playerController.getKeeper().getId(), manaControl); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + // Listen for posessed entities |
| 56 | + possessedEntities = entityData.getEntities(Possessed.class, Owner.class); |
| 57 | + processAddedEntities(possessedEntities); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public void processTick(float tpf, double gameTime) { |
| 62 | + if (possessedEntities.applyChanges()) { |
| 63 | + |
| 64 | + processAddedEntities(possessedEntities.getAddedEntities()); |
| 65 | + |
| 66 | + processDeletedEntities(possessedEntities.getRemovedEntities()); |
| 67 | + } |
| 68 | + |
| 69 | + for (Entity entity : possessedEntities) { |
| 70 | + |
| 71 | + // See if the player is running out of mana |
| 72 | + Possessed possessed = entity.get(Possessed.class); |
| 73 | + Owner owner = entity.get(Owner.class); |
| 74 | + if (possessed.manaCheckTime + 1 < gameTime) { |
| 75 | + continue; |
| 76 | + } |
| 77 | + |
| 78 | + if (!manaControls.get(owner.ownerId).hasEnoughMana(possessed.manaDrain)) { |
| 79 | + entityData.removeComponent(entity.getId(), Possessed.class); |
| 80 | + } else { |
| 81 | + entityData.setComponent(entity.getId(), new Possessed(possessed.manaDrain, gameTime)); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public void start() { |
| 88 | + |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public void stop() { |
| 93 | + possessedEntities.release(); |
| 94 | + } |
| 95 | + |
| 96 | + private void processAddedEntities(Set<Entity> entities) { |
| 97 | + for (Entity entity : entities) { |
| 98 | + gameController.setPossession(entity.getId(), entity.get(Owner.class).ownerId); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + private void processDeletedEntities(Set<Entity> entities) { |
| 103 | + for (Entity entity : entities) { |
| 104 | + gameController.setPossession(null, entity.get(Owner.class).ownerId); |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments