mmo-project/lisp/client/common-lisp/client.lisp

69 lines
2.5 KiB
Common Lisp

(load "~/quicklisp/setup.lisp") ; need to load quicklisp
(ql:quickload '(:cl-raylib :cl-bcrypt :usocket :bordeaux-threads :alexandria :com.inuoe.jzon))
(defpackage :mmo-client
(:use :common-lisp :cl-raylib :3d-vectors :usocket :bordeaux-threads :alexandria :com.inuoe.jzon))
(in-package :mmo-client)
(defvar *lock* (bt:make-lock))
(defvar *logged-in-users-hash* (make-hash-table))
(defun add-entry (key value)
(bt:with-lock-held (*lock*)
(setf (gethash key *logged-in-users-hash*) value)))
(defun get-value (key)
(bt:with-lock-held (*lock*)
(gethash key *logged-in-users-hash*)))
(add-entry 'pos (vec 0.0 0.0 0.0))
(defun create-client ()
(usocket:with-client-socket (socket stream "127.0.0.1" 5001
:element-type 'character)
(unwind-protect
(progn
(loop
(format stream "~a~%" (com.inuoe.jzon:stringify (get-value 'pos)))
(force-output stream)
(usocket:wait-for-input socket)
(format t "from server: ~a~%" (read-line stream)))))
(usocket:socket-close socket)))
(defun game-loop ()
(let* ((screen-width 800)
(screen-height 450)
(username "username")
(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)))
(with-window (screen-width screen-height title)
(disable-cursor)
(set-target-fps 60) ; Set our game to run at 60 FPS
(loop
until (window-should-close) ; detect window close button or ESC key
do
(update-camera camera :camera-third-person)
(add-entry 'pos (list (camera3d-target camera) username :red :move))
(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))
(floor (vy cube-screen-pos) ) 20 :black))))))
(bt:make-thread 'create-client)
(bt:make-thread 'game-loop)
(bt:run-in-new-thread (bt:get-thread 'create-client))
(bt:run-in-new-thread (bt:get-thread 'game-loop))