Fix bug with setting an entity in entity-map

Now it does not break when entity and entity-id have different id's
This commit is contained in:
TakeV 2024-05-27 00:31:18 -04:00
parent bc2e3064d9
commit 3729fcac49
Signed by: TakeV
GPG key ID: A64F41345C7400AF
2 changed files with 33 additions and 5 deletions

View file

@ -97,13 +97,20 @@ it just checks if the value is an entity."
entity-map)))
(define (set-entity! entity-map entity-id entity)
"Replaces the entity in the entity-map the given entity."
"Replaces the entity in the entity-map the given entity.
If the entity's game-entity-id does not match entity-id, then
a copy of entity will be created with entity-id and that will be
stored."
(if (and (entity-map? entity-map)
(game-entity? entity))
(begin
(let ((to-store
(if (eqv? entity-id (game-entity-id entity))
entity
(create-entity entity-id
(get-entity-components entity)))))
(hashtable-set! (entity-map-entities entity-map)
entity-id
entity))
to-store))
(error "Invalid game entity and entity-map" entity-map entity)))
(define (delete-entity! entity-map entity)
@ -136,7 +143,7 @@ Returns the updated entity, or false if the entity does not exist."
(if (entity-map? entity-map)
(let ((old-map (entity-map-entities entity-map)))
(make-entity-map (hashtable-copy old-map)))
#f))
(error "Invalid input for entity-map:copy-entity-map" entity-map)))
(define (update-entities! entity-map updated-entities-map)
"Updates entity-map using entities. entities can be a list, vector, or entity-map.

View file

@ -54,12 +54,13 @@
(test-assert (entity-map? hashtable-constructed))
(test-assert (entity-map? emap-constructed))
;; Use the same map for the tests
;; Test getting entity
(let ((test-em list-constructed))
(test-assert (game-entity? (get-entity test-em empty-id)))
(test-eqv (get-entity test-em empty-id) empty-entity)
(test-eqv (get-entity test-em empty-entity) empty-entity))
;; Test getting entities when filtered
(let* ((all-entities (get-entities list-constructed))
(only-zero-proc (lambda (x) (= 0 (game-entity-id x))))
(zero-pass-entities (get-entities list-constructed
@ -69,6 +70,26 @@
(test-assert (member empty-entity zero-pass-entities))
(test-assert (not (member next-entity zero-pass-entities))))
;; Test copy entity-map
(let* ((emap-original list-constructed)
(emap-copy (copy-entity-map emap-original))
(third-entity (entity-generator))
(third-entity-id (game-entity-id third-entity)))
(test-assert (entity-map? emap-copy))
(test-assert (not (eq? emap-copy emap-original)))
;; Mutation tests
(set-entity! emap-copy third-entity-id third-entity)
(test-eqv 'notfound (get-entity emap-original third-entity #:default 'notfound))
(test-eqv third-entity (get-entity emap-copy third-entity-id))
;; Making sure that the entity-id's match
(let* ((funky-entity (entity-generator))
(funky-entity-id (game-entity-id funky-entity))
(correct-id (+ 100 funky-entity-id)))
(set-entity! emap-copy correct-id funky-entity)
(test-eqv correct-id (game-entity-id (get-entity emap-copy correct-id)))))
(test-end)
#t