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

refactor(ri): split EntityType into Item, Structure, and Unit 💠 #90

Merged
merged 2 commits into from
Jul 14, 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
@@ -1,13 +1,6 @@
import {
getComponentValueStrict,
Has,
HasValue,
defineSyncSystem,
defineEnterSystem,
setComponent,
} from "@latticexyz/recs";
import { Has, defineSyncSystem, setComponent, defineComponentSystem } from "@latticexyz/recs";
import { LocalLayer } from "../../types";
import { EntityTypes } from "../../../Network/types";
import { UnitTypeNames, StructureTypeNames, ItemTypeNames } from "../../../Network/types";

/**
* The Sync system handles adding Local layer components to entites based on components they have on parent layers
Expand All @@ -17,61 +10,69 @@ export function createSyncSystem(layer: LocalLayer) {
world,
parentLayers: {
network: {
components: { EntityType, Position },
components: { ItemType, UnitType, StructureType, Movable },
},
},
components: { Strolling, LocalPosition, MoveSpeed, Selectable, Name },
components: { MoveSpeed, Selectable, Name },
} = layer;

// Add Strolling component to entities with type Creature
defineSyncSystem(
world,
[HasValue(EntityType, { value: EntityTypes.Hero })],
() => Strolling,
() => ({ value: true })
[Has(Movable)],
() => MoveSpeed,
() => ({ default: 1000, current: 1000 })
);

// Add LocalPosition to entities with Position of type Creature
defineSyncSystem(
world,
[HasValue(EntityType, { value: EntityTypes.Hero }), Has(Position)],
() => LocalPosition,
(entity) => {
const pos = getComponentValueStrict(Position, entity);
return pos;
}
[Has(UnitType)],
() => Selectable,
() => ({ value: true })
);

// Add MoveSpeed to entities of type Creature
defineSyncSystem(
world,
[HasValue(EntityType, { value: EntityTypes.Hero })],
() => MoveSpeed,
() => ({ default: 1000, current: 1000 })
[Has(StructureType)],
() => Selectable,
() => ({ value: true })
);

defineSyncSystem(
world,
[Has(EntityType)],
[Has(ItemType)],
() => Selectable,
() => ({ value: true })
);

defineEnterSystem(world, [Has(EntityType)], ({ entity, value }) => {
defineComponentSystem(world, UnitType, ({ entity, value }) => {
const [newValue] = value;
const type = newValue?.value;
if (type == null) return;

let name = "Unknown";
const entityType = value[0]?.value;
if (UnitTypeNames[type]) name = UnitTypeNames[type];

if (entityType === EntityTypes.Hero) {
name = "Soldier";
} else if (entityType === EntityTypes.Settlement) {
name = "Settlement";
} else if (entityType === EntityTypes.Gold) {
name = "Gold";
} else if (entityType === EntityTypes.EmberCrown) {
name = "EmberCrown";
} else if (entityType === EntityTypes.EscapePortal) {
name = "EscapePortal";
}
setComponent(Name, entity, { value: name });
});

defineComponentSystem(world, StructureType, ({ entity, value }) => {
const [newValue] = value;
const type = newValue?.value;
if (type == null) return;

let name = "Unknown";
if (StructureTypeNames[type]) name = StructureTypeNames[type];

setComponent(Name, entity, { value: name });
});

defineComponentSystem(world, ItemType, ({ entity, value }) => {
const [newValue] = value;
const type = newValue?.value;
if (type == null) return;

let name = "Unknown";
if (ItemTypeNames[type]) name = ItemTypeNames[type];

setComponent(Name, entity, { value: name });
});
Expand Down

This file was deleted.

1 change: 0 additions & 1 deletion packages/ri/client/src/layers/Network/components/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export { definePositionComponent } from "./Position";
export { defineEntityTypeComponent } from "./EntityType";
export { defineMovableComponent } from "./Movable";
export { defineOwnedByComponent } from "./OwnedBy";
export { defineUntraversableComponent } from "./Untraversable";
23 changes: 19 additions & 4 deletions packages/ri/client/src/layers/Network/createNetworkLayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
} from "@latticexyz/recs";
import {
definePositionComponent,
defineEntityTypeComponent,
defineMovableComponent,
defineOwnedByComponent,
defineUntraversableComponent,
Expand Down Expand Up @@ -61,13 +60,27 @@ export async function createNetworkLayer(config: NetworkLayerConfig) {
id: "Systems",
metadata: { contractId: keccak256("world.component.systems") },
}),
Position: definePositionComponent(world, keccak256("ember.component.positionComponent")),
EntityType: defineEntityTypeComponent(world, keccak256("ember.component.entityTypeComponent")),
UnitType: defineComponent(
world,
{ value: Type.Number },
{ id: "UnitType", metadata: { contractId: keccak256("ember.component.unitType") } }
),
StructureType: defineComponent(
world,
{ value: Type.Number },
{ id: "StructureType", metadata: { contractId: keccak256("ember.component.structureType") } }
),
ItemType: defineComponent(
world,
{ value: Type.Number },
{ id: "ItemType", metadata: { contractId: keccak256("ember.component.itemType") } }
),
TerrainType: defineComponent(
world,
{ value: Type.Number },
{ metadata: { contractId: keccak256("ember.component.terrainType") } }
),
Position: definePositionComponent(world, keccak256("ember.component.positionComponent")),
Movable: defineMovableComponent(world, keccak256("ember.component.movableComponent")),
OwnedBy: defineOwnedByComponent(world, keccak256("ember.component.ownedByComponent")),
Untraversable: defineUntraversableComponent(world, keccak256("ember.component.untraversableComponent")),
Expand Down Expand Up @@ -157,9 +170,11 @@ export async function createNetworkLayer(config: NetworkLayerConfig) {
const mappings: Mappings<typeof components> = {
[keccak256("world.component.components")]: "Components",
[keccak256("world.component.systems")]: "Systems",
[keccak256("ember.component.unitType")]: "UnitType",
[keccak256("ember.component.structureType")]: "StructureType",
[keccak256("ember.component.itemType")]: "ItemType",
[keccak256("ember.component.gameConfigComponent")]: "GameConfig",
[keccak256("ember.component.positionComponent")]: "Position",
[keccak256("ember.component.entityTypeComponent")]: "EntityType",
[keccak256("ember.component.terrainType")]: "TerrainType",
[keccak256("ember.component.movableComponent")]: "Movable",
[keccak256("ember.component.ownedByComponent")]: "OwnedBy",
Expand Down
2 changes: 1 addition & 1 deletion packages/ri/client/src/layers/Network/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { createNetworkLayer } from "./createNetworkLayer";
export type { NetworkLayer } from "./types";
export { EntityTypes, TerrainTypes } from "./types";
export { UnitTypes, ItemTypes, StructureTypes, TerrainTypes } from "./types";
39 changes: 35 additions & 4 deletions packages/ri/client/src/layers/Network/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,52 @@ export enum ContractWorldEvent {
ComponentValueRemoved = "ComponentValueRemoved",
}

export enum EntityTypes {
export enum UnitTypes {
Hero,
Donkey,
}

export const UnitTypeNames: Record<number, string> = {
[UnitTypes.Hero]: "Hero",
[UnitTypes.Donkey]: "Donkey",
};

export enum StructureTypes {
Settlement,
GoldShrine,
EscapePortal,
}

export const StructureTypeNames: Record<number, string> = {
[StructureTypes.Settlement]: "Settlement",
[StructureTypes.GoldShrine]: "Gold Shrine",
[StructureTypes.EscapePortal]: "Escape Portal",
};

export enum ItemTypes {
Inventory,
Gold,
GoldShrine,
EmberCrown,
EscapePortal,
Donkey,
}

export const ItemTypeNames: Record<number, string> = {
[ItemTypes.Inventory]: "Inventory",
[ItemTypes.Gold]: "Gold",
[ItemTypes.EmberCrown]: "Ember Crown",
};

export enum TerrainTypes {
Grass,
Mountain,
Water,
Wall,
Tree,
}

export const TerrainTypeNames: Record<number, string> = {
[TerrainTypes.Grass]: "Grass",
[TerrainTypes.Mountain]: "Mountain",
[TerrainTypes.Water]: "Water",
[TerrainTypes.Wall]: "Wall",
[TerrainTypes.Tree]: "Tree",
};
26 changes: 16 additions & 10 deletions packages/ri/client/src/layers/Renderer/Phaser/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EntityTypes } from "../../Network";
import { UnitTypes, ItemTypes, StructureTypes } from "../../Network";

export const TILE_WIDTH = 16;
export const TILE_HEIGHT = 16;
Expand Down Expand Up @@ -33,13 +33,19 @@ export enum Sprites {

export enum Animations {}

export const EntityTypeSprites: Record<number, Sprites> = {
[EntityTypes.Hero]: Sprites.Hero,
[EntityTypes.Gold]: Sprites.Gold,
[EntityTypes.Inventory]: Sprites.Inventory,
[EntityTypes.Settlement]: Sprites.Settlement,
[EntityTypes.GoldShrine]: Sprites.GoldShrine,
[EntityTypes.EmberCrown]: Sprites.EmberCrown,
[EntityTypes.EscapePortal]: Sprites.EscapePortal,
[EntityTypes.Donkey]: Sprites.Donkey,
export const UnitTypeSprites: Record<number, Sprites> = {
[UnitTypes.Hero]: Sprites.Hero,
[UnitTypes.Donkey]: Sprites.Donkey,
};

export const ItemTypeSprites: Record<number, Sprites> = {
[ItemTypes.Inventory]: Sprites.Inventory,
[ItemTypes.Gold]: Sprites.Gold,
[ItemTypes.EmberCrown]: Sprites.EmberCrown,
};

export const StructureTypeSprites: Record<number, Sprites> = {
[StructureTypes.Settlement]: Sprites.Settlement,
[StructureTypes.GoldShrine]: Sprites.GoldShrine,
[StructureTypes.EscapePortal]: Sprites.EscapePortal,
};
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
removeComponent,
} from "@latticexyz/recs";
import { PhaserLayer } from "../../types";
import { EntityTypeSprites } from "../../constants";
import { UnitTypeSprites, StructureTypeSprites, ItemTypeSprites } from "../../constants";

/**
* The Sync system handles adding Phaser layer components to entites based on components they have on parent layers
Expand All @@ -18,7 +18,7 @@ export function createSyncSystem(layer: PhaserLayer) {
world,
parentLayers: {
network: {
components: { EntityType },
components: { UnitType, StructureType, ItemType },
},
local: {
components: { Selected, LocalPosition },
Expand All @@ -34,13 +34,33 @@ export function createSyncSystem(layer: PhaserLayer) {
() => ({ color: 0xfff000 })
);

defineSystem(world, [Has(EntityType), Has(LocalPosition)], ({ entity, type }) => {
const entityType = getComponentValueStrict(EntityType, entity).value;
defineSystem(world, [Has(UnitType), Has(LocalPosition)], ({ entity, type }) => {
const entityType = getComponentValueStrict(UnitType, entity).value;

if (type === UpdateType.Exit) removeComponent(Appearance, entity);

setComponent(Appearance, entity, {
value: EntityTypeSprites[entityType],
value: UnitTypeSprites[entityType],
});
});

defineSystem(world, [Has(StructureType), Has(LocalPosition)], ({ entity, type }) => {
const entityType = getComponentValueStrict(StructureType, entity).value;

if (type === UpdateType.Exit) removeComponent(Appearance, entity);

setComponent(Appearance, entity, {
value: StructureTypeSprites[entityType],
});
});

defineSystem(world, [Has(ItemType), Has(LocalPosition)], ({ entity, type }) => {
const entityType = getComponentValueStrict(ItemType, entity).value;

if (type === UpdateType.Exit) removeComponent(Appearance, entity);

setComponent(Appearance, entity, {
value: ItemTypeSprites[entityType],
});
});
}
4 changes: 3 additions & 1 deletion packages/ri/contracts/deploy.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
{
"components": [
"UnitTypeComponent",
"StructureTypeComponent",
"ItemTypeComponent",
"AttackComponent",
"CapturableComponent",
"EmberCrownComponent",
"EntityTypeComponent",
"FactoryComponent",
"FromBlueprintComponent",
"GameConfigComponent",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
pragma solidity >=0.8.0;
import "std-contracts/components/Uint32Component.sol";

uint256 constant ID = uint256(keccak256("ember.component.entityTypeComponent"));
uint256 constant ID = uint256(keccak256("ember.component.itemType"));

contract EntityTypeComponent is Uint32Component {
contract ItemTypeComponent is Uint32Component {
constructor(address world) Uint32Component(world, ID) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// SPDX-License-Identifier: Unlicense
pragma solidity >=0.8.0;
import "std-contracts/components/Uint32Component.sol";

uint256 constant ID = uint256(keccak256("ember.component.structureType"));

contract StructureTypeComponent is Uint32Component {
constructor(address world) Uint32Component(world, ID) {}
}
9 changes: 9 additions & 0 deletions packages/ri/contracts/src/components/UnitTypeComponent.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// SPDX-License-Identifier: Unlicense
pragma solidity >=0.8.0;
import "std-contracts/components/Uint32Component.sol";

uint256 constant ID = uint256(keccak256("ember.component.unitType"));

contract UnitTypeComponent is Uint32Component {
constructor(address world) Uint32Component(world, ID) {}
}
Loading