]> jxnshi.xyz Git - jxnshi.xyz.git/commitdiff
Working on cattoland
authorjxnshi <jxnshi@cock.li>
Sun, 15 Dec 2024 20:01:03 +0000 (21:01 +0100)
committerjxnshi <jxnshi@cock.li>
Sun, 15 Dec 2024 20:01:03 +0000 (21:01 +0100)
public/cattoland/cattoland.js
src/main.zig

index a8af32092de2db39ce04ba60b17308c820197427..71516b59b1d78cbba2f7c9e8bd0cdba3711249db 100644 (file)
@@ -43,8 +43,11 @@ let state;
 let socket;
 let socket_state;
 
+let player_id;
 let player;
 
+let requests_funcs = new Map();
+
 const event_strength = (event) => {
     return events.get(event) ? 1 : 0;  
 };
@@ -56,24 +59,6 @@ const image_load = (images, path) => {
     images.set(path, image);
 };
 
-const entity_init = () => {
-    return {
-        x: 0,
-        y: 0,
-        width: 16 * 3,
-        height: 16 * 3,
-        anchor: "bottom",
-        sheet_rect: { x: 0, y: 0, width: 16, height: 16 },
-        image: undefined,
-        speed: 3,
-        anim: undefined,
-        anim_frame_size: 16,
-        anim_frame: 0,
-        anim_counter: 0,
-        shadow: true,
-    };
-};
-
 const entity_play_anim = (entity, anim) => {
     entity.anim = anim;
     entity.anim_frame = 0;
@@ -103,24 +88,37 @@ const entity_update_anim = (entity) => {
     entity.anim_frame = 0;
 };
 
-const catto_init = () => {
-    let entity = entity_init();
-    entity.sheet_rect = { x: 0, y: 0, width: 16, height: 16 };
-    entity.anim = "idle_right";
-    entity.image = "cattoland/catto.png";
-
-    return entity;
-};
-
 const scene_clear = () => {
     entities = [];
 };
 
-const request = (content) => {
+const request = (content, func) => {
     let id = Math.floor(Math.random() * 1000000000);    
+
+    while (requests_funcs.has(id)) {
+        id = Math.floor(Math.random() * 1000000000);    
+    }
+
+    requests_funcs.set(id, func);
+
     socket.send(`${id} ${content}`);
 };
 
+const handle_message = (event) => {
+    let data = event.data;
+
+    let id_raw = data.split(' ')[0];
+    let id = parseInt(id_raw, 10);
+    
+    let content = data.substring(id_raw.length + 1);
+
+    let func = requests_funcs.get(id);
+
+    func(content);
+
+    requests_funcs.delete(id);
+};
+
 const socket_connect = () => {
     socket = new WebSocket("ws://localhost:3000");
     socket_state = "connecting";
@@ -132,22 +130,20 @@ const socket_connect = () => {
 
     socket.onopen = (event) => {
         socket_state = "connected";
-        request("Gimme catto pleaz.");
+
+        request("Gimme catto pleaz.", (content) => {
+            player_id = parseInt(content, 10);
+        });
+
         init_game();
     };
 
-    socket.onmessage = (event) => {
-        console.log(event.data);
-    };
+    socket.onmessage = handle_message;
 };
 
 const init_game = () => {
     scene_clear();
-
     state = "game";
-
-    player = catto_init();
-    entities.push(player);
 };
 
 const init_error = () => {
@@ -227,6 +223,10 @@ const loop = (delta) => {
             if (socket_state === "error") {
                 socket_connect();
             }
+        } else if (state === "game") {
+            request("Update plz.", (content) => {
+                entities = JSON.parse(content);
+            })
         }
     }
 
index ae53b3143c7b1dc1aefb13f9e62c96bdbf7d8910..8f07de23718000ec8986c16003e2b998b0d564fb 100644 (file)
@@ -107,12 +107,8 @@ fn handleClient(parent_allocator: std.mem.Allocator, connection: std.net.Server.
 
                     var git_iter = git_dir.iterate();
 
-<<<<<<< Updated upstream
                     try temp_buffer_writer.writeAll("<code>git clone repo</code>");
                     try temp_buffer_writer.writeAll("<ul id=\"repos-list\">\n");
-=======
-                    try temp_buffer_writer.writeAll("<ul id=\"repos-list\" class=\"no-marker-list\">\n");
->>>>>>> Stashed changes
 
                     while (try git_iter.next()) |entry| {
                         const repo_name = entry.name[0 .. entry.name.len - ".git".len];