|
@@ -1,6 +1,6 @@
|
|
|
<!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>Custom Controls</title><link rel="stylesheet" href="./asciidoctor.css">
|
|
|
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.5.0/css/font-awesome.min.css">
|
|
|
-<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/custom_controls.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/"><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>Custom Controls</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="#usage">Usage</a></li><li><a href="#example-code">Example Code</a></li><li><a href="#abstractcontrol-class">AbstractControl Class</a></li><li><a href="#the-control-interface">The Control Interface</a></li></ul></div></div><div id="content"><div id="preamble"><div class="sectionbody"><div class="paragraph"><p>A <code>com.jme3.scene.control.Control</code> is a customizable jME3 interface that allows you to cleanly steer the behaviour of game entities (Spatials), such as artificially intelligent behaviour in NPCs, traps, automatic alarms and doors, animals and pets, self-steering vehicles or platforms – anything that moves and interacts. Several instances of custom Controls together implement the behaviours of a type of Spatial.</p></div>
|
|
|
+<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/custom_controls.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/"><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>Custom Controls</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="#usage">Usage</a></li><li><a href="#example-code">Example Code</a></li><li><a href="#abstractcontrol-class">AbstractControl Class</a></li><li><a href="#the-control-interface">The Control Interface</a></li><li><a href="#best-practices">Best Practices</a></li></ul></div></div><div id="content"><div id="preamble"><div class="sectionbody"><div class="paragraph"><p>A <code>com.jme3.scene.control.Control</code> is a customizable jME3 interface that allows you to cleanly steer the behaviour of game entities (Spatials), such as artificially intelligent behaviour in NPCs, traps, automatic alarms and doors, animals and pets, self-steering vehicles or platforms – anything that moves and interacts. Several instances of custom Controls together implement the behaviours of a type of Spatial.</p></div>
|
|
|
<div class="paragraph"><p>To control global game behaviour see <a href="../../jme3/advanced/application_states.html">Application States</a> – you often use AppStates and Control together.</p></div>
|
|
|
<div class="ulist"><ul><li><p><a href="http://www.youtube.com/watch?v=MNDiZ9YHIpM">Quick video introduction to Custom Controls</a></p></li></ul></div>
|
|
|
<div class="paragraph"><p>To control the behaviour of spatials:</p></div>
|
|
@@ -159,7 +159,38 @@ The AssetManager also uses this method if the same spatial is loaded twice. You
|
|
|
spatial = (Spatial) ic.readSavable(<span class="string"><span class="delimiter">"</span><span class="content">spatial</span><span class="delimiter">"</span></span>, <span class="predefined-constant">null</span>);<span class="error"> </span><span class="error"> </span><span class="error"> </span><span class="error"> </span><span class="error"> </span><span class="error"> </span><span class="error"> </span>
|
|
|
<span class="comment">// read custom variables .... </span>
|
|
|
}
|
|
|
-}</code></pre></div></div></li></ol></div></div></div></div><div id="footer"><div id="footer-text">Version <br>Last updated 2017-06-22 22:35:32 UTC</div></div><script type="text/javascript" src="https://cdn.jsdelivr.net/docsearch.js/2/docsearch.min.js"></script><script>docsearch({
|
|
|
+}</code></pre></div></div></li></ol></div></div></div>
|
|
|
+<div class="sect1"><h2 id="best-practices">Best Practices</h2><div class="sectionbody"><div class="paragraph"><p>Use the getControl() accessor to get Control objects from Spatials. No need to pass around lots of object references.Here an example from the <a href="http://code.google.com/p/monkeyzone/">MonkeyZone</a> code:</p></div>
|
|
|
+<div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java"><span class="directive">public</span> <span class="type">class</span> <span class="class">CharacterAnimControl</span> <span class="directive">implements</span> <span class="predefined-type">Control</span> {<span class="error"> </span>
|
|
|
+ ...<span class="error"> </span>
|
|
|
+ <span class="directive">public</span> <span class="type">void</span> setSpatial(Spatial spatial) {<span class="error"> </span><span class="error"> </span><span class="error"> </span>
|
|
|
+ ...<span class="error"> </span><span class="error"> </span><span class="error"> </span>
|
|
|
+ animControl<span class="error"> </span><span class="error"> </span><span class="error"> </span><span class="error"> </span><span class="error"> </span> = spatial.getControl(AnimControl.class);<span class="error"> </span><span class="error"> </span><span class="error"> </span>
|
|
|
+ characterControl = spatial.getControl(CharacterControl.class);<span class="error"> </span><span class="error"> </span><span class="error"> </span>
|
|
|
+ ...<span class="error"> </span>
|
|
|
+ }
|
|
|
+}</code></pre></div></div>
|
|
|
+<div class="paragraph"><p>You can create custom Control interfaces so a set of different Controls provide the same methods and can be accessed with the interface class type.</p></div>
|
|
|
+<div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java"><span class="directive">public</span> <span class="type">interface</span> <span class="class">ManualControl</span> <span class="directive">extends</span> <span class="predefined-type">Control</span> {<span class="error"> </span><span class="error"> </span><span class="error"> </span>
|
|
|
+ <span class="directive">public</span> <span class="type">void</span> steerX(<span class="type">float</span> value);<span class="error"> </span><span class="error"> </span><span class="error"> </span>
|
|
|
+ <span class="directive">public</span> <span class="type">void</span> steerY(<span class="type">float</span> value);<span class="error"> </span><span class="error"> </span><span class="error"> </span>
|
|
|
+ <span class="directive">public</span> <span class="type">void</span> moveX(<span class="type">float</span> value);<span class="error"> </span><span class="error"> </span><span class="error"> </span>
|
|
|
+ <span class="directive">public</span> <span class="type">void</span> moveY(<span class="type">float</span> value);<span class="error"> </span><span class="error"> </span><span class="error"> </span>
|
|
|
+ <span class="directive">public</span> <span class="type">void</span> moveZ(<span class="type">float</span> value);<span class="error"> </span><span class="error"> </span>
|
|
|
+ ...
|
|
|
+}</code></pre></div></div>
|
|
|
+<div class="paragraph"><p>Then you create custom sub-Controls and implement the methods accordingly to the context:</p></div>
|
|
|
+<div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java"><span class="directive">public</span> <span class="type">class</span> <span class="class">ManualVehicleControl</span><span class="error"> </span><span class="error"> </span> <span class="directive">extends</span> ManualControl {...}</code></pre></div></div>
|
|
|
+<div class="paragraph"><p>and</p></div>
|
|
|
+<div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java"><span class="directive">public</span> <span class="type">class</span> <span class="class">ManualCharacterControl</span> <span class="directive">extends</span> ManualControl {...}</code></pre></div></div>
|
|
|
+<div class="paragraph"><p>Then add the appropriate controls to spatials:</p></div>
|
|
|
+<div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java">characterSpatial.addControl(<span class="keyword">new</span> ManualCharacterControl());
|
|
|
+...
|
|
|
+vehicleSpatial.addControl(<span class="keyword">new</span> ManualVehicleControl());
|
|
|
+...</code></pre></div></div>
|
|
|
+<div class="admonitionblock tip"><table><tr><td class="icon"><i class="fa icon-tip" title="Tip"></i></td><td class="content"><div class="paragraph"><p>Use the getControl() method on a Spatial to get a specific Control object, and activate its behaviour!</p></div>
|
|
|
+<div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java">ManualControl c = mySpatial.getControl(ManualControl.class);
|
|
|
+c.steerX(steerX);</code></pre></div></div></td></tr></table></div></div></div></div><div id="footer"><div id="footer-text">Version <br>Last updated 2017-06-22 22:47:06 UTC</div></div><script type="text/javascript" src="https://cdn.jsdelivr.net/docsearch.js/2/docsearch.min.js"></script><script>docsearch({
|
|
|
apiKey: 'a736b6d93de805e26ec2f49b55013fbd',
|
|
|
indexName: 'jmonkeyengine',
|
|
|
inputSelector: '#doc-search',
|