Skip to content

Commit

Permalink
v0.8.25 better apple feeding (#205)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderclarktx authored Oct 18, 2024
1 parent 2520e13 commit 7f1bb9a
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 58 deletions.
2 changes: 0 additions & 2 deletions core/src/ecs/actions/interactive/Eat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ export const Eat = Action<{ target: Entity<Position> }>(({ entity, params, world
const { target } = params
if (!target || !world.entities[target.id]) return

if (target.components.collider?.active === false) return

world.removeEntity(target.id)

if (entity.components.renderable?.scale) {
Expand Down
7 changes: 1 addition & 6 deletions core/src/ecs/actions/interactive/ItemActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,12 @@ export const DropItem = Action(({player, world}) => {
const activeItem = inventory.activeItem()
if (!activeItem) return

const { equip, position, collider, renderable } = activeItem.components
const { equip, position, collider } = activeItem.components

equip.dropped = true
position.data.follows = undefined

position.data.x += renderable.position.x
position.data.y += renderable.position.y

if (collider) collider.active = true

renderable.position = { x: 0, y: 0 }

inventory.dropActiveItem()
}, 10)
2 changes: 1 addition & 1 deletion core/src/ecs/commands/DebugCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Command } from "@piggo-gg/core";

export const DebugCommand: Command = {
id: "debug",
regex: /\/debug/,
regex: /\/d/,
parse: ({ world }): undefined => {
world.debug = !world.debug;
},
Expand Down
91 changes: 54 additions & 37 deletions core/src/ecs/components/Inventory.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
Actions, Character, Component, Equip, Effects, Entity, Input,
Name, Position, Renderable, SystemBuilder, Team
Name, Position, Renderable, SystemBuilder, Team,
keys, values, entries
} from "@piggo-gg/core"

export type Item = Entity<Name | Position | Actions | Effects | Renderable | Equip>
Expand All @@ -9,7 +10,7 @@ export const Item = Entity<Name | Position | Actions | Effects | Renderable | Eq
export type ItemBuilder = (character: Character) => Item

export type Inventory = Component<"inventory"> & {
items: (Item | undefined)[]
items: Record<string, Item | undefined>
itemBuilders: ItemBuilder[]
activeItemIndex: number
activeItem: () => Item | null
Expand All @@ -21,22 +22,22 @@ export type Inventory = Component<"inventory"> & {
export const Inventory = (items: ((character: Character) => Item)[]): Inventory => {
const inventory: Inventory = {
type: "inventory",
items: [],
items: { "1": undefined, "2": undefined, "3": undefined, "4": undefined, "5": undefined },
itemBuilders: items,
activeItemIndex: 0,
activeItem: () => inventory.items[inventory.activeItemIndex] ?? null,
addItem: (item: Item) => {
if (!inventory.items.map(x => x?.id).includes(item.id)) {

if (!values(inventory.items).map(x => x?.id).includes(item.id)) {
let inserted = false

inventory.items.forEach((slot, index) => {
if (!slot && !inserted) {
keys(inventory.items).forEach(index => {
if (!inventory.items[index] && !inserted) {
inventory.items[index] = item
inserted = true
return
}
})
if (!inserted) inventory.items.push(item)
}
},
dropActiveItem: () => {
Expand All @@ -51,39 +52,55 @@ export const Inventory = (items: ((character: Character) => Item)[]): Inventory

export const InventorySystem: SystemBuilder<"InventorySystem"> = {
id: "InventorySystem",
init: (world) => ({
id: "InventorySystem",
query: ["position", "input", "actions", "renderable", "inventory", "team"],
onTick: (entities: Entity<Position | Input | Actions | Renderable | Inventory | Team>[]) => {
entities.forEach(entity => {
const { inventory } = entity.components

if (inventory.itemBuilders.length) {
inventory.items = inventory.itemBuilders.map(builder => builder(entity))
inventory.itemBuilders = []
}

// reset state for all items
inventory.items.forEach(item => {
if (!item) return

if (item.components.input) {
throw new Error("Item cannot have input component (it breaks InputSystem)")
init: (world) => {
const knownItems: Set<string> = new Set()

return {
id: "InventorySystem",
query: ["position", "input", "actions", "renderable", "inventory", "team"],
onTick: (entities: Entity<Position | Input | Actions | Renderable | Inventory | Team>[]) => {
entities.forEach(entity => {
const { inventory } = entity.components

// build items
if (inventory.itemBuilders.length) {
inventory.itemBuilders.forEach((builder, index) => {
const item = builder(entity)
inventory.items[index] = item
})
inventory.itemBuilders = []
}

if (!world.entities[item.id]) world.addEntity(item)
// reset state for all items
entries(inventory.items).forEach(([index, item]) => {
if (!item) return

item.components.renderable.visible = false
item.components.equip.equipped = false
})
if (knownItems.has(item.id) && !world.entities[item.id]) {
inventory.items[index] = undefined
knownItems.delete(item.id)
return
}

if (item.components.input) {
throw new Error("Item cannot have input component (it breaks InputSystem)")
}

if (!world.entities[item.id]) world.addEntity(item)

// set state for active item
const activeItem = inventory.activeItem()
if (activeItem) {
activeItem.components.renderable.visible = true
activeItem.components.equip.equipped = true
}
})
knownItems.add(item.id)

item.components.renderable.visible = false
item.components.equip.equipped = false
})

// set state for active item
const activeItem = inventory.activeItem()
if (activeItem) {
activeItem.components.renderable.visible = true
activeItem.components.equip.equipped = true
}
})
}
}
})
}
}
7 changes: 5 additions & 2 deletions core/src/ecs/components/Position.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export type Position = Component<"position", {
heading: XY
velocityResets: number
follows: string | undefined
offset: XY
}> & {
lastCollided: number
screenFixed: boolean
Expand Down Expand Up @@ -51,7 +52,8 @@ export const Position = (props: PositionProps = {}): Position => {
pointingDelta: { x: NaN, y: NaN },
heading: { x: NaN, y: NaN },
velocityResets: props.velocityResets ?? 0,
follows: props.follows ?? undefined
follows: props.follows ?? undefined,
offset: { x: 0, y: 0 }
},
orientation: "r",
orientationRads: 0,
Expand Down Expand Up @@ -141,7 +143,8 @@ export const PositionSystem: SystemBuilder<"PositionSystem"> = {

position.data = {
...position.data,
x, y,
x: x + position.data.offset.x,
y: y + position.data.offset.y,
pointing,
speed,
velocity: { ...velocity },
Expand Down
2 changes: 1 addition & 1 deletion core/src/ecs/entities/characters/Skelly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const Skelly = (player: Noob, color?: number, pos?: XY) => {
collider: Collider({ shape: "ball", radius: 8, mass: 600, hittable: true }),
health: Health({ health: 100 }),
team: player.components.team,
inventory: Inventory([Sword, Axe, Pickaxe]),
inventory: Inventory([Axe, Pickaxe, Sword]),
element: Element("flesh"),
input: Input({
press: {
Expand Down
3 changes: 1 addition & 2 deletions core/src/ecs/entities/items/Tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ export const dynamicItem = ({ mouseLast, flip }: { mouseLast: XY, flip: boolean
const hyp_x = pointingDelta.x / hypotenuse
const hyp_y = pointingDelta.y / hypotenuse

// TODO use some kind of follow-offset mechanism on Position (to get piggos to chase)
r.position = {
item.components.position.data.offset = {
x: hyp_x * min(20, abs(pointingDelta.x)),
y: hyp_y * min(20, abs(pointingDelta.y)) - 5
}
Expand Down
3 changes: 2 additions & 1 deletion core/src/ecs/systems/ui/DebugSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ export const DebugSystem = ClientSystemBuilder({

// text box
const textBox = TextBox({
dynamic: (c: Text) => {
dynamic: (c: Text, textRenderable) => {
if (renderable && position) {
const bounds = renderable.c.getLocalBounds()
c.position.set(bounds.x, bounds.top - 25)
c.text = debugText(position)
textRenderable.visible = renderable.visible
}
},
fontSize: 8, color: 0x00ff00
Expand Down
4 changes: 2 additions & 2 deletions core/src/runtime/IsometricWorld.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
ActionSystem, ClickableSystem, CommandSystem, ControlSystem, CooldownSystem,
DamageSystem, DebugCommand, DebugSystem, EffectsSystem, ExpiresSystem,
GameCommand, InputSystem, NPCSystem, NametagSystem, PhysicsSystem,
GameCommand, InputSystem, InventorySystem, NPCSystem, NametagSystem, PhysicsSystem,
PositionSystem, RenderSystem, SpawnCommand, World, WorldBuilder
} from "@piggo-gg/core";
import { NameCommand } from "../ecs/commands/NameCommand";
Expand All @@ -15,7 +15,7 @@ export const IsometricWorld: WorldBuilder = (props) => {

world.addSystemBuilders([
ExpiresSystem, ControlSystem, ClickableSystem, InputSystem, DebugSystem,
DamageSystem, CommandSystem, NPCSystem, NametagSystem, CooldownSystem,
DamageSystem, CommandSystem, NPCSystem, NametagSystem, CooldownSystem, InventorySystem,
PhysicsSystem, ActionSystem, EffectsSystem, PositionSystem, RenderSystem
]);

Expand Down
2 changes: 1 addition & 1 deletion docs/piggo-gg-min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions games/sandbox/Sandbox.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {
InventorySystem, IsometricGame, Piggo, SkellySpawnSystem,
IsometricGame, Piggo, SkellySpawnSystem,
Tree, isMobile, MobilePvEHUD, PvEHUD, Rock
} from "@piggo-gg/core"

export const Sandbox = IsometricGame({
id: "sandbox",
init: () => ({
id: "sandbox",
systems: [SkellySpawnSystem, InventorySystem],
systems: [SkellySpawnSystem],
entities: [
isMobile() ? MobilePvEHUD() : PvEHUD(),
Piggo(), Piggo(), Piggo(), Piggo(), Piggo(), Piggo(), Piggo(),
Expand Down
2 changes: 1 addition & 1 deletion web/src/components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const Header = ({ world, netState, setNetState }: HeaderProps) => (

<div style={{ position: 'absolute', right: 0, bottom: 0 }}>
<span style={{ fontFamily: "sans-serif", fontSize: 14, marginRight: 5, verticalAlign: "-70%" }}>
v<b>0.8.24</b>
v<b>0.8.25</b>
</span>
<a style={{ margin: 0, color: "inherit", textDecoration: "none" }} target="_blank" href="https://discord.gg/VfFG9XqDpJ">
<FaDiscord size={20} style={{ color: "white", verticalAlign: "-80%" }}></FaDiscord>
Expand Down

0 comments on commit 7f1bb9a

Please # to comment.