#|
  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 (component (main-camera) 'scene-object-component) :dz -0.1)))
  (global-set-key (:j :continuous) (lambda () (move (component (main-camera) 'scene-object-component) :dz 0.1)))
  (global-set-key (:q :continuous) (lambda () (move (component (main-camera) 'scene-object-component) :dx -0.1)))
  (global-set-key (:k :continuous) (lambda () (move (component (main-camera) 'scene-object-component) :dx 0.1)))
  (global-set-key (:o :continuous) (lambda () (rotate (component (main-camera) 'scene-object-component) :dz -2)))
  (global-set-key (:u :continuous) (lambda () (rotate (component (main-camera) 'scene-object-component) :dz 2)))
  (let (freelook-mode)
    (flet ((set-freelook (enable)
             (setf freelook-mode enable))
           (freelook-move (dx dy)
             (when freelook-mode
               (rotate (component (main-camera) 'scene-object-component) :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 "World"))

(defun finalize ())

(defun update (delta-time)
  (declare (ignore delta-time))
  (unless (null (world))
    (locking-scene
      (with-components (camera-component scene-object-component) (main-camera)
        (with-slots (position direction) scene-object-component
          (update-view camera-component position direction))))))

(defmodule stoe-game :game)
