diff options
author | Lucas Fryzek <lucas.fryzek@fryzekconcepts.com> | 2025-03-23 00:17:59 +0000 |
---|---|---|
committer | Lucas Fryzek <lucas.fryzek@fryzekconcepts.com> | 2025-03-23 00:17:59 +0000 |
commit | a2feba2d918db861ea85d22887f73993c173e742 (patch) | |
tree | 7027dc2ded85a2ef0f8982e75099434f68c4a0d8 | |
parent | bca429dd94dafc49cf9134040dfdfb6baf0de10c (diff) |
Reorganize source, create basic threading and server
Need to create main world loop and have it message with web server
-rw-r--r-- | main.rhm | 91 | ||||
-rw-r--r-- | src/chunk.rhm (renamed from chunk.rhm) | 0 | ||||
-rw-r--r-- | src/entity.rhm (renamed from entity.rhm) | 0 | ||||
-rw-r--r-- | src/item.rhm (renamed from item.rhm) | 0 | ||||
-rw-r--r-- | src/test.rhm | 67 | ||||
-rw-r--r-- | src/world.rhm (renamed from world.rhm) | 0 | ||||
-rw-r--r-- | thread/thread.rhm | 20 | ||||
-rw-r--r-- | thread/thread.rkt | 10 |
8 files changed, 124 insertions, 64 deletions
@@ -1,67 +1,30 @@ #lang rhombus/static import: - "world.rhm" - -fun test(test_result :: Boolean, test_str :: String): - if test_result - | println("Passed " +& test_str) - | println("Failed " +& test_str) - -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) -wrld.chunks.add(chunk) -let entity = world.EntityPlayer(0, 0, 0, chunk) -wrld.entities.add(entity) - - -reset_entity(entity) -test(entity.move(1, 0), "move horizontal") - -reset_entity(entity, 1, 1) -test(entity.move(0, 1), "move vertical") - -reset_entity(entity) -test(entity.move(1, 1), "move diagonal") - -reset_entity(entity) -test(!entity.move(2, 0), "move diagonal") - -reset_entity(entity) -test(!entity.move(0, 0), "starting pos") - -let new_chunk = world.Chunk(64, 64, 1, 0) -chunk.add_neighbour(new_chunk, world.Direction.east) -reset_entity(entity, 63, 0) -test(entity.move(64, 0), "cross chunk") -test(entity.current_chunk == new_chunk, "changed chunk") - -entity.current_chunk := chunk -reset_entity(entity, 0, 1) -entity.current_chunk.set_tile(0, 1, world.Tile(#'wall_north)) -test(!entity.move(0, 0), "blocked by wall") -test(entity.move(1,1), "move by wall") - -reset_entity(entity, 0, 0) -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") - -reset_entity(other_ent, 2, 2) -entity.timer := 0 -test(entity.gather(other_ent) == world.ActionResponse.invalid, "gather too far away") - -reset_entity(other_ent, 64, 0) -reset_entity(entity, 63, 0) -entity.timer := 0 -let gather_result = entity.gather(other_ent) -test(gather_result == world.ActionResponse.ok || gather_result == world.ActionResponse.unsuccessful, "gather accross chunk") -entity.tick() -test(entity.gather(other_ent) == world.ActionResponse.cooldown, "gather cooldown") + lib("web-server/http.rkt") open + lib("web-server/servlet.rkt") open + lib("web-server/servlet-env.rkt") open + "thread/thread.rhm" open + +fun start(req): + #{response/xexpr}("Hello world!") + +//#{serve/servlet}(start, #{#:launch-browser?}: #false, #{#:servlet-path}: "/game") + +fun loop(): + println("Hello World!") + sleep(1) + loop() + +fun hello(): + let thread_evt = thread_receive_evt() + let sync_res = sync_timeout(1/500, thread_evt) + if !sync_res + | println("No sync result") + | println("Got message " +& thread_receive()) + +println("Creating thread") +def thr: thread(hello) +thread_send(thr, "Hello") +thread_wait(thr) +println("Thread done") diff --git a/chunk.rhm b/src/chunk.rhm index 3ded42d..3ded42d 100644 --- a/chunk.rhm +++ b/src/chunk.rhm diff --git a/entity.rhm b/src/entity.rhm index 3fcd812..3fcd812 100644 --- a/entity.rhm +++ b/src/entity.rhm diff --git a/src/test.rhm b/src/test.rhm new file mode 100644 index 0000000..5648c53 --- /dev/null +++ b/src/test.rhm @@ -0,0 +1,67 @@ +#lang rhombus/static + +import: + "world.rhm" + +fun test(test_result :: Boolean, test_str :: String): + if test_result + | println("Passed " +& test_str) + | println("Failed " +& test_str) + +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) +wrld.chunks.add(chunk) +let entity = world.EntityPlayer(0, 0, 0, chunk) +wrld.entities.add(entity) + + +reset_entity(entity) +test(entity.move(1, 0), "move horizontal") + +reset_entity(entity, 1, 1) +test(entity.move(0, 1), "move vertical") + +reset_entity(entity) +test(entity.move(1, 1), "move diagonal") + +reset_entity(entity) +test(!entity.move(2, 0), "move diagonal") + +reset_entity(entity) +test(!entity.move(0, 0), "starting pos") + +let new_chunk = world.Chunk(64, 64, 1, 0) +chunk.add_neighbour(new_chunk, world.Direction.east) +reset_entity(entity, 63, 0) +test(entity.move(64, 0), "cross chunk") +test(entity.current_chunk == new_chunk, "changed chunk") + +entity.current_chunk := chunk +reset_entity(entity, 0, 1) +entity.current_chunk.set_tile(0, 1, world.Tile(#'wall_north)) +test(!entity.move(0, 0), "blocked by wall") +test(entity.move(1,1), "move by wall") + +reset_entity(entity, 0, 0) +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") + +reset_entity(other_ent, 2, 2) +entity.timer := 0 +test(entity.gather(other_ent) == world.ActionResponse.invalid, "gather too far away") + +reset_entity(other_ent, 64, 0) +reset_entity(entity, 63, 0) +entity.timer := 0 +let gather_result = entity.gather(other_ent) +test(gather_result == world.ActionResponse.ok || gather_result == world.ActionResponse.unsuccessful, "gather accross chunk") +entity.tick() +test(entity.gather(other_ent) == world.ActionResponse.cooldown, "gather cooldown") diff --git a/world.rhm b/src/world.rhm index 9357422..9357422 100644 --- a/world.rhm +++ b/src/world.rhm diff --git a/thread/thread.rhm b/thread/thread.rhm new file mode 100644 index 0000000..a0c1b18 --- /dev/null +++ b/thread/thread.rhm @@ -0,0 +1,20 @@ +#lang rhombus/static + +import "thread.rkt" open + +export: + thread + thread_send + thread_receive + thread_receive_evt + thread_wait + sleep + sync_timeout + current_thread + +def thread_send: #{thread-send} +def thread_receive: #{thread-receive} +def thread_receive_evt: #{thread-receive-evt} +def thread_wait: #{thread-wait} +def sync_timeout: #{sync/timeout} +def current_thread: #{current-thread} diff --git a/thread/thread.rkt b/thread/thread.rkt new file mode 100644 index 0000000..bf7d8eb --- /dev/null +++ b/thread/thread.rkt @@ -0,0 +1,10 @@ +#lang racket + +(provide thread + thread-send + thread-receive + thread-receive-evt + thread-wait + sleep + sync/timeout + current-thread) |