WIP client, do not use this is broken!

This commit is contained in:
zongor 2024-01-04 08:46:46 -05:00
parent d648f49c68
commit 4ca8770bd5
1 changed files with 56 additions and 46 deletions

View File

@ -1,47 +1,50 @@
(load "~/quicklisp/setup.lisp") ; need to load quicklisp (load "~/quicklisp/setup.lisp") ; need to load quicklisp
(ql:quickload '(:cl-raylib :cl-bcrypt :websocket-driver-client :alexandria :com.inuoe.jzon)) (ql:quickload '(:cl-raylib :cl-bcrypt :websocket-driver-client
:alexandria :com.inuoe.jzon))
(defpackage :mmo-client (defpackage :mmo-client
(:use :common-lisp :cl-raylib :raylib :3d-vectors :websocket-driver-client :alexandria :com.inuoe.jzon)) (:use :common-lisp :cl-raylib :raylib :3d-vectors
:websocket-driver-client :alexandria :com.inuoe.jzon))
(in-package :mmo-client) (in-package :mmo-client)
(defvar *client* (wsd:make-client "ws://localhost:12345")) (defvar *client* (wsd:make-client "ws://localhost:12345"))
(defvar *current-user* (make-hash-table))
(defvar *logged-in-users* (make-hash-table)) (defvar *logged-in-users* (make-hash-table))
(defvar *current-user* (make-hash-table))
(defvar *login* (make-hash-table))
;;
; Game loop info
;
; when start connection happens the server will generate a random id
; this is called the `login` request, this should only be seen by this user
; this user will then associate the id that is returned from the server with
; this users username/password and send out a `user-login` request
; the server should then associate that user get their info from the database
; and broadcast the updated list to all logged in users to all logged in users
;
; the `logged-in-users` key:generated user id, value: hash of user info
; user info is username, color, and position
;
; in the game loop it will loop over all the logged in users in the hash,
; if the user in the loop is the current user then ignore the info and only
; use local user info, otherwise it will draw all of the other cubes
;;
(wsd:start-connection *client*) (wsd:start-connection *client*)
(wsd:on :message *client* (wsd:on :message *client*
(lambda (message) (lambda (message)
(setq json (com.inuoe.jzon:parse message)) (let* ((json (com.inuoe.jzon:parse message))
(setq cmd (gethash "cmd" json)) (cmd (gethash "cmd" json))
(case (uid (gethash "uid" json)))
((= cmd "login") (setf (gethash "uid" *user*) (gethash "uid" json))) ; handles self login (cond
((= cmd "user-login") (setf (gethash (gethash "uid" json) *logged-in-users*) (make-hash-table))) ; handles a new user logging on (not the user using this client!) ((equalp cmd "login")
((= cmd "logout") ()) ; log out another user (setf (gethash "uid" *current-user*) uid))
((= cmd "move") ()) ; some user moved ((equalp cmd "user-login")
(t (format t "Unknown message: ~A~%" message))))) ; some unknown thing happened (setf *logged-in-users* json))
((equalp cmd "logout")
(remhash uid *logged-in-users*))
((equalp cmd "move")
(setf (gethash "x" (gethash uid *logged-in-users*))
(gethash "x" json))
(setf (gethash "y" (gethash uid *logged-in-users*))
(gethash "y" json)))
(t
(format t "Unknown message: ~A~%" message))))))
(defun send-move (camera username)
(let ((move (make-hash-table)))
(setf (gethash "cmd" move) "move")
(setf (gethash "user" move) username)
(setf (gethash "x" move) (vx (camera3d-target camera)))
(setf (gethash "y" move) (vz (camera3d-target camera)))
(wsd:send *client* (com.inuoe.jzon:stringify move))))
(let* ((screen-width 800) (let* ((screen-width 800)
(screen-height 450) (screen-height 450)
(username "zongor") (username "username")
(title "mmo client - common lisp") (title "mmo client - common lisp")
(camera (make-camera3d :position (vec 4.0 2.0 4.0) (camera (make-camera3d :position (vec 4.0 2.0 4.0)
:target (vec 0.0 0.5 0.0) :target (vec 0.0 0.5 0.0)
@ -50,42 +53,49 @@
:projection :camera-perspective)) :projection :camera-perspective))
(cube-screen-pos (vec 0.0 0.0))) (cube-screen-pos (vec 0.0 0.0)))
(defparameter *login* (make-hash-table))
(setf (gethash "cmd" *login*) "user-login") (setf (gethash "cmd" *login*) "user-login")
(setf (gethash "user" *login*) username) (setf (gethash "user" *login*) username)
(setf (gethash "user" *current-user*) username)
(wsd:send *client* (com.inuoe.jzon:stringify *login*))
(with-window (screen-width screen-height title) (with-window (screen-width screen-height title)
;; (disable-cursor) ;; (disable-cursor)
(set-target-fps 60) ; Set our game to run at 60 FPS (set-target-fps 60) ; Set our game to run at 60 FPS
(wsd:send *client* (com.inuoe.jzon:stringify *login*))
(loop (loop
until (window-should-close) ; detect window close button or ESC key until (window-should-close) ; detect window close button or ESC key
do do
(update-camera camera :camera-third-person) (update-camera camera :camera-third-person)
(defparameter *move* (make-hash-table))
(setf (gethash "cmd" *move*) "move")
(setf (gethash "user" *move*) username)
(setf (gethash "pos" *move*) (camera3d-target camera))
(when (is-key-down 87) (when (is-key-down 87)
(wsd:send *client* (com.inuoe.jzon:stringify *move*))) (send-move camera username))
(when (is-key-down 65) (when (is-key-down 65)
(wsd:send *client* (com.inuoe.jzon:stringify *move*))) (send-move camera username))
(when (is-key-down 83) (when (is-key-down 83)
(wsd:send *client* (com.inuoe.jzon:stringify *move*))) (send-move camera username))
(when (is-key-down 68) (when (is-key-down 68)
(wsd:send *client* (com.inuoe.jzon:stringify *move*))) (send-move camera username))
(setf cube-screen-pos (get-world-to-screen (v+ (camera3d-target camera) (vec 0 1.0 0)) camera)) (setf cube-screen-pos
(get-world-to-screen
(v+ (camera3d-target camera) (vec 0 1.0 0)) camera))
(with-drawing (with-drawing
(clear-background :raywhite) (clear-background :raywhite)
(with-mode-3d (camera) (with-mode-3d (camera)
(draw-cube (camera3d-target camera) 1.0 1.0 1.0 :red) (draw-cube (camera3d-target camera) 1.0 1.0 1.0 :red)
(draw-cube-wires (camera3d-target camera) 1.0 1.0 1.0 :maroon) (draw-text username
(draw-grid 20 1.0)) (- (floor (vx cube-screen-pos))
(draw-text username (- (floor (vx cube-screen-pos)) (floor (measure-text username 20) 2)) (floor (measure-text username 20) 2))
(floor (vy cube-screen-pos) ) 20 :black))))) (floor (vy cube-screen-pos) ) 20 :black)
(loop :for user :being :the :hash-key :of *logged-in-users* :do
(let ((x (gethash "x" user))
(y (gethash "y" user)))
(draw-cube (vec x 0.5 y) 1.0 1.0 1.0 :red)
(let ((name (gethash "username" user)))
(draw-text name
(- (floor (vx cube-screen-pos))
(floor (measure-text name 20) 2))
(floor (vy cube-screen-pos)) 20 :black))))
(draw-grid 20 1.0))))))
(wsd:close-connection *client*) (wsd:close-connection *client*)