読者です 読者をやめる 読者になる 読者になる

`(kakko ,man)

Find a guide into tomorrow by taking lessons from the past

Lisp Game Programming <Step11-1>

Step11の最初はshipがenemy、enemy-shotと衝突した場合の処理プログラム

互いの位置が重なったかどうかで衝突を判断しているが、重なった場合、hitフラグをonにし、enemy、enemy-shotのstateを0(dead)にして、shipのstateを2(explode)に設定しつつ、explode-cntを0に設定して爆発カウンターを起動する

;; Step11 <Judge Enemy Shot Hit>
;; -----------------------------------------------------------------------------------------------
(defgeneric Ship-hit-p (ship foe))

(defmethod Ship-hit-p (ship foe)
  (when (= (state ship) 1) ; if ship is alive
  (let ((hit 0))
    (dolist (enemy (enemy-list foe))
      (when (and (= (state enemy) 1) ; enemies and ship hit
                         (> (+ (x ship) 16) (x enemy))
                         (< (+ (x ship) 16) (+ (x enemy) 31))
                         (> (+ (y ship) 16) (y enemy))
                         (< (+ (y ship) 16) (+ (y enemy) 31)))
        (setf (state enemy) 0
                hit 1)))
      (dolist (enemy-shot (enemy-shot-list foe))
        (when (and (= (state enemy-shot) 1) ; enemies shot and ship hit
                           (> (+ (x ship) 16) (x enemy-shot))
                           (< (+ (x ship) 16) (+ (x enemy-shot) 15))
                           (> (+ (y ship) 16) (y enemy-shot))
                           (< (+ (y ship) 16) (+ (y enemy-shot) 15)))
          (setf (state enemy-shot) 0
                  hit 1)))
      (when (= hit 1)
        (setf (state ship) 2 ; ship is explode
        (explode-cnt ship) 0))))) ; ship explode count 0