Skip to content

Commit

Permalink
Add shooting
Browse files Browse the repository at this point in the history
  • Loading branch information
borodust committed Apr 20, 2017
1 parent f23d6d9 commit c0207d3
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 33 deletions.
18 changes: 10 additions & 8 deletions client/src/ball.lisp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
(body geom)) (body geom))




(defmethod initialize-instance :after ((this ball-body) &key) (defmethod initialize-instance :after ((this ball-body) &key position force)
(with-slots (body geom) this (with-slots (body geom) this
(setf body (make-rigid-body) (setf body (make-rigid-body)
geom (make-instance 'ball-geom :radius 1.025)) geom (make-instance 'ball-geom :radius (/ 1.025 2)))
(bind-geom geom body) (when force

(apply-force body force))
(setf (position-of body) (vec3 0.0 10.0 0.0)))) (when position
(setf (position-of body) position))
(bind-geom geom body)))




(defmethod transform-of ((this ball-body)) (defmethod transform-of ((this ball-body))
Expand Down Expand Up @@ -60,14 +62,14 @@
(body mesh program)) (body mesh program))




(defmethod initialization-flow ((this ball-model) &key) (defmethod initialization-flow ((this ball-model) &key position force)
(with-slots (body mesh program) this (with-slots (body mesh program) this
(>> (resource-flow "mesh.Ball" (shading-program-resource-name "passthru-program")) (>> (resource-flow "mesh.Ball" (shading-program-resource-name "passthru-program"))
(instantly (m p) (instantly (m p)
(setf mesh m (setf mesh m
program p)) program p))
(-> ((physics)) () (-> ((physics)) ()
(setf body (make-instance 'ball-body))) (setf body (make-instance 'ball-body :position position :force force)))
(call-next-method)))) (call-next-method))))




Expand All @@ -84,7 +86,7 @@




(defmethod scene-pass ((this ball-model) (pass rendering-pass) ball-transform) (defmethod scene-pass ((this ball-model) (pass rendering-pass) ball-transform)
(let ((*model-matrix* (mult *model-matrix* ball-transform))) (let ((*model-matrix* ball-transform))
(call-next-method))) (call-next-method)))




Expand Down
4 changes: 2 additions & 2 deletions client/src/camera.lisp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
(with-slots (player front-gaze) this (with-slots (player front-gaze) this
(let* ((pos (position-of player)) (let* ((pos (position-of player))
(rotation (rotation-of player))) (rotation (rotation-of player)))
(setf (transform-of this) (mult (euler-angles->mat4 (vec3 (- (x rotation)) (y rotation) 0.0)) (setf (transform-of this) (mult (euler-angles->mat4 (vec3 (- (x rotation)) (- (y rotation)) 0.0))
(translation-mat4 (- (x pos)) -10.0 (- (y pos))))))) (translation-mat4 (- (x pos)) -13.0 (y pos))))))
(call-next-method)) (call-next-method))
62 changes: 60 additions & 2 deletions client/src/dude.lisp
Original file line number Original file line Diff line number Diff line change
@@ -1,6 +1,41 @@
(in-package :mortar-combat) (in-package :mortar-combat)




(defclass dude-bounds (collidable cylinder-geom) ())


(defclass dude-body (disposable)
(body bounds))


(defmethod initialize-instance :after ((this dude-body) &key)
(with-slots (body bounds) this
(setf body (make-rigid-body)
bounds (make-instance 'dude-bounds
:radius 2.0
:length 13.0))
#++(bind-geom bounds body)))


(define-destructor dude-body (body bounds)
(dispose bounds)
(dispose body))


(defmethod (setf position-of) (value (this dude-body))
(with-slots (body) this
(setf (position-of body) value)))


(defmethod transform-of ((this dude-body))
(with-slots (body) this
(transform-of body)))


(defmethod rotation-of ((this dude-body))
(with-slots (body) this
(rotation-of body)))

;;; ;;;
;;; ;;;
;;; ;;;
Expand Down Expand Up @@ -33,12 +68,12 @@
(mult (bone-transform name) offset))) (mult (bone-transform name) offset)))
(call-next-method)))) (call-next-method))))



