Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
eshamster committed Apr 16, 2017
2 parents 722b80a + fa6dfdf commit c0d2001
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/cl-ps-ecs.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,6 @@

:do-ecs-components-of-entity
:register-ecs-system
:register-next-frame-func
:ecs-main))
(in-package :cl-ps-ecs)
30 changes: 18 additions & 12 deletions src/ecs.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

:do-ecs-components-of-entity
:register-ecs-system
:register-next-frame-func
:ecs-main
:clean-ecs-env)
(:import-from :alexandria
Expand All @@ -67,8 +68,6 @@
)

(defvar.ps+ *entity-list* '())
(defvar.ps+ *entity-list-buffer* '()
"The list of (entity parent) pairs. This is added to by add-ecs-entity-to-buffer.")

(defmacro.ps+ do-ecs-entity-tree-list ((var entity-tree-list) &body body)
(with-gensyms (rec entity-list)
Expand Down Expand Up @@ -189,8 +188,21 @@
(,(cadr var) (cadr ,pair)))
,@body)))))

(defvar.ps+ *next-frame-func-list* '())

(defun.ps+ register-next-frame-func (func)
"Register a function with no argument that is executed in first of next frame.
Note: Some functions (Ex. add or delete resource) can cause troublesome problems if it is executed in frame. Use this wrapper when executing such functions."
(push func *next-frame-func-list*))

(defun.ps+ execute-all-registered-funcs ()
;; Reverse to execute functions in order of registration.
(dolist (func (reverse *next-frame-func-list*))
(funcall func))
(setf *next-frame-func-list* '()))

(defun.ps+ ecs-main ()
(flush-ecs-entities-buffer)
(execute-all-registered-funcs)
(do-ecs-systems system
(when (ecs-system-enable system)
(funcall (ecs-system-process-all system) system)
Expand Down Expand Up @@ -220,6 +232,7 @@
;; entity component system

(defun.ps+ clean-ecs-env ()
(setf *next-frame-func-list* '())
(clean-ecs-entities)
(clean-ecs-systems))

Expand Down Expand Up @@ -267,15 +280,8 @@

(defun.ps+ add-ecs-entity-to-buffer (entity &optional (parent nil))
"Add the entity to the buffer of the global list. They are added in the loop, ecs-main. This is useful for adding entities in do-ecs-entities loop."
;;--- TODO: Error check for arguments. (Lately these are checked in add-ecs-entity).
(push (list entity parent) *entity-list-buffer*))

(defun.ps+ flush-ecs-entities-buffer ()
"Add all entities in the buffer."
;; The 'reverse' is required to add entities in the order of addition. Otherwize, the relationships between a parent and its children cannot be properly processed.
(dolist (pair (reverse *entity-list-buffer*))
(add-ecs-entity (car pair) (cadr pair)))
(setf *entity-list-buffer* '()))
(register-next-frame-func
#'(lambda () (add-ecs-entity entity parent))))

(defun.ps+ delete-ecs-entity (entity)
"Remove an entity from global *entity-list* with its descendants."
Expand Down
19 changes: 18 additions & 1 deletion t/ecs.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
:is-list.ps+))
(in-package :cl-ps-ecs-test.ecs)

(plan 5)
(plan 6)

(declaim #+sbcl (sb-ext:muffle-conditions sb-ext:compiler-note))

Expand Down Expand Up @@ -491,6 +491,23 @@
(delete-ecs-entity ent-not-target)
(is *test-counter* 101))))))

(subtest
"Test register-next-frame-func"
(with-prove-in-both ()
(with-modify-env
(let ((counter 0))
;; Check if 2 functions are executed in order of registration
(register-next-frame-func #'(lambda () (incf counter)))
(register-next-frame-func #'(lambda () (setf counter (* 2 counter))))
(is counter 0)
(incf counter 10)
(is counter 10)
(ecs-main)
(is counter 22)
;; Check if registered functions are cleared after execution
(ecs-main)
(is counter 22)))))

(subtest
"Test do-ecs-components-of-entity"
(with-prove-in-both ()
Expand Down

0 comments on commit c0d2001

Please sign in to comment.