diff --git a/wh-engine/actor.lisp b/wh-engine/actor.lisp index 9354fb3..2dccec8 100644 --- a/wh-engine/actor.lisp +++ b/wh-engine/actor.lisp @@ -15,6 +15,7 @@ (scene :documentation "The scene containing this actor." :reader scene :type pointer + :initarg :scene :initform nil) (tags :documentation "This actor's tags." :reader tags @@ -87,6 +88,12 @@ "Whether or not this actor and all its parents are active." (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) (print-unreadable-object (this stream :type t :identity t) (prin1 [this :slot id] stream) diff --git a/wh-engine/main.lisp b/wh-engine/main.lisp index d8aaa90..bb45251 100644 --- a/wh-engine/main.lisp +++ b/wh-engine/main.lisp @@ -98,6 +98,20 @@ "Get a scene by its ID." (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 "View-space width in pixels.") (defvar *view-height* 256 @@ -122,7 +136,7 @@ (setf test-actor (make-instance 'actor :name "Actor")) - [test-scene (add-actor test-actor)] + ;; [test-scene (add-actor test-actor)] (setf test-drawable (make-instance 'drawable-test)) [test-actor (add-component test-drawable)] @@ -132,7 +146,7 @@ :location (vec2 0.5 0.5) :rotation (coerce (/ pi 4) 'single-float) :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 :colour (vec4 0.0 1.0 0.0 1.0))) @@ -142,7 +156,7 @@ :name "Child Actor" :location (vec2 0 0.5) :z-layer -2)) - [test-scene (add-actor child-actor)] + ;; [test-scene (add-actor child-actor)] [test-actor-2 (add-child child-actor)] [child-actor (add-component (make-instance 'drawable-test @@ -154,7 +168,7 @@ :location (vec2 0 1) :scale (vec2 0.25 0.25) :z-layer 1)) - [test-scene (add-actor grandchild-actor)] + ;; [test-scene (add-actor grandchild-actor)] [child-actor (add-child grandchild-actor)] [grandchild-actor (add-component (make-instance 'drawable-test @@ -163,11 +177,15 @@ (setf camera-actor (make-instance 'actor :name "Camera")) - [test-scene (add-actor camera-actor)] + ;; [test-scene (add-actor camera-actor)] (setf camera-view (make-instance '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)) (defun run ()