;;; ;;;
;;; ;;;
;;; ;;;
(defclass dude-model (model) (defclass dude-model (model)
((mesh :initform nil) ((mesh :initform nil)
(body :initform nil)
(program :initform nil) (program :initform nil)
(run-animation :initform nil) (run-animation :initform nil)
(rest-animation :initform nil) (rest-animation :initform nil)
Expand All @@ -51,7 +86,7 @@




(defmethod initialization-flow ((this dude-model) &key) (defmethod initialization-flow ((this dude-model) &key)
(with-slots (mesh animation skeleton program (with-slots (mesh animation skeleton program body
strafe-animation run-animation rest-animation) strafe-animation run-animation rest-animation)
this this
(>> (resource-flow "mesh.Dude" "Stickman" (>> (resource-flow "mesh.Dude" "Stickman"
Expand All @@ -66,12 +101,35 @@
run-animation run run-animation run
rest-animation rest rest-animation rest
program p)) program p))
(-> ((physics)) ()
(setf body (make-instance 'dude-body)))
(call-next-method)))) (call-next-method))))




(defmethod discard-node ((this dude-model))
(with-slots (body) this
(dispose body)))


(defmethod model-graph-assembly-flow ((this dude-model)) (defmethod model-graph-assembly-flow ((this dude-model))
(with-slots (skeleton mesh program color rest-animation) this (with-slots (skeleton mesh program color rest-animation) this
(scenegraph (scenegraph
((animation-node :initial-animation rest-animation :name :dude-animation) ((animation-node :initial-animation rest-animation :name :dude-animation)
((animated-skeleton-node :root-bone skeleton) ((animated-skeleton-node :root-bone skeleton)
((dude-mesh :mesh mesh :program program :color color))))))) ((dude-mesh :mesh mesh :program program :color color)))))))


(defmethod scene-pass ((this dude-model) (pass simulation-pass) input)
(with-slots (body) this
#++(let* ((pos (mult *model-matrix* (vec4 0.0 0.0 0.0 1.0)))
(w (w pos)))
(flet ((w/ (v)
(/ v w)))
(setf (position-of body) (vec3 (w/ (x pos))
(w/ (y pos))
(w/ (z pos))))))
(call-next-method)))


(defmethod scene-pass ((this dude-model) (pass rendering-pass) input)
(call-next-method))
12 changes: 10 additions & 2 deletions client/src/keymap.lisp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
(key-table :initform (make-hash-table :test 'eq)))) (key-table :initform (make-hash-table :test 'eq))))




(defun mouse-button->keymap-button (button)
(case button
(:left :mouse-left)
(:right :mouse-right)
(:middle :mouse-middle)))


(defun enable-keymap (keymap) (defun enable-keymap (keymap)
(with-slots (callbacks cursor-action key-table) keymap (with-slots (callbacks cursor-action key-table) keymap
(when callbacks (when callbacks
Expand All @@ -19,8 +26,9 @@
(when-let ((action (gethash (key-from ev) key-table))) (when-let ((action (gethash (key-from ev) key-table)))
(funcall action (state-from ev)))) (funcall action (state-from ev))))
(process-button-event (ev) (process-button-event (ev)
(when-let ((action (gethash (button-from ev) key-table))) (let ((button (mouse-button->keymap-button (button-from ev))))
(funcall action (state-from ev)))) (when-let ((action (gethash button key-table)))
(funcall action (state-from ev)))))
(process-cursor-event (ev) (process-cursor-event (ev)
(when cursor-action (when cursor-action
(funcall cursor-action (x-from ev) (y-from ev))))) (funcall cursor-action (x-from ev) (y-from ev)))))
Expand Down
61 changes: 44 additions & 17 deletions client/src/main.lisp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@




(define-constant +framestep+ 0.016) (define-constant +framestep+ 0.016)
(define-constant +player-speed+ 15.0)
(defvar *main-latch* (mt:make-latch)) (defvar *main-latch* (mt:make-latch))






(defclass mortar-combat (enableable generic-system) (defclass mortar-combat (enableable generic-system dispatcher)
((scene :initform nil) ((scene :initform nil :reader scene-of)
(keymap :initform (make-instance 'keymap)) (task-queue :initform nil)
(keymap :initform nil)
(player :initform nil)) (player :initform nil))
(:default-initargs :depends-on '(graphics-system (:default-initargs :depends-on '(graphics-system
physics-system physics-system
Expand All @@ -25,17 +27,33 @@
((projection-node :aspect (/ 800 600)) ((projection-node :aspect (/ 800 600))
((player-camera :player player) ((player-camera :player player)
(room-model) (room-model)
(ball-model) ((scene-node :name :ball-group))
((transform-node :translation (vec3 4.0 0.0 0.0)) ((dude-model :color (vec3 0.9 0.4 0.4))
(mortar-model) (mortar-model)))))))
((dude-model :color (vec3 0.9 0.4 0.4)))))))))

(defmethod dispatch ((this mortar-combat) (task function) invariant &key)
(with-slots (task-queue) this
(push-task task task-queue)))


(defun shoot-ball (scene player)
(let ((pos (position-of player)))
(run (>> (assembly-flow 'ball-model
:position (vec3 (+ (x pos) 1.0) 10.0 (- (y pos)))
:force (mult (gaze-of player) 10000))
(-> ((mortar-combat)) (ball)
(let ((group (find-node (root-of scene) :ball-group)))
(adopt group ball)))))))




(defmethod initialize-system :after ((this mortar-combat)) (defmethod initialize-system :after ((this mortar-combat))
(with-slots (scene player keymap) this (with-slots (scene player keymap task-queue) this
(register-resource-loader (make-resource-loader (asset-path "font.brf") (register-resource-loader (make-resource-loader (asset-path "font.brf")
(asset-path "dude-and-mortar.brf"))) (asset-path "dude-and-mortar.brf")))
(setf player (make-instance 'player)) (setf player (make-instance 'player)
keymap (make-instance 'keymap)
task-queue (make-task-queue))


(let ((prev-x nil) (let ((prev-x nil)
(prev-y nil) (prev-y nil)
Expand All @@ -44,15 +62,15 @@
(when (and prev-x prev-y) (when (and prev-x prev-y)
(let ((ax (/ (- y prev-y) 1000)) (let ((ax (/ (- y prev-y) 1000))
(ay (/ (- x prev-x) 1000))) (ay (/ (- x prev-x) 1000)))
(look-at player ax ay))) (look-at player ax (- ay))))
(setf prev-x x (setf prev-x x
prev-y y)) prev-y y))
(key-velocity (key) (key-velocity (key)
(case key (case key
(:w (vec2 0.0 -10.0)) (:w (vec2 0.0 +player-speed+))
(:s (vec2 0.0 10.0)) (:s (vec2 0.0 (- +player-speed+)))
(:a (vec2 -10.0 0.0)) (:a (vec2 (- +player-speed+) 0.0))
(:d (vec2 10.0 0.0)))) (:d (vec2 +player-speed+ 0.0))))
(update-velocity () (update-velocity ()
(setf (player-velocity player) (reduce #'add movement-keys (setf (player-velocity player) (reduce #'add movement-keys
:key #'key-velocity :key #'key-velocity
Expand All @@ -62,12 +80,18 @@
(case state (case state
(:pressed (push button movement-keys)) (:pressed (push button movement-keys))
(:released (deletef movement-keys button))) (:released (deletef movement-keys button)))
(update-velocity)))) (update-velocity)))
(shoot (state)
(when (eq :pressed state)
(shoot-ball scene player))))

(bind-cursor keymap #'rotate-camera) (bind-cursor keymap #'rotate-camera)

(bind-button keymap :w (update-buttons :w)) (bind-button keymap :w (update-buttons :w))
(bind-button keymap :s (update-buttons :s)) (bind-button keymap :s (update-buttons :s))
(bind-button keymap :a (update-buttons :a)) (bind-button keymap :a (update-buttons :a))
(bind-button keymap :d (update-buttons :d)))) (bind-button keymap :d (update-buttons :d))
(bind-button keymap :mouse-left #'shoot)))


(enable-keymap keymap) (enable-keymap keymap)


Expand All @@ -86,7 +110,10 @@
scenegraph-root))) scenegraph-root)))
(concurrently () (concurrently ()
(let (looped-flow) (let (looped-flow)
(setf looped-flow (>> (-> ((physics)) () (setf looped-flow (>> (instantly ()
(let ((*system* this))
(drain task-queue)))
(-> ((physics)) ()
(observe-universe +framestep+)) (observe-universe +framestep+))
(scene-processing-flow scene) (scene-processing-flow scene)
(instantly () (instantly ()
Expand Down
10 changes: 8 additions & 2 deletions client/src/player.lisp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@




(defclass player () (defclass player ()
((position :initform (vec2 0.0 18.0)) ((position :initform (vec2)) ; f(x,y) field space = f(x,-z) global space
(rotation :initform (vec2 0.0 0.0) :reader rotation-of) (rotation :initform (vec2) :reader rotation-of)


(updated-at :initform (real-time-seconds)) (updated-at :initform (real-time-seconds))
(velocity :initform (vec2 0.0 0.0)))) (velocity :initform (vec2 0.0 0.0))))
Expand All @@ -27,6 +27,12 @@
position)) position))




(defun gaze-of (player)
"In global coords"
(with-slots (rotation) player
(normalize (rotate *forward-gaze* (euler-angles->quat (x rotation) (y rotation) 0.0)))))


(defun (setf player-velocity) (vec2 player) (defun (setf player-velocity) (vec2 player)
(with-slots (position velocity) player (with-slots (position velocity) player
(flush-position player) (flush-position player)
Expand Down
3 changes: 3 additions & 0 deletions client/src/utils.lisp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@


(defun asset-path (file) (defun asset-path (file)
(merge-pathnames file (merge-working-pathname (property :assets "assets/")))) (merge-pathnames file (merge-working-pathname (property :assets "assets/"))))


(defgeneric scene-of (obj))

0 comments on commit c0207d3

Please sign in to comment.