Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
layarmmo-monsters [2012/05/11 08:21]
matthewleach created
layarmmo-monsters [2012/11/18 13:51]
matthewleach Added descriptions around code
Line 1: Line 1:
-== Loading Monsters From the Database ==+==== Loading Monsters From the Database ==== 
 +It's time to add our monsters to the real-world. ​ We are going to make some significant changes to the code that we already have, so that so that the games searches for and displays any monsters within range of the player.
  
-Changes to simple-display.php.  ​In the include:+===Changes to Main File=== 
 +In our main file we are going to add another ​include ​statement.
  
 <code php> <code php>
Line 9: Line 11:
  
 In the echo: In the echo:
 +
 +Where the description of the hotspots normally goes, we're going to call a function **findMonsters**. ​ This function is defined in the monsters.php file that we included above.
  
 <code php> <code php>
Line 30: Line 34:
 </​code>​ </​code>​
  
-The process:+===Process for Loading Monsters=== 
 +Now we'll start looking at what to put in the monsters.php file.  ​The basic process ​that we're going to follow is outlined below:
  
 <​code>​ <​code>​
Line 40: Line 45:
 </​code>​ </​code>​
  
-The actual code:+===Code for Loading Monsters=== 
 +Shown below is the actual code.  Copy it, paste it, and save the file as monsters.php. ​ This will search the database for any monsters in range, and output them as '​hotspots'​ (or '​points of interest',​ or some other terminology that just means something with a location).
  
 <code php> <code php>
Line 63: Line 69:
  $monsterLon = $results[$loop]['​lon'​];​  $monsterLon = $results[$loop]['​lon'​];​
   
- // Find monster details+ // Find specific ​monster details, including some details from besitary
  $mdet = $db->​prepare( "​SELECT `name`, `desc`, `image` FROM `bestiary` WHERE `id`=:​montype"​ );  $mdet = $db->​prepare( "​SELECT `name`, `desc`, `image` FROM `bestiary` WHERE `id`=:​montype"​ );
  $mdet->​bindParam( ':​montype',​ $results[$loop]['​type'​],​ PDO::​PARAM_INT);​  $mdet->​bindParam( ':​montype',​ $results[$loop]['​type'​],​ PDO::​PARAM_INT);​
Line 106: Line 112:
 </​code>​ </​code>​
  
-Explanation - particularly on the distance ​query.+===Explanation ​of Code=== 
 + 
 +Almost the first line is something that probably makes no sense to you - the call to the database. 
 + 
 +<code php> 
 +        // Find monsters in range 
 + $sql = $db->​prepare( "​SELECT *, ( 6371000 * ACOS( COS( RADIANS(:​lat) ) * COS( RADIANS( lat ) ) * cos( RADIANS( lon ) - RADIANS(:​lon) ) + SIN(RADIANS(:​lat) ) * SIN( RADIANS( lat ) ) ) ) AS distance ​FROM monster ORDER BY distance LIMIT 0 , 20;" ); 
 +</​code>​ 
 + 
 +The main part is a distance calculation,​ and if that is removed, you should find it less daunting. 
 + 
 +<code php> 
 +        // Find monsters in range 
 + $sql = $db->​prepare( "​SELECT *, calculate_distance FROM monster ORDER BY distance LIMIT 0 , 20;" ); 
 +</​code>​ 
 + 
 +As all but the medieval-minded amongst us are aware, the Earth is roughly a sphere. ​ This makes distance calculations quite complicated,​ since latitude is actually a measure of your angle around the Earth, and is a longer distance at the equator than near the pole. I just used the code from one of Layar'​s examples, but you can look into it further on [[http://​en.wikipedia.org/​wiki/​Great-circle_distance|Wikipedia:​ Great-circle distances]]. 
 + 
 +The main bulk of the function should be fairly familiar. ​ It is just outputting any monsters that are found in the JSON format for Layar. 
 +