About Social Code
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLucas Fryzek <lucas.fryzek@fryzekconcepts.com>2025-03-20 21:31:19 +0000
committerLucas Fryzek <lucas.fryzek@fryzekconcepts.com>2025-03-20 21:31:19 +0000
commite4c4ec67e975b8668112e09be5bcea9cff8f5f89 (patch)
treec830a4a2e3d5dd9f7c65c4e4fd684206b979cd65
parentd26b5010c233d1dd308f4a433fe505bd76f89aa0 (diff)
Make entity move part of entity class
-rw-r--r--main.rhm24
-rw-r--r--world.rhm33
2 files changed, 32 insertions, 25 deletions
diff --git a/main.rhm b/main.rhm
index 3ab966f..f3c5188 100644
--- a/main.rhm
+++ b/main.rhm
@@ -7,9 +7,11 @@ fun test(test_result :: Boolean, test_str :: String):
| println("Passed " +& test_str)
| println("Failed " +& test_str)
-fun reset_entity(entity :: world.Entity):
- entity.x := 0
- entity.y := 0
+fun
+| reset_entity(entity :: world.Entity): reset_entity(entity, 0, 0)
+| reset_entity(entity :: world.Entity, x :: Int, y :: Int):
+ entity.x := x
+ entity.y := y
let wrld = world.World()
let chunk = world.Chunk(64, 64, 0, 0)
@@ -19,19 +21,25 @@ wrld.entities.add(entity)
reset_entity(entity)
-test(wrld.entity_move(0, 1, 0), "move horizontal")
+test(entity.move(1, 0), "move horizontal")
reset_entity(entity)
-test(wrld.entity_move(0, 0, 1), "move vertical")
+test(entity.move(0, 1), "move vertical")
reset_entity(entity)
-test(wrld.entity_move(0, 1, 1), "move diagonal")
+test(entity.move(1, 1), "move diagonal")
reset_entity(entity)
-test(!wrld.entity_move(0, 2, 0), "move diagonal")
+test(!entity.move(2, 0), "move diagonal")
reset_entity(entity)
-test(!wrld.entity_move(0, 0, 0), "starting pos")
+test(!entity.move(0, 0), "starting pos")
+
+let new_chunk = world.Chunk(64, 64, 1, 0)
+chunk.add_neighbour(new_chunk, #'east)
+reset_entity(entity, 63, 0)
+test(entity.move(64, 0), "cross chunk")
+test(entity.current_chunk == new_chunk, "changed chunk")
//world.entities
println(@str{Entity is @(entity.x) @(entity.y)})
diff --git a/world.rhm b/world.rhm
index 043504b..3a5515a 100644
--- a/world.rhm
+++ b/world.rhm
@@ -14,20 +14,16 @@ class Chunk(width :: Int,
constructor(width :: Int, height :: Int, offset_x :: Int, offset_y :: Int):
super(width, height, offset_x, offset_y, Array.make(width * height, 0), MutableMap())
+ method add_neighbour(chunk :: Chunk, direction :: Symbol):
+ neighbours[direction] := chunk
+
method get_tile(x :: Int, y :: Int):
tiles[y * width + x]
-class Entity(id:: Int, mutable x :: Int, mutable y :: Int, mutable current_chunk :: Chunk)
-
-class World(chunks :: MutableList.now_of(Chunk),
- entities :: MutableList.now_of(Entity)):
-
- constructor():
- super(MutableList(), MutableList())
-
+class Entity(id:: Int, mutable x :: Int, mutable y :: Int, mutable current_chunk :: Chunk):
method
- | entity_move(id :: Int, x :: Int, y :: Int): entity_move(id, x, y, 1)
- | entity_move(id :: Int, x :: Int, y :: Int, max_dist :: Int) :: Boolean:
+ | move(x :: Int, y :: Int): move(x, y, 1)
+ | move(x :: Int, y :: Int, max_dist :: Int) :: Boolean:
let validate_dist = fun (entity :: Entity) :: Boolean:
let diff_x = math.abs(entity.x - x)
let diff_y = math.abs(entity.y - y)
@@ -66,14 +62,17 @@ class World(chunks :: MutableList.now_of(Chunk),
entity.current_chunk := next_chunk!!
#true
- let entity :: maybe(Entity) = entities.find((fun (ent :: Entity) :: Boolean: ent.id == id))
cond
- | !entity:
- println("Can't find entity")
- #false
- | validate_dist(entity!!) && validate_chunk(entity!!):
- entity!!.x := x
- entity!!.y := y
+ | validate_dist(this) && validate_chunk(this):
+ this.x := x
+ this.y := y
#true
| ~else:
#false
+
+class World(chunks :: MutableList.now_of(Chunk),
+ entities :: MutableList.now_of(Entity)):
+
+ constructor():
+ super(MutableList(), MutableList())
+