diff options
-rw-r--r-- | src/entity.rhm | 32 | ||||
-rw-r--r-- | src/test.rhm | 34 | ||||
-rw-r--r-- | src/world.rhm | 8 |
3 files changed, 37 insertions, 37 deletions
diff --git a/src/entity.rhm b/src/entity.rhm index 3fcd812..831228a 100644 --- a/src/entity.rhm +++ b/src/entity.rhm @@ -13,7 +13,7 @@ export: let rand = random.Random() -class Entity(id:: Int, mutable x :: Int, mutable y :: Int, mutable current_chunk :: Chunk): +class Entity(id:: Int, mutable x :: Int, mutable y :: Int): nonfinal // Validate that the provided coordinates are within the max distance range of // entities current coordinates. @@ -30,8 +30,7 @@ class Entity(id:: Int, mutable x :: Int, mutable y :: Int, mutable current_chunk // Validate that the x,y coordinates map to a chunk that is // near the current coordinates of the entity - method validate_chunk(x :: Int, y :: Int) :: maybe(Chunk): - let current_chunk = this.current_chunk + method validate_chunk(x :: Int, y :: Int, current_chunk :: Chunk) :: maybe(Chunk): let local_x = x - current_chunk.offset_x * current_chunk.width let local_y = y - current_chunk.offset_y * current_chunk.height let less_x = local_x < 0 @@ -55,8 +54,8 @@ class Entity(id:: Int, mutable x :: Int, mutable y :: Int, mutable current_chunk // Validate that the tile at the coordinates would not block a movement of the entity // to those coordinates from its current coordinates - method validate_move(x :: Int, y :: Int, max_dist :: Int, target_chunk :: Chunk): - let current_tile = this.current_chunk.get_tile(this.x, this.y) + method validate_move(x :: Int, y :: Int, max_dist :: Int, current_chunk :: Chunk, target_chunk :: Chunk): + let current_tile = current_chunk.get_tile(this.x, this.y) let target_tile = target_chunk.get_tile(x, y) let diff_x = x - this.x let diff_y = y - this.y @@ -74,24 +73,23 @@ class Entity(id:: Int, mutable x :: Int, mutable y :: Int, mutable current_chunk !(direction == current_block || direction == target_block) method - | move(x :: Int, y :: Int): move(x, y, 1) - | move(x :: Int, y :: Int, max_dist :: Int) :: Boolean: - let target_chunk = validate_chunk(x, y) + | move(x :: Int, y :: Int, current_chunk :: Chunk): move(x, y, 1, current_chunk) + | move(x :: Int, y :: Int, max_dist :: Int, current_chunk :: Chunk) :: maybe(Chunk): + let target_chunk = validate_chunk(x, y, current_chunk) cond - | validate_dist(x, y, max_dist) && target_chunk != #false && validate_move(x, y, max_dist, target_chunk!!): - this.current_chunk := target_chunk!! + | validate_dist(x, y, max_dist) && target_chunk != #false && validate_move(x, y, max_dist, current_chunk, target_chunk!!): this.x := x this.y := y - #true + target_chunk!! | ~else: #false class EntityGatherable(quantity :: Int, odds :: Real, cap :: ItemCap): extends Entity - constructor(id :: Int, x :: Int, y :: Int, current_chunk :: Chunk, cap :: ItemCap): - super(id, x, y, current_chunk)(10, 1 / 10, cap) + constructor(id :: Int, x :: Int, y :: Int, cap :: ItemCap): + super(id, x, y)(10, 1 / 10, cap) enum ActionResponse: ok @@ -109,8 +107,8 @@ class EntityPlayer(mutable timer :: Int): field inventory :: Array.now_of(maybe(Stack)) = Array.make(20, #false) field equip :: MutableMap.now_of(Slot, Item) = MutableMap() - constructor(id :: Int, x :: Int, y :: Int, current_chunk :: Chunk): - super(id, x, y, current_chunk)(0) + constructor(id :: Int, x :: Int, y :: Int): + super(id, x, y)(0) method tick(): // TODO this "ticks" the entity. Things like healing and other @@ -121,10 +119,10 @@ class EntityPlayer(mutable timer :: Int): | timer := max_timer | timer := timer - 1 - method gather(target_entity :: Entity) :: ActionResponse: + method gather(target_entity :: Entity, current_chunk :: Chunk) :: ActionResponse: let x = target_entity.x let y = target_entity.y - let target_chunk = validate_chunk(x, y) + let target_chunk = validate_chunk(x, y, current_chunk) let right_type = target_entity is_a EntityGatherable fun validate_gather_resource(entity :: EntityGatherable) :: ActionResponse: diff --git a/src/test.rhm b/src/test.rhm index 5648c53..01c4649 100644 --- a/src/test.rhm +++ b/src/test.rhm @@ -17,51 +17,49 @@ fun 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) +let entity = world.EntityPlayer(0, 0, 0) +wrld.entities[0] := entity reset_entity(entity) -test(entity.move(1, 0), "move horizontal") +test(entity.move(1, 0, chunk) == chunk, "move horizontal") reset_entity(entity, 1, 1) -test(entity.move(0, 1), "move vertical") +test(entity.move(0, 1, chunk) == chunk, "move vertical") reset_entity(entity) -test(entity.move(1, 1), "move diagonal") +test(entity.move(1, 1, chunk) == chunk, "move diagonal") reset_entity(entity) -test(!entity.move(2, 0), "move diagonal") +test(!entity.move(2, 0, chunk), "move diagonal") reset_entity(entity) -test(!entity.move(0, 0), "starting pos") +test(!entity.move(0, 0, chunk), "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") +test(entity.move(64, 0, chunk) == new_chunk, "cross 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") +chunk.set_tile(0, 1, world.Tile(#'wall_north)) +test(!entity.move(0, 0, chunk), "blocked by wall") +test(entity.move(1, 1, chunk) == chunk, "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) +let other_ent = world.EntityGatherable(1, 1, 1, world.ItemCap.chop) +let gather_result = entity.gather(other_ent, chunk) 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") +test(entity.gather(other_ent, chunk) == 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) +let gather_result = entity.gather(other_ent, chunk) 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") +test(entity.gather(other_ent, chunk) == world.ActionResponse.cooldown, "gather cooldown") diff --git a/src/world.rhm b/src/world.rhm index 01a1553..75747bd 100644 --- a/src/world.rhm +++ b/src/world.rhm @@ -6,6 +6,9 @@ import: "../thread/thread.rhm" open export: + all_from("entity.rhm") + all_from("chunk.rhm") + all_from("item.rhm") all_defined @@ -42,7 +45,7 @@ class World(chunks :: MutableList.now_of(Chunk), // TODO should be accessing some DB to get player and it should // return some session token that the client would use when refering // to the player - let player = EntityPlayer(user_id, 0, 0, chunks[0]) + let player = EntityPlayer(user_id, 0, 0) player.equip[Slot.right_hand] := Item.bronze_axe entities[user_id] := player let resp = MessageLoginResponse(user_id) @@ -52,7 +55,8 @@ class World(chunks :: MutableList.now_of(Chunk), method get_chunk(user_id :: Int, thr): // TODO there should be a session token to user entity system let player :: Entity = entities[user_id] - let chunk = player.current_chunk + //let chunk = player.current_chunk + let chunk = chunks[0] let resp = MessageGetChunksResponse(chunk) thread_send(thr, resp) |