Skip to content

Commit

Permalink
v0.8.29 stackable items (#209)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderclarktx authored Oct 31, 2024
1 parent 37aab3d commit 79a0913
Show file tree
Hide file tree
Showing 32 changed files with 272 additions and 128 deletions.
6 changes: 3 additions & 3 deletions core/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from "./src/runtime/Game"
export * from "./src/runtime/IsometricGame"
export * from "./src/runtime/DefaultGame"
export * from "./src/runtime/DefaultWorld"
export * from "./src/runtime/StateBuffer"
export * from "./src/runtime/World"
Expand Down Expand Up @@ -39,6 +39,7 @@ export * from "./src/ecs/entities/buttons/ConnectButton"
export * from "./src/ecs/entities/buttons/FullscreenButton"
export * from "./src/ecs/entities/buttons/HomeButton"
export * from "./src/ecs/entities/buttons/ShopButton"
export * from "./src/ecs/entities/characters/Chicko"
export * from "./src/ecs/entities/characters/Piggo"
export * from "./src/ecs/entities/characters/Skelly"
export * from "./src/ecs/entities/characters/Spaceship"
Expand Down Expand Up @@ -78,14 +79,13 @@ export * from "./src/ecs/components/Effects"
export * from "./src/ecs/components/Food"
export * from "./src/ecs/components/Data"
export * from "./src/ecs/components/Debug"
export * from "./src/ecs/components/Equip"
export * from "./src/ecs/components/Item"
export * from "./src/ecs/components/Element"
export * from "./src/ecs/components/Expires"
export * from "./src/ecs/components/Gun"
export * from "./src/ecs/components/Health"
export * from "./src/ecs/components/Input"
export * from "./src/ecs/components/Inventory"
export * from "./src/ecs/components/Name"
export * from "./src/ecs/components/Networked"
export * from "./src/ecs/components/NPC"
export * from "./src/ecs/components/Player"
Expand Down
8 changes: 4 additions & 4 deletions core/src/ecs/Component.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import {
Actions, Clickable, Collider, Controlling, Data, Debug, Name,
Actions, Clickable, Collider, Controlling, Data, Debug,
Effects, Expires, Gun, Health, Input, NPC, Networked, Player,
Position, Renderable, Team, Money, XY, entries, keys, Inventory,
Food, Element, Equip
Food, Element, Item
} from "@piggo-gg/core";

export type ComponentTypes =
Actions | Clickable | Collider | Controlling | Element |
Data | Debug | Effects | Expires | Health | Food |
Input | Team | Networked | NPC | Player | Name |
Position | Renderable | Gun | Money | Inventory | Equip
Input | Team | Networked | NPC | Player |
Position | Renderable | Gun | Money | Inventory | Item

export type ValidComponents = ComponentTypes["type"]

Expand Down
2 changes: 1 addition & 1 deletion core/src/ecs/actions/interactive/Eat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ export const Eat = Action<{ target: Entity<Position> }>(({ entity, params, world
// @ts-expect-error
entity.components.collider.colliderDesc.shape.radius += 1
}
})
}, 40)
15 changes: 8 additions & 7 deletions core/src/ecs/actions/interactive/ItemActions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Action, Item } from "@piggo-gg/core"
import { Action, ItemEntity } from "@piggo-gg/core"

export const setActiveItemIndex = Action<{ index: number }>(({ params, entity }) => {
if (params.index === null || params.index === undefined) return
Expand All @@ -18,10 +18,10 @@ export const PickupItem = Action(({player, entity, world}) => {
const { inventory } = character.components
if (!inventory) return

const { name, position, actions, effects, equip, collider, renderable, clickable } = entity.components
if (!name || !position || !actions || !effects || !equip || !renderable) return
const { position, actions, effects, item, collider, renderable, clickable } = entity.components
if (!position || !actions || !effects || !item || !renderable) return

equip.dropped = false
item.dropped = false

position.data.follows = character.id
position.data.rotation = 0
Expand All @@ -31,7 +31,7 @@ export const PickupItem = Action(({player, entity, world}) => {
if (clickable) clickable.active = false
if (collider) collider.active = false

inventory.addItem(entity as Item)
inventory.addItem(entity as ItemEntity)
})

export const DropItem = Action(({player, world}) => {
Expand All @@ -44,10 +44,11 @@ export const DropItem = Action(({player, world}) => {
const activeItem = inventory.activeItem()
if (!activeItem) return

const { equip, position, collider, clickable } = activeItem.components
const { item, position, collider, clickable } = activeItem.components

equip.dropped = true
item.dropped = true
position.data.follows = undefined
position.setVelocity()

if (clickable) clickable.active = true
if (collider) collider.active = true
Expand Down
17 changes: 0 additions & 17 deletions core/src/ecs/components/Equip.ts

This file was deleted.

8 changes: 5 additions & 3 deletions core/src/ecs/components/Gun.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { Actions, Component, Equip, Effects, Entity, ItemBuilder, Name, Reload, Renderable, Shoot, SpawnHitbox, World, abs, hypot, loadTexture, min, randomInt } from "@piggo-gg/core";
import {
Actions, Component, Item, Effects, Entity, ItemBuilder, Reload, Renderable,
Shoot, SpawnHitbox, World, abs, hypot, loadTexture, min, randomInt
} from "@piggo-gg/core";
import { AnimatedSprite } from "pixi.js";

export type GunNames = "deagle" | "ak" | "awp";
Expand Down Expand Up @@ -127,7 +130,6 @@ export const WeaponTable: Record<GunNames, () => Gun> = {
export const GunItem = (name: string, gun: () => Gun): ItemBuilder => (character) => Entity({
id: name,
components: {
name: Name(name),
position: character.components.position,
actions: Actions({
spawnHitbox: SpawnHitbox,
Expand All @@ -136,7 +138,7 @@ export const GunItem = (name: string, gun: () => Gun): ItemBuilder => (character
}),
gun: gun(),
effects: Effects(),
equip: Equip(),
item: Item({ name }),
renderable: Renderable({
scaleMode: "nearest",
zIndex: 2,
Expand Down
78 changes: 50 additions & 28 deletions core/src/ecs/components/Inventory.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,65 @@
import {
Actions, Character, Component, Equip, Effects, Entity, Input,
Name, Position, Renderable, SystemBuilder, Team,
keys, values, entries
Actions, Character, Component, Entity, Input, Position, Renderable,
SystemBuilder, Team, keys, values, entries, ItemEntity
} from "@piggo-gg/core"

export type Item = Entity<Name | Position | Actions | Effects | Renderable | Equip>
export const Item = Entity<Name | Position | Actions | Effects | Renderable | Equip>

export type ItemBuilder = (character: Character) => Item
export type ItemBuilder = (character: Character) => ItemEntity

export type Inventory = Component<"inventory"> & {
items: Record<string, Item | undefined>
items: Record<string, ItemEntity[] | undefined>
itemBuilders: ItemBuilder[]
activeItemIndex: number
activeItem: () => Item | null
addItem: (item: Item) => void
activeItem: () => ItemEntity | null
addItem: (item: ItemEntity) => void
dropActiveItem: () => void
setActiveItemIndex: (index: number) => void
includes: (item: ItemEntity) => boolean
}

export const Inventory = (items: ((character: Character) => Item)[]): Inventory => {
export const Inventory = (items: ((character: Character) => ItemEntity)[]): Inventory => {
const inventory: Inventory = {
type: "inventory",
items: { 1: undefined, 2: undefined, 3: undefined, 4: undefined, 5: undefined },
itemBuilders: items,
activeItemIndex: 0,
activeItem: () => inventory.items[inventory.activeItemIndex] ?? null,
addItem: (item: Item) => {
activeItem: () => {
return inventory.items[inventory.activeItemIndex]?.[0] ?? null
},
addItem: (item: ItemEntity) => {

if (!inventory.includes(item)) {

if (!values(inventory.items).map(x => x?.id).includes(item.id)) {
let inserted = false
if (item.components.item.stackable) {
for (let index of keys(inventory.items)) {
if (inventory.items[index]?.[0].components.item.name === item.components.item.name) {
inventory.items[index].push(item)
return
}
}
}

keys(inventory.items).forEach(index => {
if (!inventory.items[index] && !inserted) {
inventory.items[index] = item
inserted = true
for (let index of keys(inventory.items)) {
if (!inventory.items[index]) {
inventory.items[index] = [ item ]
return
}
})
}
}
},
dropActiveItem: () => {
const slot = inventory.items[inventory.activeItemIndex]
if (!slot) return
if (slot.length > 1) {
slot.shift()
return
}
inventory.items[inventory.activeItemIndex] = undefined
},
setActiveItemIndex: (index: number) => {
inventory.activeItemIndex = index
},
includes: (item: ItemEntity) => {
return values(inventory.items).map(x => x?.[0]).includes(item)
}
}
return inventory
Expand All @@ -66,19 +81,26 @@ export const InventorySystem: SystemBuilder<"InventorySystem"> = {
if (inventory.itemBuilders.length) {
inventory.itemBuilders.forEach((builder, index) => {
const item = builder(entity)
inventory.items[index] = item
inventory.items[index] = [item]
})
inventory.itemBuilders = []
}

// reset state for all items
entries(inventory.items).forEach(([index, item]) => {
if (!item) return
entries(inventory.items).forEach(([index, slot]) => {
if (!slot) return
const item = slot[0]

if (knownItems.has(item.id) && !world.entities[item.id]) {
inventory.items[index] = undefined
knownItems.delete(item.id)
return
if (item.components.item.stackable) {
slot.shift()
if (slot.length === 0) inventory.items[index] = undefined
return
} else {
inventory.items[index] = undefined
knownItems.delete(item.id)
return
}
}

if (item.components.input) {
Expand All @@ -90,14 +112,14 @@ export const InventorySystem: SystemBuilder<"InventorySystem"> = {
knownItems.add(item.id)

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

// set state for active item
const activeItem = inventory.activeItem()
if (activeItem) {
activeItem.components.renderable.visible = true
activeItem.components.equip.equipped = true
activeItem.components.item.equipped = true
}
})
}
Expand Down
22 changes: 22 additions & 0 deletions core/src/ecs/components/Item.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Actions, Component, Effects, Entity, Renderable, Position } from "@piggo-gg/core";

export type Item = Component<"item"> & {
name: string
dropped: boolean
equipped: boolean
stackable: boolean
}

export type ItemProps = {
name: string
dropped?: boolean
equipped?: boolean
stackable?: boolean
}

export const Item = ({ name, dropped = false, equipped = false, stackable = false }: ItemProps): Item => ({
type: "item", name, dropped, equipped, stackable
})

export type ItemEntity = Entity<Position | Actions | Effects | Renderable | Item>
export const ItemEntity = Entity<Position | Actions | Effects | Renderable | Item>
10 changes: 0 additions & 10 deletions core/src/ecs/components/Name.ts

This file was deleted.

4 changes: 2 additions & 2 deletions core/src/ecs/components/Position.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export type Position = Component<"position", {
orientation: "u" | "ur" | "r" | "dr" | "d" | "dl" | "l" | "ul"
orientationRads: number
setPosition: (_: XY) => Position
setVelocity: (_: XY) => Position
setVelocity: (_?: XY) => Position
setSpeed: (_: number) => void
setHeading: (_: XY) => Position
clearHeading: () => Position
Expand Down Expand Up @@ -64,7 +64,7 @@ export const Position = (props: PositionProps = {}): Position => {
position.data.y = y
return position
},
setVelocity: ({ x, y }: XY) => {
setVelocity: ({ x, y }: XY = { x: 0, y: 0 }) => {
position.data.velocity.x = round(x * 100) / 100
position.data.velocity.y = round(y * 100) / 100

Expand Down
Loading

0 comments on commit 79a0913

Please # to comment.