You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
1.8 KiB
56 lines
1.8 KiB
2 years ago
|
(in-package :pong.game)
|
||
|
|
||
|
(defclass action ()
|
||
|
((timestamp :initarg :timestamp
|
||
|
:initform (/ (get-internal-real-time) internal-time-units-per-second)
|
||
|
:reader action-timestamp)))
|
||
|
|
||
|
(defgeneric apply-action (action game))
|
||
|
|
||
|
(defclass start-game-action (action)
|
||
|
((name :initarg :name :initform nil :accessor name)))
|
||
|
|
||
|
(defclass start-down-action (action)
|
||
|
((paddle :initarg :paddle :initform nil :reader paddle)))
|
||
|
|
||
|
(defclass stop-down-action (action)
|
||
|
((paddle :initarg :paddle :initform nil :reader paddle)))
|
||
|
|
||
|
(defclass start-up-action (action)
|
||
|
((paddle :initarg :paddle :initform nil :reader paddle)))
|
||
|
|
||
|
(defclass stop-up-action (action)
|
||
|
((paddle :initarg :paddle :initform nil :reader paddle)))
|
||
|
|
||
|
(cpk:defencoding action timestamp)
|
||
|
(cpk:defencoding start-game-action name)
|
||
|
(cpk:defencoding start-down-action paddle)
|
||
|
(cpk:defencoding stop-down-action paddle)
|
||
|
(cpk:defencoding start-up-action paddle)
|
||
|
(cpk:defencoding stop-up-action paddle)
|
||
|
|
||
|
(defun get-paddle (paddle state)
|
||
|
(case paddle
|
||
|
(:left (state-left-paddle state))
|
||
|
(:right (state-right-paddle state))) )
|
||
|
|
||
|
(defmethod apply-action ((action start-down-action) (game game))
|
||
|
(let* ((state (game-state game))
|
||
|
(paddle (get-paddle (paddle action) state)))
|
||
|
(incf (paddle-vy paddle) +paddle-speed+)))
|
||
|
|
||
|
(defmethod apply-action ((action stop-down-action) (game game))
|
||
|
(let* ((state (game-state game))
|
||
|
(paddle (get-paddle (paddle action) state)))
|
||
|
(decf (paddle-vy paddle) +paddle-speed+)))
|
||
|
|
||
|
(defmethod apply-action ((action start-up-action) (game game))
|
||
|
(let* ((state (game-state game))
|
||
|
(paddle (get-paddle (paddle action) state)))
|
||
|
(decf (paddle-vy paddle) +paddle-speed+)))
|
||
|
|
||
|
(defmethod apply-action ((action stop-up-action) (game game))
|
||
|
(let* ((state (game-state game))
|
||
|
(paddle (get-paddle (paddle action) state)))
|
||
|
(incf (paddle-vy paddle) +paddle-speed+)))
|