#|
  This file is a part of stoe project.
  Copyright (c) 2015 Renaud Casenave-Péré (renaud@casenave-pere.fr)
|#

(uiop:define-package :stoe-game
  (:use :cl :stoe))
(in-package :stoe-game)

(defun load-cube ()
  (locking-scene
    (create-model "cube" #P"data/cube.dae")))

(defun load-tie ()
  (locking-scene
    (create-model "tie fighter" #P"data/TypeFighter.dae")))

(defun load-levi ()
  (locking-scene
    (create-model "levi" #P"data/levi.dae")))

(defun setup-input ()
  (global-set-key :escape #'quit)
  (global-set-key (:e :continuous) (lambda () (move (get-first-component (main-camera (world)) :graph-node) :dz -0.1)))
  (global-set-key (:j :continuous) (lambda () (move (get-first-component (main-camera (world)) :graph-node) :dz 0.1)))
  (global-set-key (:q :continuous) (lambda () (move (get-first-component (main-camera (world)) :graph-node) :dx -0.1)))
  (global-set-key (:k :continuous) (lambda () (move (get-first-component (main-camera (world)) :graph-node) :dx 0.1)))
  (global-set-key (:o :continuous) (lambda () (rotate (get-first-component (main-camera (world)) :graph-node) :dz -2)))
  (global-set-key (:u :continuous) (lambda () (rotate (get-first-component (main-camera (world)) :graph-node) :dz 2)))
  (let (freelook-mode)
    (flet ((set-freelook (enable)
             (setf freelook-mode enable))
           (freelook-move (dx dy)
             (when freelook-mode
               (rotate (get-first-component (main-camera (world)) :graph-node) :dx dx :dy dy))))
      (global-set-key 3 #'set-freelook t)
      (global-set-key (3 :release) #'set-freelook nil)
      (global-set-motion #'freelook-move :dx :dy))))

(defun initialize (&optional argv)
  (declare (ignore argv))
  (setup-input)
  (create-world))

(defun finalize ())

(defun update (delta-time)
  (declare (ignore delta-time))
  (unless (null (world))
    (locking-scene
      (when (main-camera (world))
        (update-view (get-first-component (main-camera (world)) :camera))))))

(defmodule stoe-game :game)
