<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>http://wiki.spheredev.org/index.php?action=history&amp;feed=atom&amp;title=Battle_System_Tutorial_by_Flik</id>
		<title>Battle System Tutorial by Flik - Revision history</title>
		<link rel="self" type="application/atom+xml" href="http://wiki.spheredev.org/index.php?action=history&amp;feed=atom&amp;title=Battle_System_Tutorial_by_Flik"/>
		<link rel="alternate" type="text/html" href="http://wiki.spheredev.org/index.php?title=Battle_System_Tutorial_by_Flik&amp;action=history"/>
		<updated>2026-05-02T13:49:05Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.29.0</generator>

	<entry>
		<id>http://wiki.spheredev.org/index.php?title=Battle_System_Tutorial_by_Flik&amp;diff=734&amp;oldid=prev</id>
		<title>Apollolux: created from http://web.archive.org/web/20120911000900/http://spheredev.org/wiki/Battle_System_Tutorial_by_Flik</title>
		<link rel="alternate" type="text/html" href="http://wiki.spheredev.org/index.php?title=Battle_System_Tutorial_by_Flik&amp;diff=734&amp;oldid=prev"/>
				<updated>2013-06-08T00:44:56Z</updated>
		
		<summary type="html">&lt;p&gt;created from http://web.archive.org/web/20120911000900/http://spheredev.org/wiki/Battle_System_Tutorial_by_Flik&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;How on Earth do you create a battle system in Sphere? Simple, you script it. (Okay, so it can be a bit difficult.)&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
