/usr/share/qt5/doc/qtquick/qtquick-demos-maroon-content-logic-js.html is in qtdeclarative5-doc-html 5.5.1-2ubuntu6.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 | <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>logic.js Example File | Qt Quick 5.5</title>
<link rel="stylesheet" type="text/css" href="style/offline.css" />
</head>
<body>
<div class="header" id="qtdocheader">
<div class="main">
<div class="main-rounded">
<div class="navigationbar">
<ul>
<li>Qt 5.5</li>
<li><a href="qtquick-index.html">Qt Quick</a></li>
<li><a href="qtquick-demos-maroon-example.html">Qt Quick Demo - Maroon in Trouble</a></li>
<li>logic.js Example File</li>
<li id="buildversion">Qt 5.5.1 Reference Documentation</li>
</ul>
</div>
</div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar"><div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">logic.js Example File</h1>
<span class="subtitle">demos/maroon/content/logic.js</span>
<!-- $$$demos/maroon/content/logic.js-description -->
<div class="descr"> <a name="details"></a>
<pre class="js"><span class="comment">/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of The Qt Company Ltd nor the names of its
** contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/</span>
.pragma library // Shared game state
.import QtQuick 2.0 as QQ
// Game Stuff
var <span class="name">gameState</span> <span class="comment">// Local reference</span>
<span class="keyword">function</span> <span class="name">getGameState</span>() { <span class="keyword">return</span> <span class="name">gameState</span>; }
var <span class="name">towerData</span> = [ <span class="comment">// Name and cost, stats are in the delegate per instance</span>
{ "name": <span class="string">"Melee"</span>, "cost": <span class="number">20</span> },
{ "name": <span class="string">"Ranged"</span>, "cost": <span class="number">50</span> },
{ "name": <span class="string">"Bomb"</span>, "cost": <span class="number">75</span> },
{ "name": <span class="string">"Factory"</span>, "cost": <span class="number">25</span> }
]
var <span class="name">waveBaseData</span> = [<span class="number">300</span>, <span class="number">290</span>, <span class="number">280</span>, <span class="number">270</span>, <span class="number">220</span>, <span class="number">180</span>, <span class="number">160</span>, <span class="number">80</span>, <span class="number">80</span>, <span class="number">80</span>, <span class="number">30</span>, <span class="number">30</span>, <span class="number">30</span>, <span class="number">30</span>];
var <span class="name">waveData</span> = [];
var <span class="name">towerComponents</span> = new <span class="name">Array</span>(<span class="name">towerData</span>.<span class="name">length</span>);
var <span class="name">mobComponent</span> = <span class="name">Qt</span>.<span class="name">createComponent</span>(<span class="string">"mobs/MobBase.qml"</span>);
<span class="keyword">function</span> <span class="name">endGame</span>()
{
<span class="name">gameState</span>.<span class="name">gameRunning</span> <span class="operator">=</span> <span class="number">false</span>;
<span class="name">gameState</span>.<span class="name">gameOver</span> <span class="operator">=</span> <span class="number">true</span>;
<span class="keyword">for</span> (<span class="keyword">var</span> <span class="name">i</span> = <span class="number">0</span>; <span class="name">i</span> <span class="operator"><</span> <span class="name">gameState</span>.<span class="name">cols</span>; i++) {
<span class="keyword">for</span> (<span class="keyword">var</span> <span class="name">j</span> = <span class="number">0</span>; <span class="name">j</span> <span class="operator"><</span> <span class="name">gameState</span>.<span class="name">rows</span>; j++) {
<span class="keyword">if</span> (<span class="name">gameState</span>.<span class="name">towers</span>[<span class="name">towerIdx</span>(<span class="name">i</span>, <span class="name">j</span>)]) {
<span class="name">gameState</span>.<span class="name">towers</span>[<span class="name">towerIdx</span>(<span class="name">i</span>, <span class="name">j</span>)].<span class="name">destroy</span>();
<span class="name">gameState</span>.<span class="name">towers</span>[<span class="name">towerIdx</span>(<span class="name">i</span>, <span class="name">j</span>)] <span class="operator">=</span> <span class="number">null</span>;
}
}
<span class="keyword">for</span> (<span class="keyword">var</span> <span class="name">j</span> in <span class="name">gameState</span>.<span class="name">mobs</span>[<span class="name">i</span>])
<span class="name">gameState</span>.<span class="name">mobs</span>[<span class="name">i</span>][<span class="name">j</span>].<span class="name">destroy</span>();
<span class="name">gameState</span>.<span class="name">mobs</span>[<span class="name">i</span>].<span class="name">splice</span>(<span class="number">0</span>,<span class="name">gameState</span>.<span class="name">mobs</span>[<span class="name">i</span>].<span class="name">length</span>); <span class="comment">//Leaves queue reusable</span>
}
}
<span class="keyword">function</span> <span class="name">startGame</span>(<span class="name">gameCanvas</span>)
{
<span class="name">waveData</span> <span class="operator">=</span> new <span class="name">Array</span>();
<span class="keyword">for</span> (<span class="keyword">var</span> <span class="name">i</span> in <span class="name">waveBaseData</span>)
<span class="name">waveData</span>[<span class="name">i</span>] <span class="operator">=</span> <span class="name">waveBaseData</span>[<span class="name">i</span>];
<span class="name">gameState</span>.<span class="name">freshState</span>();
<span class="keyword">for</span> (<span class="keyword">var</span> <span class="name">i</span> = <span class="number">0</span>; <span class="name">i</span> <span class="operator"><</span> <span class="name">gameCanvas</span>.<span class="name">cols</span>; i++) {
<span class="keyword">for</span> (<span class="keyword">var</span> <span class="name">j</span> = <span class="number">0</span>; <span class="name">j</span> <span class="operator"><</span> <span class="name">gameCanvas</span>.<span class="name">rows</span>; j++)
<span class="name">gameState</span>.<span class="name">towers</span>[<span class="name">towerIdx</span>(<span class="name">i</span>, <span class="name">j</span>)] <span class="operator">=</span> <span class="number">null</span>;
<span class="name">gameState</span>.<span class="name">mobs</span>[<span class="name">i</span>] <span class="operator">=</span> new <span class="name">Array</span>();
}
<span class="name">gameState</span>.<span class="name">towers</span>[<span class="name">towerIdx</span>(<span class="number">0</span>, <span class="number">0</span>)] <span class="operator">=</span> <span class="name">newTower</span>(<span class="number">3</span>, <span class="number">0</span>, <span class="number">0</span>);<span class="comment">//Start with a starfish in the corner</span>
<span class="name">gameState</span>.<span class="name">gameRunning</span> <span class="operator">=</span> <span class="number">true</span>;
<span class="name">gameState</span>.<span class="name">gameOver</span> <span class="operator">=</span> <span class="number">false</span>;
}
<span class="keyword">function</span> <span class="name">newGameState</span>(<span class="name">gameCanvas</span>)
{
<span class="keyword">for</span> (<span class="keyword">var</span> <span class="name">i</span> = <span class="number">0</span>; <span class="name">i</span> <span class="operator"><</span> <span class="name">towerComponents</span>.<span class="name">length</span>; i++) {
<span class="name">towerComponents</span>[<span class="name">i</span>] <span class="operator">=</span> <span class="name">Qt</span>.<span class="name">createComponent</span>(<span class="string">"towers/"</span> <span class="operator">+</span> <span class="name">towerData</span>[<span class="name">i</span>].<span class="name">name</span> <span class="operator">+</span> <span class="string">".qml"</span>);
<span class="keyword">if</span> (<span class="name">towerComponents</span>[<span class="name">i</span>].<span class="name">status</span> <span class="operator">==</span> <span class="name">QQ</span>.<span class="name">Component</span>.<span class="name">Error</span>) {
<span class="name">gameCanvas</span>.<span class="name">errored</span> <span class="operator">=</span> <span class="number">true</span>;
<span class="name">gameCanvas</span>.<span class="name">errorString</span> <span class="operator">+=</span> <span class="string">"Loading Tower "</span> <span class="operator">+</span> <span class="name">towerData</span>[<span class="name">i</span>].<span class="name">name</span> <span class="operator">+</span> <span class="string">"\n"</span> <span class="operator">+</span> (<span class="name">towerComponents</span>[<span class="name">i</span>].<span class="name">errorString</span>());
<span class="name">console</span>.<span class="name">log</span>(<span class="name">towerComponents</span>[<span class="name">i</span>].<span class="name">errorString</span>());
}
}
<span class="name">gameState</span> <span class="operator">=</span> <span class="name">gameCanvas</span>;
<span class="name">gameState</span>.<span class="name">freshState</span>();
<span class="name">gameState</span>.<span class="name">towers</span> <span class="operator">=</span> new <span class="name">Array</span>(<span class="name">gameCanvas</span>.<span class="name">rows</span> <span class="operator">*</span> <span class="name">gameCanvas</span>.<span class="name">cols</span>);
<span class="name">gameState</span>.<span class="name">mobs</span> <span class="operator">=</span> new <span class="name">Array</span>(<span class="name">gameCanvas</span>.<span class="name">cols</span>);
<span class="keyword">return</span> <span class="name">gameState</span>;
}
<span class="keyword">function</span> <span class="name">row</span>(<span class="name">y</span>)
{
<span class="keyword">return</span> <span class="name">Math</span>.<span class="name">floor</span>(<span class="name">y</span> <span class="operator">/</span> <span class="name">gameState</span>.<span class="name">squareSize</span>);
}
<span class="keyword">function</span> <span class="name">col</span>(<span class="name">x</span>)
{
<span class="keyword">return</span> <span class="name">Math</span>.<span class="name">floor</span>(<span class="name">x</span> <span class="operator">/</span> <span class="name">gameState</span>.<span class="name">squareSize</span>);
}
<span class="keyword">function</span> <span class="name">towerIdx</span>(<span class="name">x</span>, y)
{
<span class="keyword">return</span> <span class="name">y</span> <span class="operator">+</span> (<span class="name">x</span> <span class="operator">*</span> <span class="name">gameState</span>.<span class="name">rows</span>);
}
<span class="keyword">function</span> <span class="name">newMob</span>(<span class="name">col</span>)
{
var <span class="name">ret</span> = <span class="name">mobComponent</span>.<span class="name">createObject</span>(<span class="name">gameState</span>.<span class="name">canvas</span>,
{ "col" : <span class="name">col</span>,
"speed" : (<span class="name">Math</span>.<span class="name">min</span>(<span class="number">2.0</span>, <span class="number">0.10</span> <span class="operator">*</span> (<span class="name">gameState</span>.<span class="name">waveNumber</span> <span class="operator">+</span> <span class="number">1</span>))),
"y" : <span class="name">gameState</span>.<span class="name">canvas</span>.<span class="name">height</span> });
<span class="name">gameState</span>.<span class="name">mobs</span>[<span class="name">col</span>].<span class="name">push</span>(<span class="name">ret</span>);
<span class="keyword">return</span> <span class="name">ret</span>;
}
<span class="keyword">function</span> <span class="name">newTower</span>(<span class="name">type</span>, row, col)
{
var <span class="name">ret</span> = <span class="name">towerComponents</span>[<span class="name">type</span>].<span class="name">createObject</span>(<span class="name">gameState</span>.<span class="name">canvas</span>);
<span class="name">ret</span>.<span class="name">row</span> <span class="operator">=</span> <span class="name">row</span>;
<span class="name">ret</span>.<span class="name">col</span> <span class="operator">=</span> <span class="name">col</span>;
<span class="name">ret</span>.<span class="name">fireCounter</span> <span class="operator">=</span> <span class="name">ret</span>.<span class="name">rof</span>;
<span class="name">ret</span>.<span class="name">spawn</span>();
<span class="keyword">return</span> <span class="name">ret</span>;
}
<span class="keyword">function</span> <span class="name">buildTower</span>(<span class="name">type</span>, x, y)
{
<span class="keyword">if</span> (<span class="name">gameState</span>.<span class="name">towers</span>[<span class="name">towerIdx</span>(<span class="name">x</span>,<span class="name">y</span>)] <span class="operator">!=</span> <span class="number">null</span>) {
<span class="keyword">if</span> (<span class="name">type</span> <span class="operator"><=</span> <span class="number">0</span>) {
<span class="name">gameState</span>.<span class="name">towers</span>[<span class="name">towerIdx</span>(<span class="name">x</span>,<span class="name">y</span>)].<span class="name">sell</span>();
<span class="name">gameState</span>.<span class="name">towers</span>[<span class="name">towerIdx</span>(<span class="name">x</span>,<span class="name">y</span>)] <span class="operator">=</span> <span class="number">null</span>;
}
} <span class="keyword">else</span> {
<span class="keyword">if</span> (<span class="name">gameState</span>.<span class="name">coins</span> <span class="operator"><</span> <span class="name">towerData</span>[<span class="name">type</span> <span class="operator">-</span> <span class="number">1</span>].<span class="name">cost</span>)
<span class="keyword">return</span>;
<span class="name">gameState</span>.<span class="name">towers</span>[<span class="name">towerIdx</span>(<span class="name">x</span>, <span class="name">y</span>)] <span class="operator">=</span> <span class="name">newTower</span>(<span class="name">type</span> <span class="operator">-</span> <span class="number">1</span>, <span class="name">y</span>, <span class="name">x</span>);
<span class="name">gameState</span>.<span class="name">coins</span> <span class="operator">-=</span> <span class="name">towerData</span>[<span class="name">type</span> <span class="operator">-</span> <span class="number">1</span>].<span class="name">cost</span>;
}
}
<span class="keyword">function</span> <span class="name">killMob</span>(<span class="name">col</span>, mob)
{
<span class="keyword">if</span> (!<span class="name">mob</span>)
<span class="keyword">return</span>;
var <span class="name">idx</span> = <span class="name">gameState</span>.<span class="name">mobs</span>[<span class="name">col</span>].<span class="name">indexOf</span>(<span class="name">mob</span>);
<span class="keyword">if</span> (<span class="name">idx</span> <span class="operator">==</span> -<span class="number">1</span> <span class="operator">||</span> !<span class="name">mob</span>.<span class="name">hp</span>)
<span class="keyword">return</span>;
<span class="name">mob</span>.<span class="name">hp</span> <span class="operator">=</span> <span class="number">0</span>;
<span class="name">mob</span>.<span class="name">die</span>();
<span class="name">gameState</span>.<span class="name">mobs</span>[<span class="name">col</span>].<span class="name">splice</span>(<span class="name">idx</span>,<span class="number">1</span>);
}
<span class="keyword">function</span> <span class="name">killTower</span>(<span class="name">row</span>, col)
{
var <span class="name">tower</span> = <span class="name">gameState</span>.<span class="name">towers</span>[<span class="name">towerIdx</span>(<span class="name">col</span>, <span class="name">row</span>)];
<span class="keyword">if</span> (!<span class="name">tower</span>)
<span class="keyword">return</span>;
<span class="name">tower</span>.<span class="name">hp</span> <span class="operator">=</span> <span class="number">0</span>;
<span class="name">tower</span>.<span class="name">die</span>();
<span class="name">gameState</span>.<span class="name">towers</span>[<span class="name">towerIdx</span>(<span class="name">col</span>, <span class="name">row</span>)] <span class="operator">=</span> <span class="number">null</span>;
}
<span class="keyword">function</span> <span class="name">tick</span>()
{
<span class="keyword">if</span> (!<span class="name">gameState</span>.<span class="name">gameRunning</span>)
<span class="keyword">return</span>;
<span class="comment">// Spawn</span>
<span class="name">gameState</span>.<span class="name">waveProgress</span> <span class="operator">+=</span> <span class="number">1</span>;
var <span class="name">i</span> = <span class="name">gameState</span>.<span class="name">waveProgress</span>;
var <span class="name">j</span> = <span class="number">0</span>;
<span class="keyword">while</span> (<span class="name">i</span> <span class="operator">></span> <span class="number">0</span> <span class="operator">&&</span> <span class="name">j</span> <span class="operator"><</span> <span class="name">waveData</span>.<span class="name">length</span>)
<span class="name">i</span> <span class="operator">-=</span> <span class="name">waveData</span>[j++];
<span class="keyword">if</span> ( <span class="name">i</span> <span class="operator">==</span> <span class="number">0</span> ) <span class="comment">// Spawn a mob</span>
<span class="name">newMob</span>(<span class="name">Math</span>.<span class="name">floor</span>(<span class="name">Math</span>.<span class="name">random</span>() <span class="operator">*</span> <span class="name">gameState</span>.<span class="name">cols</span>));
<span class="keyword">if</span> ( <span class="name">j</span> <span class="operator">==</span> <span class="name">waveData</span>.<span class="name">length</span> ) { <span class="comment">// Next Wave</span>
<span class="name">gameState</span>.<span class="name">waveNumber</span> <span class="operator">+=</span> <span class="number">1</span>;
<span class="name">gameState</span>.<span class="name">waveProgress</span> <span class="operator">=</span> <span class="number">0</span>;
var <span class="name">waveModifier</span> = <span class="number">10</span>; <span class="comment">// Constant governing how much faster the next wave is to spawn (not fish speed)</span>
<span class="keyword">for</span> (<span class="keyword">var</span> <span class="name">k</span> in <span class="name">waveData</span> ) <span class="comment">// Slightly faster</span>
<span class="keyword">if</span> (<span class="name">waveData</span>[<span class="name">k</span>] <span class="operator">></span> <span class="name">waveModifier</span>)
<span class="name">waveData</span>[<span class="name">k</span>] <span class="operator">-=</span> <span class="name">waveModifier</span>;
}
<span class="comment">// Towers Attack</span>
<span class="keyword">for</span> (<span class="keyword">var</span> <span class="name">j</span> in <span class="name">gameState</span>.<span class="name">towers</span>) {
var <span class="name">tower</span> = <span class="name">gameState</span>.<span class="name">towers</span>[<span class="name">j</span>];
<span class="keyword">if</span> (<span class="name">tower</span> <span class="operator">==</span> <span class="number">null</span>)
<span class="keyword">continue</span>;
<span class="keyword">if</span> (<span class="name">tower</span>.<span class="name">fireCounter</span> <span class="operator">></span> <span class="number">0</span>) {
<span class="name">tower</span>.<span class="name">fireCounter</span> <span class="operator">-=</span> <span class="number">1</span>;
<span class="keyword">continue</span>;
}
var <span class="name">column</span> = <span class="name">tower</span>.<span class="name">col</span>;
<span class="keyword">for</span> (<span class="keyword">var</span> <span class="name">k</span> in <span class="name">gameState</span>.<span class="name">mobs</span>[<span class="name">column</span>]) {
var <span class="name">conflict</span> = <span class="name">gameState</span>.<span class="name">mobs</span>[<span class="name">column</span>][<span class="name">k</span>];
<span class="keyword">if</span> (<span class="name">conflict</span>.<span class="name">y</span> <span class="operator"><=</span> <span class="name">gameState</span>.<span class="name">canvas</span>.<span class="name">height</span> <span class="operator">&&</span> <span class="name">conflict</span>.<span class="name">y</span> <span class="operator">+</span> <span class="name">conflict</span>.<span class="name">height</span> <span class="operator">></span> <span class="name">tower</span>.<span class="name">y</span>
<span class="operator">&&</span> <span class="name">conflict</span>.<span class="name">y</span> <span class="operator">-</span> ((<span class="name">tower</span>.<span class="name">row</span> <span class="operator">+</span> <span class="number">1</span>) <span class="operator">*</span> <span class="name">gameState</span>.<span class="name">squareSize</span>) <span class="operator"><</span> <span class="name">gameState</span>.<span class="name">squareSize</span> <span class="operator">*</span> <span class="name">tower</span>.<span class="name">range</span>) { <span class="comment">// In Range</span>
<span class="name">tower</span>.<span class="name">fire</span>();
<span class="name">tower</span>.<span class="name">fireCounter</span> <span class="operator">=</span> <span class="name">tower</span>.<span class="name">rof</span>;
<span class="name">conflict</span>.<span class="name">hit</span>(<span class="name">tower</span>.<span class="name">damage</span>);
}
}
<span class="comment">// Income</span>
<span class="keyword">if</span> (<span class="name">tower</span>.<span class="name">income</span>) {
<span class="name">gameState</span>.<span class="name">coins</span> <span class="operator">+=</span> <span class="name">tower</span>.<span class="name">income</span>;
<span class="name">tower</span>.<span class="name">fire</span>();
<span class="name">tower</span>.<span class="name">fireCounter</span> <span class="operator">=</span> <span class="name">tower</span>.<span class="name">rof</span>;
}
}
<span class="comment">// Mobs move</span>
<span class="keyword">for</span> (<span class="keyword">var</span> <span class="name">i</span> = <span class="number">0</span>; <span class="name">i</span> <span class="operator"><</span> <span class="name">gameState</span>.<span class="name">cols</span>; i++) {
<span class="keyword">for</span> (<span class="keyword">var</span> <span class="name">j</span> = <span class="number">0</span>; <span class="name">j</span> <span class="operator"><</span> <span class="name">gameState</span>.<span class="name">mobs</span>[<span class="name">i</span>].<span class="name">length</span>; j++) {
var <span class="name">mob</span> = <span class="name">gameState</span>.<span class="name">mobs</span>[<span class="name">i</span>][<span class="name">j</span>];
var <span class="name">newPos</span> = <span class="name">gameState</span>.<span class="name">mobs</span>[<span class="name">i</span>][<span class="name">j</span>].<span class="name">y</span> <span class="operator">-</span> <span class="name">gameState</span>.<span class="name">mobs</span>[<span class="name">i</span>][<span class="name">j</span>].<span class="name">speed</span>;
<span class="keyword">if</span> (<span class="name">newPos</span> <span class="operator"><</span> <span class="number">0</span>) {
<span class="name">gameState</span>.<span class="name">lives</span> <span class="operator">-=</span> <span class="number">1</span>;
<span class="name">killMob</span>(<span class="name">i</span>, <span class="name">mob</span>);
<span class="keyword">if</span> (<span class="name">gameState</span>.<span class="name">lives</span> <span class="operator"><=</span> <span class="number">0</span>)
<span class="name">endGame</span>();
<span class="keyword">continue</span>;
}
var <span class="name">conflict</span> = <span class="name">gameState</span>.<span class="name">towers</span>[<span class="name">towerIdx</span>(<span class="name">i</span>, <span class="name">row</span>(<span class="name">newPos</span>))];
<span class="keyword">if</span> (<span class="name">conflict</span> <span class="operator">!=</span> <span class="number">null</span>) {
<span class="keyword">if</span> (<span class="name">mob</span>.<span class="name">y</span> <span class="operator"><</span> <span class="name">conflict</span>.<span class="name">y</span> <span class="operator">+</span> <span class="name">gameState</span>.<span class="name">squareSize</span>)
<span class="name">gameState</span>.<span class="name">mobs</span>[<span class="name">i</span>][<span class="name">j</span>].<span class="name">y</span> <span class="operator">+=</span> <span class="name">gameState</span>.<span class="name">mobs</span>[<span class="name">i</span>][<span class="name">j</span>].<span class="name">speed</span> <span class="operator">*</span> <span class="number">10</span>; <span class="comment">// Moved inside tower, now hurry back out</span>
<span class="keyword">if</span> (<span class="name">mob</span>.<span class="name">fireCounter</span> <span class="operator">></span> <span class="number">0</span>) {
mob.fireCounter--;
} <span class="keyword">else</span> {
<span class="name">gameState</span>.<span class="name">mobs</span>[<span class="name">i</span>][<span class="name">j</span>].<span class="name">fire</span>();
<span class="name">conflict</span>.<span class="name">hp</span> <span class="operator">-=</span> <span class="name">mob</span>.<span class="name">damage</span>;
<span class="keyword">if</span> (<span class="name">conflict</span>.<span class="name">hp</span> <span class="operator"><=</span> <span class="number">0</span>)
<span class="name">killTower</span>(<span class="name">conflict</span>.<span class="name">row</span>, <span class="name">conflict</span>.<span class="name">col</span>);
<span class="name">mob</span>.<span class="name">fireCounter</span> <span class="operator">=</span> <span class="name">mob</span>.<span class="name">rof</span>;
}
} <span class="keyword">else</span> {
<span class="name">gameState</span>.<span class="name">mobs</span>[<span class="name">i</span>][<span class="name">j</span>].<span class="name">y</span> <span class="operator">=</span> <span class="name">newPos</span>;
}
}
}
}</pre>
</div>
<!-- @@@demos/maroon/content/logic.js -->
</div>
</div>
</div>
</div>
</div>
<div class="footer">
<p>
<acronym title="Copyright">©</acronym> 2015 The Qt Company Ltd.
Documentation contributions included herein are the copyrights of
their respective owners.<br> The documentation provided herein is licensed under the terms of the <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation License version 1.3</a> as published by the Free Software Foundation.<br> Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property
of their respective owners. </p>
</div>
</body>
</html>
|