src/aspects/coordinates.lisp @ 1c92535d2aec

I has a flavor
author Steve Losh <steve@stevelosh.com>
date Sat, 07 Jan 2017 19:11:35 +0000
parents 5560e722bc5c
children dd9a7ef86a21
(in-package :ap.entities)

(defparameter *world-contents*
  (make-array (list ap::*map-size*
                    ap::*map-size*)
    :initial-element nil))

(define-aspect coords x y)

(defun within-bounds-p (x y)
  (and (in-range-p 0 x ap::*map-size*)
       (in-range-p 0 y ap::*map-size*)))

(defun coords-insert-entity (e)
  (push e (aref *world-contents* (coords/x e) (coords/y e))))

(defun coords-remove-entity (e)
  (deletef (aref *world-contents* (coords/x e) (coords/y e)) e))

(defun coords-move-entity (e new-x new-y)
  (when (within-bounds-p new-x new-y)
    (coords-remove-entity e)
    (setf (coords/x e) new-x
          (coords/y e) new-y)
    (coords-insert-entity e)))

(defun coords-lookup (x y)
  (when (within-bounds-p x y)
    (aref *world-contents* x y)))

(defun nearby (entity &optional (radius 1))
  (remove entity
          (iterate (with x = (coords/x entity))
                   (with y = (coords/y entity))
                   (for (dx dy) :within-radius radius)
                   (appending (coords-lookup (+ x dx)
                                             (+ y dy))))))

(defmethod entity-created :after ((entity coords))
  (coords-insert-entity entity))

(defmethod entity-destroyed :after ((entity coords))
  (coords-remove-entity entity))