holdItem
triggers on self
"drop item"parameters
| Parameter | Default Value | Type | Description |
|---|---|---|---|
pos | vec2 | vec2(0, -30) |
example
holdItem({ pos: vec2(0, -30)}),function holdItem(p) { const param = { pos: vec2(0, -30), item: { sprite: "key", holdItemType: "key", }, itemOnStart: false, deleteDropOnRespawn: true, onInteract: false, catchWhileHolding: true, ...p, } return { id: "holdItem", holdItem: null, add() { const catchItem = (obj) => { const catchable = obj.is("catchable") const isntOwn = obj.holdingParentId != this.id const handFree = param.catchWhileHolding || this.holdItem == null if (catchable && isntOwn && handFree) { if (this.holdItem) this.dropItem(obj.pos) this.setHoldingItem(obj, obj.pos) obj.hidden = true obj.collisionIgnore = ["*"] } } if (param.itemOnStart) this.setHoldingItem(param.item) if (param.onInteract) this.on("interact", (obj) => catchItem(obj)) else this.onCollide("catchable", (obj) => catchItem(obj)) this.on("respawn", () => { this.emptyHoldingItem() if (param.itemOnStart) this.setHoldingItem(param.item) }) this.on("drop item", () => { this.emptyHoldingItem() }) }, setHoldingItem(item, origin = param.pos) { this.holdItem = { type: item.holdItemType, sprite: item.sprite } this.trigger("grab", { item: item, origin: origin }) }, emptyHoldingItem() { this.holdItem = null }, isHolding(type) { return (this.holdItem && type == this.holdItem.type) }, dropItem(p) { const item = add([ sprite(this.holdItem.sprite), area(), anchor("bot"), pos(this.pos.add(param.pos)), catchable({ type: this.holdItem }), ]) this.trigger("drop item") item.trigger("dropped", { pos: p, parentPos: this.pos.add(param.pos) }) if (!param.onInteract) { item.holdingParentId = this.id item.onCollideEnd("holdItem", (obj) => { if (item.holdingParentId == this.id) item.holdingParentId = null }) } item.on("player death", () => destroy(item)) }, }}