About Monkey 2 › Forums › Monkey 2 Programming Help › Minimal Scenegraph
This topic contains 0 replies, has 1 voice, and was last updated by
cocon 1 year, 11 months ago.
-
AuthorPosts
-
May 12, 2017 at 10:28 am #8174
Just to make some better organization of the game scenes, I had a look at the Composite pattern. This is a tree pattern (used in scenegraphs) but instead of making distinctions between branches and leafs, I would simply go for one datatype which is a Component that has other components.
Now the next step is that I wanted to have more reusable code, instead the Components be the strong datatype (Component>Player, Component>Enemy) I would care more about how the tree works, rather than specify class system-wide datatypes. The result was that I made the component a generic class.
The result is something like this for now, I post this just in case anyone is interested to know, but later on I will try to make more corrections to it. If you want to make any suggestions or having ideas you are welcomed.
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121#Import "<std>"Using std..Class Component<T>PrivateField name:StringPublicProperty Name:String()Return nameEndProtectedField children := New List<Component>PublicProperty HasChildren:Bool()Return children.Count() > 0EndProperty Children:List<Component>()Return childrenEndPublicField DataType:TPublicMethod New(name:String)Self.name = nameSelf.DataType = New TEndPublicField OnUpdate:Void()Field OnUpdateDatatype:Void(c:T)PublicMethod Add(c:Component)children.Add(c)EndMethod Remove(c:Component)children.Remove(c)EndEndClass ComponentFunctions AbstractFunction RepeatString:String(s:String, times:Int)Local out := ""For Local i := 0 Until timesout += sNextReturn outEndPublicFunction PrintTree<T>(component:Component<T>, depth:Int = 0)Print(RepeatString("-", depth * 4) + component.Name)If component.HasChildren > 0depth += 1For Local i := Eachin component.ChildrenComponentFunctions.PrintTree(i, depth)Nextdepth -= 1EndEndFunction UpdateTree<T>(component:Component<T>)If component.OnUpdate <> Null Then component.OnUpdate()If component.OnUpdateDatatype <> Null And component.DataType <> Nullcomponent.OnUpdateDatatype(component.DataType)EndIf component.HasChildren > 0For Local i := Eachin component.ChildrenComponentFunctions.UpdateTree(i)NextEndEndEndClass DummyClassField Data:IntEndFunction Main()' construct the objectsLocal w := New Component<DummyClass>("World")Local b := New Component<DummyClass>("Body")Local h := New Component<DummyClass>("Head")Local e := New Component<DummyClass>("Eyes")Local m := New Component<DummyClass>("Mouth")' add object relationsw.Add(b) ' add body to worldb.Add(h) ' add head to bodyh.Add(e) ' add eyes to headh.Add(m) ' add mouth to head' add some updating functionsw.OnUpdate = Lambda()Print("updating world")Endb.OnUpdate = Lambda()Print("updating body")End' add some updating functions to operate upon the datatypeh.OnUpdateDatatype = Lambda(c:DummyClass)c.Data += 1Print("~t~t(data of the head " + c.Data + ")~n")End' testComponentFunctions.PrintTree(w)Print("")For Local i := 0 To 5Print(Millisecs())ComponentFunctions.UpdateTree(w)Sleep(1)NextEnd -
AuthorPosts
You must be logged in to reply to this topic.