java - The composite pattern/entity system and traditional OOP -


i'm working on small game written in java (but question language-agnostic). since wanted explore various design patterns, got hung on composite pattern/entity system (which read here , here) alternative typical deep hierarchical inheritance.

now, after writing several thousand lines of code, i'm bit confused. think understand pattern , enjoy using it. think it's cool , starbucks-ish, feels benefit provides short-lived , (what irks me most) heavily dependent on granularity.

here's picture second article above: enter image description here

i love way objects (game entities, or whatever want call them) have minimal set of components , inferred idea write code looks like:

baseentity alien = new baseentity(); baseentity player = new baseentity();  alien.addcomponent(new position(), new movement(), new render(), new script(), new target()); player.addcomponent(new position(), new movement(), new render(), new script(), new physics()); 

.. nice... in reality, code ends looking like

baseentity alien = new baseentity(); baseentity player = new baseentity();  alien.addcomponent(new position(), new alienaimovement(), new renderalien(), new scriptalien(), new target()); player.addcomponent(new position(), new keyboardinputmovement(), new renderplayer(), new scriptplayer(), new physicsplayer()); 

it seems end having specialized components made of lesser components. times, have make components have dependencies of other components. after all, how can render if have no position? not that, way end rendering player vs. alien vs. grenade can fundamentally different. can't have 1 component dictates three, unless make big component (in case... why using composite pattern anyway?)

to give real-life example. have characters in game can equip various gear. when piece of gear equipped, statistics changed what's displayed visually. here's code looks right now:

billy.addcontrollers(new movement(), new position(), new characteranimationrender(), new keyboardcharacterinput());  billy.get(characteranimationrender.class).setbody(body.normal_body); billy.get(characteranimationrender.class).setface(face.blush_face); billy.get(characteranimationrender.class).sethair(hair.red_hair); billy.get(characteranimationrender.class).setdress(dress.dragon_plate_armor); 

the above characteranimationrender.class affects what's displayed visually. need make component handles gear stats. however, why like:

billy.addcontrollers(new characterstatistics());  billy.get(characteranimationrender.class).setbody(body.normal_body); billy.get(characterstatistics.class).setbodystats(body_stats.normal_body); 

when can make charactergearstuff controller/component handles both distribution of stats visual change?

bottom line, i'm not sure how supposed productivity since unless want handle manually, still have create "meta-components" depend on 2+ components (and modify/cross-modify of sub-components - bringing oop). or maybe i'm thinking wrong. i?

it sounds you've misunderstood component pattern.

components data only, no code. if have code in component, it's not component more - it's more complex.

so, instance, should able trivially share characteranimationrender , characterstatistics, e.g.:

characterstats { int body } charactergamestats { ...not sure data have affects gameplay, not rendering... } charactervisualdetails { int face, int hair } 

...but there no need these aware of existence of each other. moment talk "dependencies" between components, suspet you've got lost. how can 1 struct of ints "depend upon" struct of ints? can't. they're chunks of data.

...

going concerns @ start, end with:

alien.addcomponent(new position(), new alienaimovement(), new renderalien(), new scriptalien(), new target()); player.addcomponent(new position(), new keyboardinputmovement(), new renderplayer(), new scriptplayer(), new physicsplayer()); 

...that's perfect. assuming you've written components correctly, you've split out data small chunks easy read/debug/edit/code against.

however, making guesses because haven't specified what's inside components ... e.g. alienaimovement - what's in that? normally, i'd expect have "aimovement()", , edit make alien's version, e.g. change internal flags in component indiate it's using "alien" functions in ai system.


Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -