b9c95253cef799e5300b12bf5ec8010c558580c3 — Thomas Letan 1 year, 6 months ago 3a71e25
feature: Introduce a generic step function called every step

Every ~dt~ seconds, the ~frame-step~ function is called for each
instance currently spawned by the daemon. If an instance returns
~nil~, this means it should be its last frame.

Signed-off-by: Thomas Letan <contact@thomasletan.fr>
4 files changed, 54 insertions(+), 8 deletions(-)

M daemon/main.lisp
A daemon/player.lisp
M daemon/scene.lisp
M lycan.asd
M daemon/main.lisp => daemon/main.lisp +21 -8
@@ 27,22 27,35 @@
   (make-instance 'daemon))
 
 (defmethod register-new-player ((dm daemon) socket)
-  (register-player *pool* socket)
+  (register-player *pool* (as:socket-data socket))
   (push socket (players dm)))
 
-(defmethod init ((g daemon))
+(defmethod init ((dm daemon) port)
   (as:tcp-server
    "127.0.0.1"
-   4000
+   port
    nil
-   :connect-cb (lambda (socket) (register-new-player g socket))))
+   :event-cb (lambda (err) (server-event-cb dm err))
+   :connect-cb (lambda (socket) (server-connect-cb dm socket))))
 
-(defmethod frame-step ((dm daemon))
+(defgeneric frame-step (dynamic dt))
+
+(defmethod frame-step ((dm daemon) dt)
+  (frame-step *pool* dt)
   (dolist (socket (players dm))
     (as:write-socket-data socket "hi")))
 
+(defmethod server-connect-cb ((dm daemon) socket)
+  (let ((player (make-instance 'player)))
+    (setf (as:socket-data socket) player)
+    (register-new-player dm socket)))
+
+(defmethod server-event-cb ((dm daemon) err)
+  (print err))
+
 (defun main ()
-  (let ((dm (make-daemon)))
+  (let ((dm (make-daemon))
+        (dt 1))
     (as:with-event-loop (:catch-app-errors t)
-      (init dm)
-      (as:interval (lambda () (frame-step dm)) :time 1))))
+      (init dm 4000)
+      (as:interval (lambda () (frame-step dm dt)) :time dt))))

A daemon/player.lisp => daemon/player.lisp +21 -0
@@ 0,0 1,21 @@
+;; lycan.lisp: yet another game server for the lycan project
+;; Copyright (C) 2018 Thomas Letan <contact@thomasletan.fr>
+;;
+;; This program is free software: you can redistribute it and/or modify
+;; it under the terms of the GNU Affero General Public License as published
+;; by the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+;;
+;; This program is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU Affero General Public License for more details.
+;;
+;; You should have received a copy of the GNU Affero General Public License
+;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+(cl:in-package :lycan/daemon)
+
+(defclass player ()
+  ((current-instance :initform nil
+                     :accessor current-instance)))

M daemon/scene.lisp => daemon/scene.lisp +11 -0
@@ 39,6 39,12 @@
         (push i (instances target))
         (register-player i player))))
 
+(defmethod frame-step ((p pool) dt)
+  (dolist (i (instances p))
+    (when (not (frame-step i dt))
+      ;; TODO: The instance is asking for us to kill her.
+      )))
+
 (defclass instance ()
   ((scene :initarg :scene
           :initform (error "An instance needs a scene")


@@ 54,4 60,9 @@
 
 (defmethod register-player ((target instance) player)
   (vom:info "Register player ~a to instance ~a" player target)
+  (setf (current-instance player) target)
   (push player (players target)))
+
+(defmethod frame-step ((i instance) dt)
+  ;; TODO: Implement gameplay here
+  t)

M lycan.asd => lycan.asd +1 -0
@@ 22,5 22,6 @@
   :serial t
   :depends-on (cl-async alexandria vom)
   :components ((:file "daemon")
+               (:file "daemon/player")
                (:file "daemon/scene")
                (:file "daemon/main")))