We're going to break the battle system down into separate bits so that it's easier to create.&lt;br /&gt;
&lt;br /&gt;
Since most RPG battle systems are menu-driven, you may want to learn how to use a menu object. ([[Creating a menu using flik_menu.js]])&lt;br /&gt;
&lt;br /&gt;
We'll be using objects too, if you want to read up on that. ([[Simple OOP in JavaScript]])&lt;br /&gt;
&lt;br /&gt;
You don't need to read those to understand this, though.&lt;br /&gt;
&lt;br /&gt;
==Combatants: things that can fight==&lt;br /&gt;
Okay, the people or things or whatever that are fighting? We'll call them 'combatants'.&lt;br /&gt;
&lt;br /&gt;
We'll have a list of combatants in an array.&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
var combatants = new Array();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So whenever a combatant appears, they go into that array. A player is the same as a monster, okay? :)&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
function Combatant(name) {&lt;br /&gt;
  this.name = name;&lt;br /&gt;
  this.x = 50;&lt;br /&gt;
  this.y = 50;&lt;br /&gt;
 &lt;br /&gt;
  this.min_hp = 0;  // don't ask why&lt;br /&gt;
  this.hp = 10000;&lt;br /&gt;
  this.max_hp = 50;&lt;br /&gt;
  this.str = 10;&lt;br /&gt;
 &lt;br /&gt;
  this.type = &amp;quot;monster&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
Combatant.prototype.clone = function() {&lt;br /&gt;
  var obj = new Combatant();&lt;br /&gt;
  for (var i in this)&lt;br /&gt;
    obj[i] = this[i];&lt;br /&gt;
  return obj;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{aside|Sidetrack: Why clone the object?|We clone the object so that we don't accidentally change the original.}}&lt;br /&gt;
&lt;br /&gt;
==Putting our combatants in a battle==&lt;br /&gt;
So this creates a Combatant object when we do:&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
var freddy = new Combatant(&amp;quot;Freddy&amp;quot;);  // create the object&lt;br /&gt;
    freddy.type = &amp;quot;player&amp;quot;;            // set the type to &amp;quot;player&amp;quot;&lt;br /&gt;
    freddy.x = 200;&lt;br /&gt;
 &lt;br /&gt;
var blob = new Combatant(&amp;quot;Blob&amp;quot;);&lt;br /&gt;
    blob.hp = 5000;&lt;br /&gt;
 &lt;br /&gt;
combatants.push(freddy.clone());  // freddy has joined the party&lt;br /&gt;
combatants.push(blob.clone());    // uh oh, we have a monster to fight&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{aside|Sidetrack: Why the huge hp values?|Because there's no delaying code between attacks,so it goes really fast.}}&lt;br /&gt;
&lt;br /&gt;
==Figuring out if the battle is over==&lt;br /&gt;
Now, how do we check to see if we need to fight someone? Well, we loop through the combatants array and check the type of each combatant.&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
function GetTotalMonsters() {&lt;br /&gt;
  var total_monsters = 0;&lt;br /&gt;
  for (var i = 0; i &amp;lt; combatants.length; ++i)&lt;br /&gt;
    if (combatants[i].type == &amp;quot;monster&amp;quot;)&lt;br /&gt;
      total_monsters += 1;&lt;br /&gt;
  return total_monsters;&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
function GetTotalPlayers() {&lt;br /&gt;
  var total_players = 0;&lt;br /&gt;
  for (var i = 0; i &amp;lt; combatants.length; ++i)&lt;br /&gt;
    if (combatants[i].type == &amp;quot;player&amp;quot;)&lt;br /&gt;
      total_players += 1;&lt;br /&gt;
  return total_players;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawing the combatants==&lt;br /&gt;
Okay, so let's add some rendering code to the Combatant object.&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
Combatant.prototype.render = function() {&lt;br /&gt;
  var font = GetSystemFont();&lt;br /&gt;
  font.drawText(this.x, this.y, this.name);&lt;br /&gt;
  font.drawText(this.x, this.y - 20, this.hp);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Figuring out turns==&lt;br /&gt;
We also need some sort of system for finding out who's turn it is. Enter... the turn list:&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
var turn_list = new Array();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==AI==&lt;br /&gt;
Also, now let's add some AI to them:&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
function GetAliveMonstersArray() {&lt;br /&gt;
  var total_alive = new Array();&lt;br /&gt;
  for (var i = 0; i &amp;lt; combatants.length; ++i)&lt;br /&gt;
    if (combatants[i].type == &amp;quot;monster&amp;quot; &amp;amp;&amp;amp; combatants[i].hp &amp;gt; 0)&lt;br /&gt;
      total_alive.push(i);&lt;br /&gt;
  return total_alive;&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
function GetAlivePlayersArray() {&lt;br /&gt;
  var total_alive = new Array();&lt;br /&gt;
  for (var i = 0; i &amp;lt; combatants.length; ++i)&lt;br /&gt;
    if (combatants[i].type == &amp;quot;player&amp;quot; &amp;amp;&amp;amp; combatants[i].hp &amp;gt; 0)&lt;br /&gt;
      total_alive.push(i);&lt;br /&gt;
  return total_alive;&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
function Attack(from_who, on_who) {&lt;br /&gt;
  combatants[on_who].hp -= combatants[from_who].str;&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
Combatant.prototype.AI = function() {&lt;br /&gt;
  if (this.type == &amp;quot;player&amp;quot;) {&lt;br /&gt;
    // create menus here&lt;br /&gt;
    var attack_selection = GetAliveMonstersArray()[0];  // we attack 1st monster&lt;br /&gt;
    Attack(turn_list[0], attack_selection);&lt;br /&gt;
  } else if (this.type == &amp;quot;monster&amp;quot;) {&lt;br /&gt;
    // just normal attacking here&lt;br /&gt;
    var attack_selection = GetAlivePlayersArray()[0];  // they attack 1st player&lt;br /&gt;
    Attack(turn_list[0], attack_selection);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawing the battle==&lt;br /&gt;
Finally, we do:&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
function RenderBattle() {&lt;br /&gt;
  for (var i = 0; i &amp;lt; combatants.length; ++i)&lt;br /&gt;
    combatants[i].render();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Bringing it all together==&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
function Battle() {&lt;br /&gt;
  var run_away = false;&lt;br /&gt;
 &lt;br /&gt;
  while (GetAlivePlayersArray().length &amp;gt; 0&lt;br /&gt;
      &amp;amp;&amp;amp; GetAliveMonstersArray().length &amp;gt; 0&lt;br /&gt;
      &amp;amp;&amp;amp; !run_away) {&lt;br /&gt;
 &lt;br /&gt;
    if (IsKeyPressed(KEY_R))&lt;br /&gt;
      run_away = true;&lt;br /&gt;
 &lt;br /&gt;
    // deal turns&lt;br /&gt;
    if (turn_list.length == 0) {&lt;br /&gt;
      for (var i = 0; i &amp;lt; combatants.length; ++i)&lt;br /&gt;
        turn_list.push(i);&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
    // process the current turn&lt;br /&gt;
    if (turn_list.length &amp;gt; 0) {&lt;br /&gt;
      combatants[turn_list[0]].AI();&lt;br /&gt;
      turn_list.shift();&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
    RenderBattle();&lt;br /&gt;
    FlipScreen();&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Press 'R' to run from battle.&lt;br /&gt;
&lt;br /&gt;
==Trying it out==&lt;br /&gt;
Let's say that all of that is in a file called &amp;lt;var&amp;gt;freddyvsblob.js&amp;lt;/var&amp;gt;. In your main/startup script:&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
RequireScript(&amp;quot;freddyvsblob.js&amp;quot;);&lt;br /&gt;
 &lt;br /&gt;
function game() {&lt;br /&gt;
  SetFrameRate(60);&lt;br /&gt;
  Battle();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you ever need to do it again, just do this:&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
combatants.push(freddy.clone());&lt;br /&gt;
combatants.push(blob.clone());&lt;br /&gt;
Battle();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==The whole script==&lt;br /&gt;
You can find all the battle system code at Flikky's site: http://sphere.sourceforge.net/flik/files/freddyvsblob.js .&lt;br /&gt;
&lt;br /&gt;
By Flikky, updated for the wiki by Tunginobi.&lt;br /&gt;
&lt;br /&gt;
[[Category:Tutorials]]&lt;/div&gt;</summary>
		<author><name>Apollolux</name></author>	</entry>

	</feed>