(attach-actor-to-world) method for proper initialization (for #9)

This commit is contained in:
~keith 2022-02-22 03:17:17 +00:00
parent cd11c4b53b
commit 0f8fe16fc3
Signed by: keith
GPG Key ID: 5BEBEEAB2C73D520
2 changed files with 30 additions and 5 deletions

View File

@ -15,6 +15,7 @@
(scene :documentation "The scene containing this actor." (scene :documentation "The scene containing this actor."
:reader scene :reader scene
:type pointer :type pointer
:initarg :scene
:initform nil) :initform nil)
(tags :documentation "This actor's tags." (tags :documentation "This actor's tags."
:reader tags :reader tags
@ -87,6 +88,12 @@
"Whether or not this actor and all its parents are active." "Whether or not this actor and all its parents are active."
(and [this active-p] (not [this :slot blocked-p]))) (and [this active-p] (not [this :slot blocked-p])))
(defmethod apply-to-tree ((this actor) fun)
(funcall fun this)
(loop for child-ptr in [this children]
when (typep child-ptr 'weak-pointer)
do [(weak-pointer-value child-ptr) (apply-to-tree fun)]))
(defmethod print-object ((this actor) stream) (defmethod print-object ((this actor) stream)
(print-unreadable-object (this stream :type t :identity t) (print-unreadable-object (this stream :type t :identity t)
(prin1 [this :slot id] stream) (prin1 [this :slot id] stream)

View File

@ -98,6 +98,20 @@
"Get a scene by its ID." "Get a scene by its ID."
(find-if (lambda (scene) (eq [scene id] scene-id)) *world-scenes*)) (find-if (lambda (scene) (eq [scene id] scene-id)) *world-scenes*))
(defun attach-actor-to-world (actor scene)
"Properly attach actor and its descendents to scene, and initialize them."
;; attach actors to scene
(apply-to-tree actor (lambda (a)
(setf [a :slot scene] nil)
[scene (add-actor a)]))
;; (resume) -> automatically resumes children
[actor (resume)]
;; FIXME make (activate) call itself recursively on children
(apply-to-tree actor (lambda (a)
(when [a tree-active-p]
[a (activate)])))
)
(defvar *view-width* 384 (defvar *view-width* 384
"View-space width in pixels.") "View-space width in pixels.")
(defvar *view-height* 256 (defvar *view-height* 256
@ -122,7 +136,7 @@
(setf test-actor (make-instance 'actor (setf test-actor (make-instance 'actor
:name "Actor")) :name "Actor"))
[test-scene (add-actor test-actor)] ;; [test-scene (add-actor test-actor)]
(setf test-drawable (make-instance 'drawable-test)) (setf test-drawable (make-instance 'drawable-test))
[test-actor (add-component test-drawable)] [test-actor (add-component test-drawable)]
@ -132,7 +146,7 @@
:location (vec2 0.5 0.5) :location (vec2 0.5 0.5)
:rotation (coerce (/ pi 4) 'single-float) :rotation (coerce (/ pi 4) 'single-float)
:z-layer -1)) :z-layer -1))
[test-scene (add-actor test-actor-2)] ;; [test-scene (add-actor test-actor-2)]
[test-actor-2 (add-component (make-instance 'drawable-test [test-actor-2 (add-component (make-instance 'drawable-test
:colour (vec4 0.0 1.0 0.0 1.0))) :colour (vec4 0.0 1.0 0.0 1.0)))
@ -142,7 +156,7 @@
:name "Child Actor" :name "Child Actor"
:location (vec2 0 0.5) :location (vec2 0 0.5)
:z-layer -2)) :z-layer -2))
[test-scene (add-actor child-actor)] ;; [test-scene (add-actor child-actor)]
[test-actor-2 (add-child child-actor)] [test-actor-2 (add-child child-actor)]
[child-actor (add-component (make-instance 'drawable-test [child-actor (add-component (make-instance 'drawable-test
@ -154,7 +168,7 @@
:location (vec2 0 1) :location (vec2 0 1)
:scale (vec2 0.25 0.25) :scale (vec2 0.25 0.25)
:z-layer 1)) :z-layer 1))
[test-scene (add-actor grandchild-actor)] ;; [test-scene (add-actor grandchild-actor)]
[child-actor (add-child grandchild-actor)] [child-actor (add-child grandchild-actor)]
[grandchild-actor (add-component (make-instance 'drawable-test [grandchild-actor (add-component (make-instance 'drawable-test
@ -163,11 +177,15 @@
(setf camera-actor (make-instance 'actor (setf camera-actor (make-instance 'actor
:name "Camera")) :name "Camera"))
[test-scene (add-actor camera-actor)] ;; [test-scene (add-actor camera-actor)]
(setf camera-view (make-instance 'view)) (setf camera-view (make-instance 'view))
[camera-actor (add-component camera-view)] [camera-actor (add-component camera-view)]
(attach-actor-to-world test-actor test-scene)
(attach-actor-to-world test-actor-2 test-scene)
(attach-actor-to-world camera-actor test-scene)
test-scene)) test-scene))
(defun run () (defun run ()