structure.html 9.4 KB

12345678910111213141516171819202122232425262728
  1. <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><!--[if IE]><meta http-equiv="X-UA-Compatible" content="IE=edge"><![endif]--><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="Asciidoctor 1.5.4"><title>structure</title><link rel="stylesheet" href="./asciidoctor.css">
  2. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.5.0/css/font-awesome.min.css">
  3. <link rel="stylesheet" href="./coderay-asciidoctor.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/docsearch.js/2/docsearch.min.css"></head><body class="article toc2 toc-left"><div id="header"><div id="toolbar"><a href="https://github.com/jMonkeyEngine/wiki/edit/master/src/docs/asciidoc/jme3/advanced/atom_framework/docs/code/structure.adoc"><i class="fa fa-pencil-square" aria-hidden="true"></i></a><a href="https://github.com/jMonkeyEngine/wiki/new/master/src/docs/asciidoc/jme3/advanced/atom_framework/docs/code/"><i class="fa fa-plus-square" aria-hidden="true"></i></a><input dir="auto" style="position: relative; vertical-align: top;" spellcheck="false" autocomplete="off" class="searchbox__input aa-input" id="doc-search" name="search" placeholder="Search in the doc" required="required" type="search"></div><h1>structure</h1><div class="details"><span class="author" id="author"></span><br><span id="revnumber">version ,</span> <span id="revdate">2016/03/17 20:48</span></div><div id="toc" class="toc2"><div id="toctitle">Table of Contents</div><ul class="sectlevel1"><li><a href="#how-can-i-structure-my-code">How can I structure my code?</a><ul class="sectlevel2"><li><a href="#the-question">The question</a></li><li><a href="#the-pattern">The pattern</a></li><li><a href="#the-central">The central</a></li><li><a href="#the-structure">The structure</a></li></ul></li></ul></div></div><div id="content"><div class="sect2"><h3 id="how-can-i-structure-my-code">How can I structure my code?</h3><div class="sect2"><h3 id="the-question">The question</h3><div class="literalblock"><div class="content"><pre>How can I structure my code?</pre></div></div>
  4. <div class="paragraph"><p><a href="http://hub.jmonkeyengine.org/forum/topic/how-to-structure-the-game-code/">http://hub.jmonkeyengine.org/forum/topic/how-to-structure-the-game-code/</a></p></div></div>
  5. <div class="sect2"><h3 id="the-pattern">The pattern</h3><div class="paragraph"><p>Read the explain about some design patterns in Design <a href="../../../../../jme3/advanced/atom_framework/design.html">design</a></p></div>
  6. <div class="paragraph"><p>This queston evolve several patterns like Singleton, Manager, Factory and related directly to the Central idiom of Atom&#8217;s framework!</p></div></div>
  7. <div class="sect2"><h3 id="the-central">The central</h3><div class="paragraph"><p>Now talking about the long “getManager().getSubManager().getItsManagedObject().doSomeThing()”
  8. Consider asking your self this when you code:</p></div>
  9. <div class="olist arabic"><ol class="arabic"><li><p>Can singleton help?</p></li><li><p>Can hierarchy actually help?</p></li><li><p>How secure is your application, is it a game or anything else?</p></li><li><p>How perfomance sensitive your game is?</p></li></ol></div>
  10. <div class="paragraph"><p>For this particular example, related to “Law of Demeter” that @zanval88 mentioned is about “communicating range scope” and “hiding information”, appear as “encapsulation” and “reference” in Java.</p></div>
  11. <div class="olist arabic"><ol class="arabic"><li><p>If all your Manager classes is public, and referenced every where chances that they can be Singleton (read more about singleton for its downside).</p></li><li><p>If not, you should nest your Manager and its ManagedObject it arbitrary order and way. Consider this for a game, it’s almost always not necessary!</p><div class="olist loweralpha"><ol class="loweralpha" type="a"><li><p>When you have your Managers and Object form a “tree” and obey a “cycle” or “protocol” or “common pattern”, may be contract them with an Interface or force them to extend the same base. [I DID this in my Atom framework!]</p></li><li><p>Actually while a lower (near the core) Manager are more powerful and can capture the whole process, the higher level Manager (extended Manager or sub manager) know better about detail and specific aspects. The split should be carefully thought first to avoid un necessary burden of too much Managers. Personally, I usually think about an interesting example about a society with many cops and few citizens to imagine about this balance.</p></li></ol></div></li><li><p>For part of the game, when security are most important but not the performance, or for external services. Long indirect call is the most appropriate method, because of the availability (something should be injected, lazy loaded…) and security (some protol given, real info is hidden from our view,…etc). Without a check of availability, chance that you have NPE all the time!</p></li><li><p>For part of the game that need performance. I said “a big deal of performance” you should try “singleton” for final class (and bare with its other issuses) or bet in some other flatten type of reference (little bit worse performance – one level of indirect) should also be concerned:</p><div class="olist loweralpha"><ol class="loweralpha" type="a"><li><p>array or list with an index. map via Class(Type) apear as Template in java …</p><div class="olist lowerroman"><ol class="lowerroman" type="i"><li><p>ex: StateManager.getState(ClassName.class).doSomething();</p></li><li><p>or even more abstract somekind of Central.get(“StateManager”).do(“ItsFunctionName”)!!! I do this in groovy and for Java 8 dynamic invoking</p></li></ol></div></li><li><p>evolve your hierarchy with EntitySystem paradigm</p></li></ol></div></li></ol></div></div>
  12. <div class="sect2"><h3 id="the-structure">The structure</h3><div class="paragraph"><p>I have a strong conceptual POV about video game, which affected by cinematography a lot. Because English is not my native language I can misunderstood the real meaning of the noun but I’ve tried to find the right words in decade.</p></div>
  13. <div class="paragraph"><p>This one is mine, maybe only me but noone else :p :</p></div>
  14. <div class="paragraph"><p>So consider this 5 level of separation:</p></div>
  15. <div class="olist arabic"><ol class="arabic"><li><p>Main : The main entry, have everything only relate to this single game, single application. Also game specific Configs should be here</p></li><li><p>Core : The shared part can be used in almost every application share the same base</p></li><li><p>Stage : The ‘Stage’ is the base of entities, activites and events… It’s not nessesary care about the gameplay but the World, Camera, Light and Effects, Cinematic. Stage contain most of the underlying logic, and the actors.</p></li><li><p>GamePlay: The part care about the routine of the game, the player, characters, stories, items, gameactions, techtree… it’s make the game look more like a game than an normal software or a movie. Gameplay contain most of the interactive part.</p></li><li><p>State : Even your game routine can be modeled by something else not States, I introduced State to be more friendly with JME3′s AppState concept. I ultilized it and leveraged it, and you should also.</p></li></ol></div>
  16. <div class="paragraph"><p>Others optional:</p></div>
  17. <div class="olist arabic"><ol class="arabic"><li><p>*Network : For network game, blending between state, sub-routine and arbitrary events is difficult and may require other kind of paradigm, that’s why is not in Stage, but elsewhere outside.</p></li><li><p>*Services: If your game use external services such as Web or IAP or something like that.</p></li><li><p>*UI: UI stand for user interface, almost everygame have user interfaces but not all, so it’s also optional</p></li><li><p>*Script: For scripting, in my application, I embed Groovy everywhere, but I also preseved a place to hold “tranditional” run-time script that only be awared and executed when the game is already running.</p></li><li><p>*Model: If you are in a company or from a strong “standard lized” Java workflow, it’s nearly you’ll come up with some Bean like this, but it’s kind of for normal software not a game.</p></li><li><p>*Generated: Also if you have to embed some XML or some generated sources</p></li><li><p>*DB: Of course Database and persistent solution can be here( if not better be in the Service section)</p></li></ol></div>
  18. <div class="paragraph"><p>…</p></div>
  19. <div class="paragraph"><p>The directory :
  20. * src</p></div>
  21. <div class="ulist"><ul><li><p>my.game.name</p><div class="ulist"><ul><li><p>main</p></li><li><p>core</p></li><li><p>state</p></li><li><p>gameplay</p></li><li><p>(*)network</p></li><li><p>(*)services</p></li><li><p>(*)ui</p></li><li><p>(*)others</p></li></ul></div></li></ul></div>
  22. <div class="paragraph"><p>More detailed, You can find a better example here in my game examples:
  23. <a href="../../../../../jme3/atomixtuts.html">atomixtuts</a></p></div></div></div></div><div id="footer"><div id="footer-text">Version <br>Last updated 2016-07-10 15:00:09 UTC</div></div><script type="text/javascript" src="https://cdn.jsdelivr.net/docsearch.js/2/docsearch.min.js"></script><script>docsearch({
  24. apiKey: 'a736b6d93de805e26ec2f49b55013fbd',
  25. indexName: 'jmonkeyengine',
  26. inputSelector: '#doc-search',
  27. debug: false // Set debug to true if you want to inspect the dropdown
  28. });</script></body></html>