#lang rhombus/static import: "chunk.rhm" open "entity.rhm" open "item.rhm" open "../thread/thread.rhm" open export: all_from("entity.rhm") all_from("chunk.rhm") all_from("item.rhm") all_defined class MessageLogin(user :: String, thr) class MessageLoginResponse(user_id :: Int): // TODO can I automate this? method to_map() :: Map: {"user_id": user_id} class MessageGetChunks(user_id :: Int, thr) class MessageGetChunksResponse(chunk :: Chunk): method to_map() :: Map: chunk.to_map() class World(chunks :: MutableList.now_of(Chunk), entities :: MutableMap.now_of(Int, Entity)): field thread_evt = thread_receive_evt() field tick_length = 0.6 // tick length is 600ms // TODO this should really go to some user DB field user_id = 100 constructor(): super(MutableList(), MutableMap()) method init(): let chunk = Chunk(64, 64, 0, 0) chunks.add(chunk) method login(user :: String, thr): // 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) player.equip[Slot.right_hand] := Item.bronze_axe entities[user_id] := player let resp = MessageLoginResponse(user_id) user_id := user_id + 1 thread_send(thr, resp) 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 = chunks[0] let resp = MessageGetChunksResponse(chunk) thread_send(thr, resp) method process_messages(timestamp :: Real): let time_passed = current_timestamp() - timestamp let remaining_time = math.max(tick_length - time_passed, 0) let sync_res = sync_timeout(remaining_time, thread_evt) if sync_res | let msg = thread_receive() match msg | MessageLogin(user, thr): login(user, thr) | MessageGetChunks(user_id, thr): get_chunk(user_id, thr) | ~else: println("Unknown message " +& msg) process_messages(timestamp) | #true // Return here method tick_loop(): // TODO step through entities and simulate them let starting_time = current_timestamp() process_messages(starting_time) println("Done tick") tick_loop() method simulate(): tick_loop()