2023-12-16 15:06:29 -05:00
( load "~/quicklisp/setup.lisp" ) ; need to load quicklisp
2024-01-01 16:33:39 -05:00
( ql:quickload ' ( :cl-raylib :cl-bcrypt :websocket-driver-client :alexandria :com.inuoe.jzon ) )
2023-12-16 15:06:29 -05:00
( defpackage :mmo-client
2024-01-01 16:33:39 -05:00
( :use :common-lisp :cl-raylib :raylib :3d-vectors :websocket-driver-client :alexandria :com.inuoe.jzon ) )
2023-12-16 15:06:29 -05:00
( in-package :mmo-client )
2024-01-01 15:26:28 -05:00
( defvar *client* ( wsd:make-client "ws://localhost:12345" ) )
2024-01-01 19:04:32 -05:00
( defvar *current-user* ( make-hash-table ) )
( defvar *logged-in-users* ( make-hash-table ) )
2023-12-31 15:21:34 -05:00
2024-01-01 19:04:32 -05:00
;;
; 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
;;
2024-01-01 15:26:28 -05:00
( wsd:start-connection *client* )
( wsd:on :message *client*
( lambda ( message )
2024-01-01 19:04:32 -05:00
( setq json ( com.inuoe.jzon:parse message ) )
( setq cmd ( gethash "cmd" json ) )
( case
( ( = cmd "login" ) ( setf ( gethash "uid" *user* ) ( gethash "uid" json ) ) ) ; handles self login
( ( = 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!)
( ( = cmd "logout" ) ( ) ) ; log out another user
( ( = cmd "move" ) ( ) ) ; some user moved
( t ( format t "Unknown message: ~A~%" message ) ) ) ) ) ; some unknown thing happened
2024-01-01 16:33:39 -05:00
( let* ( ( screen-width 800 )
( screen-height 450 )
( username "zongor" )
( title "mmo client - common lisp" )
( camera ( make-camera3d :position ( vec 4.0 2.0 4.0 )
:target ( vec 0.0 0.5 0.0 )
:up ( vec 0.0 1.0 0.0 )
:fovy 60.0
:projection :camera-perspective ) )
( cube-screen-pos ( vec 0.0 0.0 ) ) )
2024-01-01 19:04:32 -05:00
( defparameter *login* ( make-hash-table ) )
( setf ( gethash "cmd" *login* ) "user-login" )
( setf ( gethash "user" *login* ) username )
2024-01-01 16:33:39 -05:00
( with-window ( screen-width screen-height title )
;; (disable-cursor)
( set-target-fps 60 ) ; Set our game to run at 60 FPS
2024-01-01 19:04:32 -05:00
( wsd:send *client* ( com.inuoe.jzon:stringify *login* ) )
2024-01-01 16:33:39 -05:00
( loop
until ( window-should-close ) ; detect window close button or ESC key
do
( update-camera camera :camera-third-person )
2024-01-01 19:04:32 -05:00
( defparameter *move* ( make-hash-table ) )
( setf ( gethash "cmd" *move* ) "move" )
( setf ( gethash "user" *move* ) username )
( setf ( gethash "pos" *move* ) ( camera3d-target camera ) )
2024-01-01 16:33:39 -05:00
( when ( is-key-down 87 )
2024-01-01 19:04:32 -05:00
( wsd:send *client* ( com.inuoe.jzon:stringify *move* ) ) )
2024-01-01 16:33:39 -05:00
( when ( is-key-down 65 )
2024-01-01 19:04:32 -05:00
( wsd:send *client* ( com.inuoe.jzon:stringify *move* ) ) )
2024-01-01 16:33:39 -05:00
( when ( is-key-down 83 )
2024-01-01 19:04:32 -05:00
( wsd:send *client* ( com.inuoe.jzon:stringify *move* ) ) )
2024-01-01 16:33:39 -05:00
( when ( is-key-down 68 )
2024-01-01 19:04:32 -05:00
( wsd:send *client* ( com.inuoe.jzon:stringify *move* ) ) )
2024-01-01 16:33:39 -05:00
( setf cube-screen-pos ( get-world-to-screen ( v+ ( camera3d-target camera ) ( vec 0 1.0 0 ) ) camera ) )
( with-drawing
( clear-background :raywhite )
( with-mode-3d ( camera )
( 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-grid 20 1.0 ) )
( draw-text username ( - ( floor ( vx cube-screen-pos ) ) ( floor ( measure-text username 20 ) 2 ) )
2024-01-01 19:04:32 -05:00
( floor ( vy cube-screen-pos ) ) 20 :black ) ) ) ) )
2024-01-01 16:33:39 -05:00
( wsd:close-connection *client* )