From b31446c0f1ffb17cf4bff6f169a766d0e048e94e Mon Sep 17 00:00:00 2001
From: Jaidyn Ann <10477760+JadedCtrl@users.noreply.github.com>
Date: Fri, 16 Jun 2023 14:28:41 -0500
Subject: [PATCH] Interact with nearby entities
---
flora-search-aurora.lisp | 6 +++---
overworld.lisp | 35 +++++++++++++++++++++++++++++++----
res/map.tmx | 3 +--
3 files changed, 35 insertions(+), 9 deletions(-)
diff --git a/flora-search-aurora.lisp b/flora-search-aurora.lisp
index 2a2ca1a..c122c73 100644
--- a/flora-search-aurora.lisp
+++ b/flora-search-aurora.lisp
@@ -34,9 +34,9 @@
(in-package :flora-search-aurora)
-(defun talk (map)
- (print (gethash :mom map))
- (setf (gethash :mom map) 3434))
+(defun literary-girl-dialogue (map)
+ (print "uwu"))
+
diff --git a/overworld.lisp b/overworld.lisp
index 7dcb39b..8d5d4e4 100644
--- a/overworld.lisp
+++ b/overworld.lisp
@@ -65,11 +65,14 @@ Returns parameters to be used in the next invocation of OVERWORLD-STATE."
(if (listen)
(let* ((input (normalize-char-plist (read-char-plist))))
(cond
+ ;; Interacting with nearby characters/entities
((plist= input '(:modifier nil :char #\return))
- (let ((interact (getf (cdr (assoc 'player (gethash :entities map)))
- :interact)))
- (if interact
- (apply (intern interact) (list map)))))
+ (let* ((player (assoc 'player (gethash :entities map)))
+ (interactee (car (entities-near-entity player (gethash :entities map))))
+ (interaction (getf (cdr interactee) :interact)))
+ (if interaction
+ (apply (intern (string-upcase interaction)) (list map)))))
+ ;; Simple up-down-left-right movements
((plist= input '(:modifier nil :char #\→))
(move-entity map 'player :x 1))
((plist= input '(:modifier nil :char #\←))
@@ -187,6 +190,30 @@ alist containing a character (:CHAR) and :X & :Y coordinates."
;;; ———————————————————————————————————
;;; Misc. utility
;;; ———————————————————————————————————
+(defun entities-near-coords (coords radius entities &key (x-radius radius) (y-radius radius))
+ (remove-if-not
+ (lambda (test-entity)
+ (let ((test-coords (getf (cdr test-entity) :coords)))
+ (and (< (abs (- (getf coords :x)
+ (getf test-coords :x)))
+ x-radius)
+ (< (abs (- (getf coords :y)
+ (getf test-coords :y)))
+ y-radius))))
+ entities))
+
+
+(defun entities-near-entity (entity entities)
+ (remove-if
+ (lambda (test-entity)
+ (plist= (cdr entity)
+ (cdr test-entity)))
+ (entities-near-coords (getf (cdr entity) :coords)
+ (+ (length (getf (cdr entity) :face)) 2)
+ entities
+ :y-radius 2)))
+
+
(defun every-other-element (list)
"Collect every-other-element of a list. E.g., (1 2 3 4) → (1 3)."
(when list
diff --git a/res/map.tmx b/res/map.tmx
index d894a90..5044a82 100644
--- a/res/map.tmx
+++ b/res/map.tmx
@@ -101,7 +101,6 @@
-
@@ -110,7 +109,7 @@
-
+