Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ri): death #61

Merged
merged 1 commit into from
Jul 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,12 @@ export function createLocalPositionSystem(layer: PhaserLayer) {

if (update.type === UpdateType.Exit) {
embodiedEntity.removeComponent(LocalPosition.id);
embodiedEntity.despawn();
}

if (!pos) throw new Error("No LocalPosition value for entity");
if (!pos) {
return;
}

if (update.type === UpdateType.Enter) {
const pixel = tileCoordToPixelCoord(pos, tileWidth, tileHeight);
Expand Down
39 changes: 30 additions & 9 deletions packages/ri/contracts/src/facets/CombatFacet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { AppStorage } from "../libraries/LibAppStorage.sol";
import { PositionComponent, ID as PositionComponentID, Coord } from "../components/PositionComponent.sol";
import { AttackComponent, ID as AttackComponentID, Attack } from "../components/AttackComponent.sol";
import { HealthComponent, ID as HealthComponentID, Health } from "../components/HealthComponent.sol";
import { StaminaComponent, ID as StaminaComponentID } from "../components/StaminaComponent.sol";

contract CombatFacet is UsingDiamondOwner, UsingAccessControl {
AppStorage internal s;
Expand Down Expand Up @@ -41,15 +42,14 @@ contract CombatFacet is UsingDiamondOwner, UsingAccessControl {

// Target tries to defend themselves
// TODO check if defender is in range before retaliation
(int32 defenderStamina, int32 _atTurn) = LibStamina.getUpdatedStamina(defender);
if (
defenderStamina >= 1 &&
newDefenderHealth.current > 0 &&
healthComponent.has(attacker) &&
attackComponent.has(defender)
) {
LibStamina.reduceStamina(defender, 1);
dealDamage(defender, attacker);
if (newDefenderHealth.current > 0 && healthComponent.has(attacker) && attackComponent.has(defender)) {
// Check stamina after health check, because if the defender has died
// they will not have stamina
(int32 defenderStamina, int32 _atTurn) = LibStamina.getUpdatedStamina(defender);
if (defenderStamina >= 1) {
LibStamina.reduceStamina(defender, 1);
dealDamage(defender, attacker);
}
}
}

Expand All @@ -74,6 +74,10 @@ contract CombatFacet is UsingDiamondOwner, UsingAccessControl {
if (attackStrength > 0) {
newDefenderHealth = calculateNewHealth(defenderHealth, attackStrength);
healthComponent.set(defender, newDefenderHealth);

if (newDefenderHealth.current < 0) {
kill(defender);
}
}
}

Expand All @@ -85,4 +89,21 @@ contract CombatFacet is UsingDiamondOwner, UsingAccessControl {
int32 remainingHealth = health.current - attackStrength;
newHealth = Health({ current: remainingHealth, max: health.max });
}

function kill(uint256 entity) private {
PositionComponent positionComponent = PositionComponent(s.world.getComponent(PositionComponentID));
if (positionComponent.has(entity)) {
positionComponent.remove(entity);
}

HealthComponent healthComponent = HealthComponent(s.world.getComponent(HealthComponentID));
if (healthComponent.has(entity)) {
healthComponent.remove(entity);
}

StaminaComponent staminaComponent = StaminaComponent(s.world.getComponent(StaminaComponentID));
if (staminaComponent.has(entity)) {
staminaComponent.remove(entity);
}
}
Comment on lines +93 to +108
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the first instance of an event. I think eventually Components should be able to register themselves on to specific events and then handle reacting on their own.

}