Skip to content

Commit

Permalink
Refactor to get rid of some code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
tonihele committed Jan 12, 2025
1 parent acc9bb0 commit e65ab94
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package toniarts.openkeeper.game.controller.room;

import com.simsilica.es.EntityComponent;
import com.simsilica.es.EntityData;
import com.simsilica.es.EntityId;
import java.awt.Point;
Expand All @@ -26,7 +27,6 @@
import java.util.Map;
import java.util.Set;
import toniarts.openkeeper.common.RoomInstance;
import toniarts.openkeeper.game.component.Health;
import toniarts.openkeeper.game.component.Owner;
import toniarts.openkeeper.game.component.RoomComponent;
import toniarts.openkeeper.game.controller.IObjectsController;
Expand All @@ -39,7 +39,7 @@
*
* @author Toni Helenius <helenius.toni@gmail.com>
*/
public abstract class AbstractRoomController implements IRoomController {
public abstract class AbstractRoomController extends AbstractRoomInformation implements IRoomController {

/**
* The type of object the room houses
Expand Down Expand Up @@ -85,6 +85,7 @@ public enum RoomObjectLayout {

public AbstractRoomController(EntityId entityId, EntityData entityData, KwdFile kwdFile,
RoomInstance roomInstance, IObjectsController objectsController) {
super(entityId);
this.entityId = entityId;
this.entityData = entityData;
this.kwdFile = kwdFile;
Expand Down Expand Up @@ -287,7 +288,7 @@ public boolean isDestroyed() {
}

private RoomComponent getRoomComponent() {
return entityData.getComponent(entityId, RoomComponent.class);
return getEntityComponent(RoomComponent.class);
}

/**
Expand Down Expand Up @@ -459,40 +460,7 @@ protected static Short getPillarObject(short roomId) {
}

@Override
public EntityId getEntityId() {
return entityId;
}

@Override
public int getHealth() {
return entityData.getComponent(entityId, Health.class).health;
}

@Override
public int getMaxHealth() {
return entityData.getComponent(entityId, Health.class).maxHealth;
}

@Override
public Integer getHealthPercent() {
Health health = entityData.getComponent(entityId, Health.class);
return Math.round((float) health.health / health.maxHealth * 100);
}

@Override
public boolean isAtFullHealth() {
Health health = entityData.getComponent(entityId, Health.class);

return (health.health == health.maxHealth);
}

@Override
public short getOwnerId() {
return entityData.getComponent(entityId, Owner.class).ownerId;
}

@Override
public short getRoomId() {
return roomInstance.getEntity().getId();
protected <T extends EntityComponent> T getEntityComponent(Class<T> type) {
return entityData.getComponent(entityId, type);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@
* 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.map;
package toniarts.openkeeper.game.controller.room;

import com.simsilica.es.EntityComponent;
import com.simsilica.es.EntityId;
import java.util.Objects;
import toniarts.openkeeper.game.component.Health;
import toniarts.openkeeper.game.component.Owner;
import toniarts.openkeeper.game.component.RoomComponent;
import toniarts.openkeeper.game.map.IRoomInformation;

/**
* A presentation of a single map tile. Gets data from specified entity
Expand Down
2 changes: 1 addition & 1 deletion src/toniarts/openkeeper/view/map/MapRoomContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import toniarts.openkeeper.game.component.Health;
import toniarts.openkeeper.game.component.Owner;
import toniarts.openkeeper.game.component.RoomComponent;
import toniarts.openkeeper.game.map.AbstractRoomInformation;
import toniarts.openkeeper.game.controller.room.AbstractRoomInformation;
import toniarts.openkeeper.game.map.IRoomInformation;
import toniarts.openkeeper.game.map.IRoomsInformation;
import toniarts.openkeeper.tools.convert.map.KwdFile;
Expand Down

0 comments on commit e65ab94

Please sign in to comment.