About Social Code
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLucas Fryzek <lucas.fryzek@fryzekconcepts.com>2025-03-22 21:41:50 +0000
committerLucas Fryzek <lucas.fryzek@fryzekconcepts.com>2025-03-22 21:41:50 +0000
commitbca429dd94dafc49cf9134040dfdfb6baf0de10c (patch)
treea2a63bc9fb584622156f268dd71f94424819461d
parentcb01f1f6f930cee6529e51dc3d29945edaa9d95a (diff)
Add basic item system
-rw-r--r--entity.rhm21
-rw-r--r--item.rhm21
-rw-r--r--main.rhm3
-rw-r--r--world.rhm4
4 files changed, 37 insertions, 12 deletions
diff --git a/entity.rhm b/entity.rhm
index 07bc81b..3fcd812 100644
--- a/entity.rhm
+++ b/entity.rhm
@@ -2,12 +2,14 @@
import:
rhombus/random
"chunk.rhm" open
+ "item.rhm" open
export:
Entity
EntityGatherable
EntityPlayer
ActionResponse
+ Slot
let rand = random.Random()
@@ -85,11 +87,11 @@ class Entity(id:: Int, mutable x :: Int, mutable y :: Int, mutable current_chunk
| ~else:
#false
-class EntityGatherable(quantity :: Int, odds :: Real):
+class EntityGatherable(quantity :: Int, odds :: Real, cap :: ItemCap):
extends Entity
- constructor(id :: Int, x :: Int, y :: Int, current_chunk :: Chunk):
- super(id, x, y, current_chunk)(10, 1 / 10)
+ constructor(id :: Int, x :: Int, y :: Int, current_chunk :: Chunk, cap :: ItemCap):
+ super(id, x, y, current_chunk)(10, 1 / 10, cap)
enum ActionResponse:
ok
@@ -100,11 +102,6 @@ enum ActionResponse:
enum Slot:
right_hand
-enum Item:
- bronze_axe
-
-class Stack(item :: Item, quantity :: Int)
-
class EntityPlayer(mutable timer :: Int):
extends Entity
@@ -131,14 +128,16 @@ class EntityPlayer(mutable timer :: Int):
let right_type = target_entity is_a EntityGatherable
fun validate_gather_resource(entity :: EntityGatherable) :: ActionResponse:
+ let right_tool = item_get_cap(equip.get(Slot.right_hand, #false)) == entity.cap
// TODO odds should change based on skills of player
// we should also validate player is carrying the right tool
// we should also have the timer count down every tick
// not just inside the gathering call
let roll = rand.random()
- if roll < entity.odds:
- | ActionResponse.ok
- | ActionResponse.unsuccessful
+ cond
+ | !right_tool: ActionResponse.invalid
+ | rand.random() < entity.odds: ActionResponse.ok
+ | ~else: ActionResponse.unsuccessful
cond
| timer != 0: ActionResponse.cooldown
diff --git a/item.rhm b/item.rhm
new file mode 100644
index 0000000..6716dbc
--- /dev/null
+++ b/item.rhm
@@ -0,0 +1,21 @@
+#lang rhombus/static
+
+export:
+ Item
+ Stack
+ ItemCap
+ item_get_cap
+
+enum Item:
+ bronze_axe
+
+enum ItemCap:
+ none
+ chop
+
+fun item_get_cap(item :: maybe(Item)) :: ItemCap:
+ match item:
+ | Item.bronze_axe: ItemCap.chop
+ | ~else: ItemCap.none
+
+class Stack(item :: Item, quantity :: Int)
diff --git a/main.rhm b/main.rhm
index 2d306bb..5648c53 100644
--- a/main.rhm
+++ b/main.rhm
@@ -49,7 +49,8 @@ test(!entity.move(0, 0), "blocked by wall")
test(entity.move(1,1), "move by wall")
reset_entity(entity, 0, 0)
-let other_ent = world.EntityGatherable(1, 1, 1, chunk)
+entity.equip[world.Slot.right_hand] := world.Item.bronze_axe
+let other_ent = world.EntityGatherable(1, 1, 1, chunk, world.ItemCap.chop)
let gather_result = entity.gather(other_ent)
test(gather_result == world.ActionResponse.ok || gather_result == world.ActionResponse.unsuccessful, "gathering")
diff --git a/world.rhm b/world.rhm
index 838b9df..9357422 100644
--- a/world.rhm
+++ b/world.rhm
@@ -2,6 +2,7 @@
import:
"chunk.rhm" open
"entity.rhm" open
+ "item.rhm" open
export:
Chunk
@@ -13,6 +14,9 @@ export:
EntityPlayer
EntityGatherable
Direction
+ ItemCap
+ Item
+ Slot
class World(chunks :: MutableList.now_of(Chunk),
entities :: MutableList.now_of(Entity)):