first commit
[uxul-world.git] / game-object-with-animation.lisp
1 ;;; Copyright 2009 Christoph Senjak
2
3 (in-package :uxul-world)
4
5 ;; as many game-objects do have an animation related to them, instead
6 ;; of just having a draw-method which will manually draw anything, we
7 ;; declare a standard-class for that, with some useful methods.
8
9 (defclass game-object-with-animation (game-object)
10   ((animation-translation :initarg :animation-translation
11                           :accessor animation-translation
12                           :initform (make-xy 0 0)
13                           :documentation "The translation of the animation")
14    (animation :initarg :animation
15               :accessor animation
16               :documentation "The animation of this object")))
17
18 (defmethod (setf animation) ((newval animation) (obj game-object-with-animation))
19   "Sets the animation and x and y-coordinates. Wont rewind the animation."
20     (setf (slot-value obj 'animation) newval)
21     (setf (x obj) (x obj))
22     (setf (y obj) (y obj))
23     (setf (visible obj) (visible obj)))
24
25 (defmethod (setf x) (newval (obj game-object-with-animation))
26   (call-next-method)
27   (setf (x (animation obj)) (+ (x obj) (x (animation-translation obj)))))
28
29 (defmethod (setf y) (newval (obj game-object-with-animation))
30   (call-next-method)
31   (setf (y (animation obj)) (+ (y obj) (y (animation-translation obj)))))
32
33 (defmethod (setf visible) (newval (obj game-object-with-animation))
34   (call-next-method)
35   (setf (visible (animation obj)) newval))
36
37 (defun rectangle-in-screen (obj)
38   (rectangles-overlap 
39    ;; HAAAAAAAAAAAAAAACK
40    (- (x obj) 50)
41    (- (y obj) 50)
42    (+ (x obj) (width obj) 50)
43    (+ (y obj) (height obj) 50)
44    *current-translation-x*
45    *current-translation-y*
46    (- +screen-width+ *current-translation-x*)
47    (- +screen-height+ *current-translation-y*)))
48
49
50
51 (defmethod draw ((obj game-object-with-animation))
52   ;(if (rectangle-in-screen obj)
53       (draw (animation obj))
54 ;)
55 )
56
57 (defmethod shared-initialize :after ((instance game-object-with-animation) spam &rest
58                                      initargs &key &allow-other-keys)
59   (declare (ignore initargs))
60   (declare (ignore spam))
61   "Set the x and y-Coordinates in the drawable and the rectangle (this
62 had to be done by hand before)"
63 ;  (write (x instance))
64 ;  (write (y instance))
65   (setf (x instance) (x instance))
66   (setf (y instance) (y instance))
67   (setf (visible instance) (visible instance)))
68