added doors and keys.
[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    (animation-bounds :initarg :animation-bounds
18                      :accessor animation-bounds
19                      :initform (make-xy 50 50)
20                      :documentation "When drawing, objects outside the
21    screen are tried not to be drawn via SDL. This determines, how far
22    in every direction the graphics may go outside the
23    collision-rectangle. Try to keep this number small. If it is too
24    huge, you may get numeric errors. 50/50 should be sufficient for
25    most objects. If this value is nil, the object will always be
26    drawn.")))
27
28 (defmethod (setf animation) ((newval animation) (obj game-object-with-animation))
29   "Sets the animation and x and y-coordinates. Wont rewind the animation."
30     (setf (slot-value obj 'animation) newval)
31     (setf (x obj) (x obj))
32     (setf (y obj) (y obj))
33     (setf (visible obj) (visible obj)))
34
35 (defmethod (setf x) (newval (obj game-object-with-animation))
36   (call-next-method)
37   (setf (x (animation obj)) (+ (x obj) (x (animation-translation obj)))))
38
39 (defmethod (setf y) (newval (obj game-object-with-animation))
40   (call-next-method)
41   (setf (y (animation obj)) (+ (y obj) (y (animation-translation obj)))))
42
43 (defmethod (setf visible) (newval (obj game-object-with-animation))
44   (call-next-method)
45   (setf (visible (animation obj)) newval))
46
47 (defun rectangle-in-screen (obj)
48   (let ((bounds (animation-bounds obj)))
49     (if bounds
50         (rectangles-overlap 
51          ;; HAAAAAAAAAAAAAAACK
52          (- (x obj) (x bounds))
53          (- (y obj) (y bounds))
54          (+ (x obj) (width obj) (x bounds))
55          (+ (y obj) (height obj) (y bounds))
56          (- *current-translation-x*)
57          (- *current-translation-y*)
58          (- +screen-width+ *current-translation-x*)
59          (- +screen-height+ *current-translation-y*))
60         T)))
61
62
63
64 (defmethod draw ((obj game-object-with-animation))
65   ;(if (rectangle-in-screen obj)
66       (draw (animation obj))
67 ;)
68 )
69
70 (defmethod shared-initialize :after ((instance game-object-with-animation) spam &rest
71                                      initargs &key &allow-other-keys)
72   (declare (ignore initargs))
73   (declare (ignore spam))
74   "Set the x and y-Coordinates in the drawable and the rectangle (this
75 had to be done by hand before)"
76 ;  (write (x instance))
77 ;  (write (y instance))
78   (setf (x instance) (x instance))
79   (setf (y instance) (y instance))
80   (setf (visible instance) (visible instance)))
81