1 ;;; Copyright 2009 Christoph Senjak
3 (in-package :uxul-world)
5 ;; define the standard-class player, which will represent the
9 (defclass player (game-object-with-animation)
10 ((dont-ignore :initarg :dont-ignore
13 :documentation "When we're testing whether we can go
14 on or not without colliding, we will set this flag to
15 nil, which means, that all collision-methods should
16 ONLY set the player to the position when it collides,
17 but NOT have any other effect (since its only a test)")
18 (animation-left :accessor animation-left
19 :initform (make-animation 7
22 (animation-right :accessor animation-right
23 :initform (make-animation 7
26 (animation :initarg :animation
28 :initform (make-animation 7
31 (last-y :initarg :last-pos
34 (bounced :initarg :bounced
37 (jump-accel :accessor jump-accel
39 (mayjump :accessor mayjump
41 (maycontjump :accessor maycontjump
43 (autojump :accessor autojump
45 :documentation "push jump-events even though no key is
46 pressed for n invocations.")
47 (overjump :initarg :overjump
50 :documentation "How many Frames to overjump until movement. Default 0.")
51 (overjumped :accessor overjumped
53 :documentation "DO NOT SET MANUALLY - counter for overjumped frames")
54 (width :initarg :width
57 (height :initarg :height
60 (animation-translation :initarg :animation-translation
61 :accessor animation-translation
62 :initform (make-xy -40 -20))
63 (key-pressed-up :initform nil :accessor key-pressed-up :initarg :key-pressed-up)
64 (key-pressed-down :initform nil :accessor key-pressed-down :initarg :key-pressed-down)
65 (key-pressed-left :initform nil :accessor key-pressed-left :initarg :key-pressed-left)
66 (key-pressed-right :initform nil :accessor key-pressed-right :initarg :key-pressed-right)
67 (go-down :initform 0 :accessor go-down :initarg :go-down)
68 (go-right :initform 0 :accessor go-right :initarg :go-right)
69 (power :initform 10 :accessor
70 power :initarg :power :documentation "power - will be decreased if
72 (tulips :initform 0 :accessor tulips :initarg :tulips)
73 (immortable :initform 0
75 :documentation "after hit by an enemy you wont be
76 wounded by another enemy for that ammount of
80 :documentation "Number of keys for doors")
84 ;; Interaction with enemies
85 (defgeneric player-hits-enemy (player enemy &rest args)
87 "To be called when a player collides with an enemy."))
89 (defmethod player-hits-enemy ((player t) (enemy t) &rest args)
90 (declare (ignore args))
91 "Shouldnt be called - warn only"
93 "player-hits-enemy called with non-fitting classes: ~A ~A~%"
94 (class-name (class-of player))
95 (class-name (class-of enemy))))
97 (defgeneric enemy-hits-player (enemy player &rest args)
99 "To be called when an enemy collides with a player."))
101 (defmethod enemy-hits-player ((enemy t) (player t) &rest args)
102 (declare (ignore args))
103 "Shouldnt be called - warn only"
105 "player-hits-enemy called with non-fitting classes: ~A ~A~%"
106 (class-name (class-of enemy))
107 (class-name (class-of player))))
109 ;; interaction with items
111 (defgeneric item-catch (item player &rest args)
112 (:documentation "Obvious"))
114 (defmethod item-catch ((item t) (player t) &rest args)
115 (declare (ignore args))
116 "Do nothing, just warn."
117 (format t "item-catch called with non-fitting classes: ~A ~A~%"
118 (class-name (class-of item))
119 (class-name (class-of player))))
121 (defmethod (setf animation) ((new-value animation) (object player))
122 (setf (x new-value) (+ (x object) (x(animation-translation object))))
123 (setf (y new-value) (+ (y object) (y(animation-translation object))))
126 (defmethod on-key-down ((obj player) key)
128 ((sdl:key= key :SDL-KEY-UP)
129 (setf (key-pressed-up obj) T))
130 ((sdl:key= key :SDL-KEY-DOWN)
131 (setf (key-pressed-down obj) T))
132 ((sdl:key= key :SDL-KEY-LEFT)
133 (setf (key-pressed-left obj) T)
134 (setf (animation obj) (animation-left obj))
135 (setf (animation-translation obj) (make-xy -40 -20))
136 (ensure-playing (animation obj))
138 ((sdl:key= key :SDL-KEY-RIGHT)
139 (setf (key-pressed-right obj) T)
140 (setf (animation obj) (animation-right obj))
141 (ensure-playing (animation obj))
142 (setf (animation-translation obj) (make-xy -20 -20))
146 (defmethod on-key-up ((obj player) key)
148 ((sdl:key= key :SDL-KEY-UP )
149 (setf (key-pressed-up obj) NIL))
150 ((sdl:key= key :SDL-KEY-DOWN)
151 (setf (key-pressed-down obj) NIL))
152 ((sdl:key= key :SDL-KEY-LEFT)
153 (setf (key-pressed-left obj) NIL)
154 (ensure-pause (animation obj)))
155 ((sdl:key= key :SDL-KEY-RIGHT)
156 (setf (key-pressed-right obj) NIL)
157 (ensure-pause (animation obj)))))
159 (defmethod invoke ((obj player))
160 "Do whatever a player does ^^"
164 (setf (bounced obj) nil))
167 (< (last-y obj) (y obj)))
168 (setf (mayjump obj) nil)))
170 (setf (last-y obj) (y obj))
172 ;; SIMPLE GRAVITY HACK
173 (setf (key-pressed-down obj) (not (key-pressed-up obj)))
175 (if (not (zerop (immortable obj))) (decf (immortable obj)))
177 (let ((go-left (if (key-pressed-left obj) 10 0))
178 (go-right (if (key-pressed-right obj) 10 0))
182 (setf (mayjump obj) nil)
183 (setf (maycontjump obj) t)
184 (setf go-up (jump-accel obj))
185 (setf (jump-accel obj) -49))
187 (setf go-up (jump-accel obj))
188 (incf (jump-accel obj) 3)
189 (when (zerop (jump-accel obj))
190 (setf (maycontjump obj) nil)
191 (setf (jump-accel obj) -50))))))
193 ((key-pressed-up obj)
195 ((> (autojump obj) 0)
197 (decf (autojump obj)))
198 (T (setf (maycontjump obj) nil)))
200 (move-about obj (make-xy (- go-right go-left) go-up)))))