Javascript and HTML canvas game.

Random code work for the fun of creating something.
I was building a website project using javascript and php, I ended up learning enough to use the new HTML 5 canvas element as a display screen for a little game engine.
Have not done much with javascript so my code at the time I made this
was setup very badly.
I have since seen how to do oop structures in javascript
when I started using it again for rpgmaker mv.

At this point in time I'm good enough to setup a texture atlas, have collision detection, monster Ai random walk and seek and destroy, even a cat that does anti monster seek and destroy.
This is a html javascript game so it plays in the browser. I will upload the source.
To point out a giant flaw with this, I'm just running the movement of objects with out taking into account the time passed since last frame so it runs faster or slower depending on
your computer hardware. I also did alot of crapy array management and just said meh.. screw it and kept going lol.
I'm sure my code is funky but I only do this stuff for personal enjoyment so who cares XD.

Here is a video of me playing the game.
http://sendvid.com/wh02frxv

Image of the game.
Image

This is the html page that's playing host to the canvas element, It's part of the website project I was working on.
as you can see the javascript that is running the game is located up a folder from this file and down into another
folder named scripts. So you can copy the file structure or relocate the script and update its path the html script linking part if your
going to make a copy of this. And btw feel free to make copies of this and do anything with it.
The button on the page is launching the first call to a javascript function that
causes the game to start.
Now for the source!

HTML
  1.  
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5.     <!-- The meta info -->
  6.     <link rel="shortcut icon" href="Page.ico">
  7.     <title>Jack's Site</title>
  8.     <meta name="keywords" content="HTML, CSS, XML, XHTML, JavaScript">
  9.     <meta name="description" content="Free Web tutorials on HTML and CSS">
  10.     <meta name="author" content="Hege Refsnes">
  11.     <link rel="stylesheet" type="text/css" href="../index.css" >
  12.     <script type="text/javascript" src="../scripts/script14.js"></script>
  13. </head>
  14. <!-- RUN THE KEY HANDLER FUNCTION ON KEYPUSH AND WHEN A KEY IS RELEASED ONLY SET ITS INDICATOR TO OFF TO STOP IT FROM FREEZING WHEN 1 KEY IS RELEASED -->
  15. <body onkeydown="keyboardhan(event)" onkeyup="keyboardhan2(event)">
  16.     <section class="containerArticle" style="overflow:hidden">
  17.         <header>
  18.             <hgroup>
  19.                 <center>
  20.                     <h1>More of a real game.</h1>
  21.                 </center>
  22.                 <h2>This time i'm attempting a more normal rpg type game.</h2>
  23.             </hgroup>
  24.             <p>Starting with a simple table as the map screen.</p>
  25.         </header>
  26.         <button onclick="gameloop();return false" style="float:left">TEST</button>
  27.         <div class="screendiv">
  28.             <script type="text/javascript">
  29.                 document.write("<canvas width=\"800\" height=\"600\" id=\"rpgscreen\"></canvas>");
  30.             </script>
  31.         </div>
  32.     </section>
  33. </body>
  34. </html>
  35.  
Expand to see the code.


Now for the javascript that is the game it's self.

  1. var time=0;
  2. var seconds=0;var minutes=0;var hours=0;
  3. var hp=100;var mp=200;var helpers=0;
  4. var clockinterval=0;
  5. var timesample=[];
  6. var canvasgame;
  7. var contextgame;
  8. var typelist=[];
  9. tilesizex=32;
  10. tilesizey=32;
  11. tilemaptilesizex=32;
  12. tilemaptilesizey=32;
  13. var maplength=64;
  14. var mapxlength=64;
  15. var tilemap = new Image();
  16. tilemap.src = "../images/rpggame/tilemap.png";
  17. var zoomx=32;
  18. var zoomy=32;
  19. var typename=0;
  20. var cameraposx=0;
  21. var cameraposy=0;
  22.  
  23. var lastone=1;
  24.  
  25. //THERE NEEDS TO BE A GENERATOR THAT CONSTRUCTS THIS INFORMATION FROM LOCALHOST
  26. //NPCLIST HOLDS TILE-IMAGE X Y COLLSIONABLE? AI-INDICATOR HP MP FOR EACH NPC
  27. var npclistx=[339,512,384,0,999,100,100,0,0,3,1,1];
  28. var npclistx2=[12,-100,-100,2,74,100,100,0,0,3,1,1];
  29. var npclistx3=[12,-100,-100,2,74,100,100,0,0,3,1,1];
  30. var npclistx4=[339,55,200,1,150,100,100,2,2,3,1,1];
  31. var npclistx5=[339,100,260,1,1,100,100,-2,-2,3,1,1];
  32. var npclistx6=[339,60,200,1,1,100,100,-2,0,3,1,1];
  33. var npclist=[npclistx,npclistx2,npclistx3,npclistx4,npclistx5,npclistx6];
  34.  
  35. var tileimage;
  36. var npcx;
  37. var npcy;
  38. var dnpcx;
  39. var dnpcy;
  40. var mnpcx;
  41. var mnpcy;
  42. var mdnpcx;
  43. var mdnpcy;
  44. var solid;
  45. var aitype;
  46. var hp;
  47. var mp;
  48. var overlapopunit;
  49. var key1=0;
  50. var key2=0;
  51. var key3=0;
  52. var key4=0;
  53. var key5=0;
  54. var key6=0;
  55. var key7=0;
  56. var key8=0;
  57. var key9=0;
  58. var charspeed=3;
  59. var b=[];
  60. var aitime=[];
  61. var aitime2=[];
  62. b[0]=time;
  63. aitime[0]=0;
  64. aitime2[0]=0;
  65. var npclisttemptty;
  66. var newvecx;
  67. var newvecy;
  68. var path1=[500,500,750,1000,1000];
  69. var path2=[750,1000,1500,1000,750];
  70. var tomsfrog=0;
  71. var tomsfrog2=0;
  72. var frogtime=time;
  73. var aimove1time=time;
  74. var map=[];
  75.  
  76. //A X AND Y LIST FOR TILE CORDS FOR ANIMATED TILES
  77. var celllist=[];
  78. var celllist2=[];
  79. var aitime3=[];
  80. aitime3[0]=0;
  81.  
  82. var swordtoggle=0;
  83. var swordtoggle2=0;
  84. var swordtoggle3=0;
  85. var swordtoggle4=0;
  86.  
  87. //THE NPC ANIMATION SET
  88. var animation1=[366,344];
  89. var animation3=[367,345];
  90. var animation4=[365,343];
  91. var animation5=[364,342];
  92. var animation2=[339+66,339+67,339+77,339+78];
  93. var animation6=[375,386];
  94. var animation7=[376,387];
  95. var animation8=[377,388];
  96. var animation9=[378,389];
  97. var animation12=[329,351];
  98. var animation13=[360,382];
  99. var animation10=[327,349];
  100. var animation11=[328,350];
  101. var animationidl1=[415,426];
  102. var animationidl2=[437,448];
  103. var animationidl3=[393,404];
  104.  
  105. //THE PLAYER CHARACTER ANIMATION
  106. var animationcharidl=[animationidl1,animationidl2,animationidl3];
  107. var characterdirani=[animation5,animation1,animation4,animation3,animation6,animation7,animation8,animation9,animation10,animation11,animation12,animation13];
  108.  
  109. var startopen=1;
  110. var splash1 = new Image();
  111. var splash2 = new Image();
  112. splash1.src = "../images/rpggame/splash1.png";
  113. splash2.src = "../images/rpggame/splash2.png";
  114. var cycletrack=0;
  115. var cycletrack2=0;
  116. var cycletrackr=0;
  117. var selection=0;
  118. var invlist=[];
  119.  
  120. function invsystem()
  121. {
  122.     for (var i=selection;i<typelist.length;i=i+3)
  123.     {
  124.         if (invlist[i/3]>0)
  125.         {
  126.             contextgame.drawImage(tilemap, typelist[i+2], typelist[i+1], 32, 32,  typelist[i+2]-selection,  typelist[i+1], 32, 32);
  127.         }
  128.     }
  129. }
  130. function buildinv()
  131. {
  132.     for (var i=0;i<typelist.length/3;i++)
  133.     {
  134.         invlist.push(0);
  135.     }
  136. }
  137. function clockbuild()
  138. {
  139.     //UPDATE THE CLOCKS SECONDS MIN AND HR
  140.     if (time-timesample[0]>=100) { seconds++; timesample[0]=time; }
  141.     if (seconds==60) { minutes++; seconds=1; }
  142.     if (minutes==60) { hours++; minutes=1; }
  143. }
  144. function clockdisplay()
  145. {
  146.     //DRAW THE CLOCK HP MP HELPERS
  147.     contextgame.fillStyle = "white";
  148.     contextgame.fillRect(724, 0, 300, 100);
  149.     contextgame.fillStyle = "black";
  150.     contextgame.font = "bold 20px sans-serif";
  151.     contextgame.fillText(hours+":"+minutes+":"+seconds+" Time Since Start.", 724, 20);
  152.     contextgame.fillText(time+" Total time units.", 724, 40);
  153.     contextgame.fillText(hp+" Total Hp.", 724, 60);
  154.     contextgame.fillText(mp+" Total Mp.", 724, 80);
  155.     contextgame.fillText(helpers+" Total Helpers.", 724, 100);
  156. }
  157. function clock()
  158. {
  159.     //RUN THE CLOCK UPDATE AND THEN RESTART
  160.     time++;
  161. }
  162. function gamescene01()
  163. {
  164.     if (clockinterval==0)
  165.     {
  166.         //BUILD THE TILES FROM THE TILE IMAGE
  167.         clockinterval=1;setInterval(clock,1); timesample[0]=time;
  168.         canvasgame = document.getElementById("rpgscreen"); contextgame = canvasgame.getContext("2d");
  169.         generatemap();
  170.         buildtiles();
  171.         buildinv();
  172.     }
  173.     //CLEAR THE IMAGES FROM LAST FRAME AND RENDER THE MAP
  174.     canvasgame.width=canvasgame.width;
  175.     //RUN ALL THE NPCS AND RENDER STUFF
  176.     updatenpcs();
  177.     animatetiles();
  178.     //RENDER THE MAP TILES AND THE NPCS
  179.     displaymap();
  180.     displaynpcs();
  181.     //BUILD THE CURRENT TIME IN SECONDS MIN HOURS AND DISPLAY IT
  182.     clockbuild();
  183.     clockdisplay();
  184.     //CHARACTER STAT DISPLAYS
  185.     healthbar();
  186.     manabar();
  187.  
  188.     if (swordtoggle4==1)
  189.     {
  190.         invsystem();
  191.     }
  192.     //RUN THE LOOP AGAIN
  193.     setTimeout(gamescene01,5);
  194.     b[0]=time;
  195. }
  196. function gamescene02()
  197. {
  198.     canvasgame = document.getElementById("rpgscreen"); contextgame = canvasgame.getContext("2d");
  199.     contextgame.drawImage(splash1, 0, 0);
  200. }
  201. //THE MAIN GAME LOOP~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  202. function gameloop()
  203. {
  204.     gamescene02();
  205.     setTimeout(gamescene01,3000);
  206. }
  207.  
  208. function buildtiles()
  209. {
  210.     generatetiles();
  211.     placenpcs();
  212. }
  213. function animatetiles()
  214. {
  215.     if (aitime3[0]>10)
  216.     {
  217.         //SCAN MAP
  218.         for (var y=0;y<maplength;y++)
  219.         {
  220.             for (var x=0;x<mapxlength;x++)
  221.             {
  222.                 for (var m=0;m<celllist.length;m++)
  223.                 {
  224.                     var tilelistonameo=Math.floor(3 * Math.random()) + 1;
  225.                     tilelistonameo=tilelistonameo-1;
  226.                     map[celllist[m]][celllist2[m]]=animation2[tilelistonameo];
  227.                 }
  228.             }
  229.         }
  230.     }
  231.     if (aitime3[0]>10)
  232.     {
  233.         aitime3[0]=0;
  234.     }
  235.     aitime3[0]++;
  236. }
  237. function generatemap()
  238. {
  239.     //SCAN MAP
  240.     for (var y=0;y<maplength;y++)
  241.     {
  242.         var mapx=[];
  243.         for (var x=0;x<mapxlength;x++)
  244.         {
  245.             var joejohn=Math.floor(100 * Math.random()) + 1;
  246.             var joejohn2=Math.floor(100 * Math.random()) + 1;
  247.             if (joejohn<=10)
  248.             {
  249.                 mapx.push(339+56);
  250.                 //CREAT THE LIST OF ANIMATED TILES WITH A 20% CHANCE ONLY ON THIS TILETYPE
  251.                 if (joejohn2<=20)
  252.                 {
  253.                     celllist.push(x);
  254.                     celllist2.push(y);
  255.                 }
  256.  
  257.             }
  258.             if (joejohn<=30)
  259.             {
  260.                 mapx.push(339+45);
  261.             }
  262.             else
  263.             {
  264.                 mapx.push(40);
  265.             }
  266.  
  267.         }
  268.         map.push(mapx);
  269.     }
  270. }
  271. function generatetiles()
  272. {
  273.     //FIND HOW MANY TILES PER WIDTH AND HEIGHT
  274.     tilemaptilesx=tilemap.height/tilemaptilesizex;
  275.     tilemaptilesy=tilemap.width/tilemaptilesizey;
  276.  
  277.     //FOR THE WIDTH AND HEIGHT IN TILES PUSH BACK A POSITION IN NORMAL CORDS AND A TYPE
  278.     for (var g=0;g<tilemaptilesy;g++)
  279.     {
  280.         for (var h=0;h<tilemaptilesx;h++)
  281.         {
  282.             typelist.push(typename);
  283.             typelist.push(h*tilemaptilesizex);
  284.             typelist.push(g*tilemaptilesizey);
  285.             typename++;
  286.         }
  287.     }
  288. }
  289. function placenpcs()
  290. {
  291.     //BUILD NPC LIST
  292.     for (var y=0;y<map.length+1;y++)
  293.     {
  294.         for (var x=0;x<mapxlength+1;x++)
  295.         {
  296.         //IF THIS IS A BOUNDRY PUSH BACK A NPC
  297.             if (x==0||y==0||y==map.length||x==mapxlength)
  298.             {
  299.                 var npclistxtempholder=[39,Math.abs(x*32),Math.abs(y*32),0,1,100,100,0,0,0,1,1];
  300.                 npclist.push(npclistxtempholder);
  301.             }
  302.             //ADD NPCS ON LINE X=20
  303.             else if (x>2&&y>2&&x<8&&y<8)
  304.             {
  305.                 var tilesettemp=[0,363,365,376,409,453,330,251];
  306.                 var randomthingtemptemp=Math.floor(7 * Math.random()) + 1;
  307.                 var randomthingtemptemp2=Math.floor(2 * Math.random()) + 1;
  308.                 var npclistxtempholder2=[tilesettemp[randomthingtemptemp],x*32,y*32,1,140,100,100,2,-2,randomthingtemptemp2,1,1];
  309.                 var npclist2=[npclistxtempholder2];
  310.                 npclist.push(npclist2[0]);
  311.             }
  312.         }
  313.     }
  314. }
  315. function healthbar()
  316. {
  317.     contextgame.fillStyle = "red";
  318.     contextgame.fillRect(0, 0, hp, 25);
  319. }
  320. function manabar()
  321. {
  322.     contextgame.fillStyle = "blue";
  323.     contextgame.fillRect(0, 25, mp, 25);
  324. }
  325. function displaymap()
  326. {
  327.     //SCAN MAP
  328.     for (var y=0;y<maplength;y++)
  329.     {
  330.         for (var x=0;x<mapxlength;x++)
  331.         {
  332.             //SCAN ALL TILE TYPES
  333.             for (var z=0;z<typelist.length;z=z+3)
  334.             {
  335.                 //IF THE VALUE IN MAP IS THE SAME AS THE CURRENT ITTERATION
  336.                 if (map[y][x]==typelist[z])
  337.                 {
  338.                     if (cameraposx+x*tilesizex>1024 || cameraposy+y*tilesizey>768)
  339.                     {
  340.                     }
  341.                     else if (cameraposy+y*tilesizey>768&&cameraposy+y*tilesizey>768)
  342.                     {
  343.                     }
  344.                     else
  345.                     {
  346.                         //THEN DRAW AT POSITION STORED +1 AND +2 FROM THE TYPELIST INDICATOR
  347.                         contextgame.drawImage(tilemap, typelist[z+2], typelist[z+1], 32, 32, cameraposx+x*tilesizex, cameraposy+y*tilesizey, zoomx, zoomy);
  348.                     }
  349.                     break;
  350.                 }
  351.             }
  352.         }
  353.     }
  354. }
  355. function displaynpcs()
  356. {
  357.     for (s=0;s<npclist.length;s++)
  358.     {
  359.         tileimage=npclist[s][0];
  360.         for (var z=0;z<typelist.length;z=z+3)
  361.         {
  362.             //IF THE VALUE IN MAP IS THE SAME AS THE CURRENT ITTERATION
  363.             if (tileimage==typelist[z])
  364.             {
  365.                 //THEN DRAW AT POSITION STORED +1 AND +2 FROM THE TYPELIST INDICATOR
  366.                 contextgame.drawImage(tilemap, typelist[z+2], typelist[z+1], 32, 32, cameraposx+npclist[s][1], cameraposy+npclist[s][2], zoomx, zoomy);
  367.                 break;
  368.  
  369.             }
  370.         }
  371.     }
  372. }
  373. function collsion()
  374. {
  375.     for (h=0;h<npclist.length;h++)
  376.     {
  377.         //IF WE ARE NOT LOOKING AT THE CURRENT NPC AGAIN
  378.         npcx=npclist[s][1];
  379.         npcy=npclist[s][2];
  380.         dnpcx=npclist[s][1]+32;
  381.         dnpcy=npclist[s][2]+32;
  382.  
  383.         mnpcx=npclist[h][1];
  384.         mnpcy=npclist[h][2];
  385.         mdnpcx=npclist[h][1]+32;
  386.         mdnpcy=npclist[h][2]+32;
  387.         if (npcx>mdnpcx||dnpcx<mnpcx||npcy>mdnpcy||dnpcy<mnpcy)
  388.         {
  389.         }
  390.         else
  391.         {
  392.             if (npclist[h][0]!=459&&npclist[s][3]==0&&s!=0)
  393.             {
  394.                 //DO COLLSION FOR WALL NPCS
  395.                 var distanceonx=npclist[h][1]-npclist[s][1];
  396.                 var distanceony=npclist[h][2]-npclist[s][2];
  397.  
  398.                 //FIND THE AMOUNT OF OVERLAP
  399.                 var distanceonxunit=distanceonx/Math.abs(distanceonx);
  400.                 var distanceonyunit=distanceony/Math.abs(distanceony);
  401.                 var overlapdistanceonx=32-Math.abs(distanceonx);
  402.                 var overlapdistanceony=32-Math.abs(distanceony);
  403.                 distanceonx=overlapdistanceonx*distanceonxunit;
  404.                 distanceony=overlapdistanceony*distanceonyunit;
  405.  
  406.                 //IF YOUR ON ZERO ON EITHER OF THE AXIS THEN SET IT TO THE LAST DIRECTION YOU WHERE HEADING BEFORE IT WAS ZERO
  407.  
  408.                 //FIND THE DOMINATING DIRECTION AND STEP OUT THAT DIRECTION
  409.                 if (Math.abs(distanceonx)<Math.abs(distanceony))
  410.                 {
  411.                     npclist[h][1]=npclist[h][1]+Math.ceil(distanceonx);
  412.                     npclist[h][7]=npclist[h][7]-npclist[h][7]*2;
  413.                 }
  414.                 else if (Math.abs(distanceony)<Math.abs(distanceonx))
  415.                 {
  416.                     npclist[h][2]=npclist[h][2]+Math.ceil(distanceony);
  417.                     npclist[h][8]=npclist[h][8]-npclist[h][8]*2;
  418.                 }
  419.             }
  420.             if (s==0&&npclist[h][3]==0)
  421.             {
  422.                 //DO COLLSION FOR WALL NPCS
  423.                 var distanceonx=npclist[s][1]-npclist[h][1];
  424.                 var distanceony=npclist[s][2]-npclist[h][2];
  425.  
  426.                 //FIND THE AMOUNT OF OVERLAP
  427.                 var distanceonxunit=distanceonx/Math.abs(distanceonx);
  428.                 var distanceonyunit=distanceony/Math.abs(distanceony);
  429.                 var overlapdistanceonx=32-Math.abs(distanceonx);
  430.                 var overlapdistanceony=32-Math.abs(distanceony);
  431.                 distanceonx=overlapdistanceonx*distanceonxunit;
  432.                 distanceony=overlapdistanceony*distanceonyunit;
  433.  
  434.                 //IF YOUR ON ZERO ON EITHER OF THE AXIS THEN SET IT TO THE LAST DIRECTION YOU WHERE HEADING BEFORE IT WAS ZERO
  435.  
  436.                 //FIND THE DOMINATING DIRECTION AND STEP OUT THAT DIRECTION
  437.                 if (Math.abs(distanceonx)<Math.abs(distanceony))
  438.                 {
  439.                     npclist[s][1]=npclist[s][1]+Math.ceil(distanceonx);
  440.                 }
  441.                 else if (Math.abs(distanceony)<Math.abs(distanceonx))
  442.                 {
  443.                     npclist[s][2]=npclist[s][2]+Math.ceil(distanceony);
  444.                 }
  445.                 if (npclist[s][4]==140 || npclist[s][4]==131)
  446.                 {
  447.                     hp--;
  448.                 }
  449.             }
  450.             else
  451.             {
  452.                 //IF A BADDIE NPC
  453.                 if (npclist[s][4]==140 || npclist[s][4]==131)
  454.                 {
  455.                     //IF THE SWORD OR A CAT NPC
  456.                     if (npclist[h][4]==74 || npclist[h][4]==198 || npclist[h][4]==199)
  457.                     {
  458.                         //TAKE DAMAGE
  459.                         npclist[s][5]=npclist[s][5]-100;
  460.                     }
  461.                     //DO COLLSION FOR WALL NPCS
  462.                     var distanceonx=npclist[s][1]-npclist[h][1];
  463.                     var distanceony=npclist[s][2]-npclist[h][2];
  464.  
  465.                     //FIND THE AMOUNT OF OVERLAP
  466.                     var distanceonxunit=distanceonx/Math.abs(distanceonx);
  467.                     var distanceonyunit=distanceony/Math.abs(distanceony);
  468.                     var overlapdistanceonx=32-Math.abs(distanceonx);
  469.                     var overlapdistanceony=32-Math.abs(distanceony);
  470.                     distanceonx=overlapdistanceonx*distanceonxunit;
  471.                     distanceony=overlapdistanceony*distanceonyunit;
  472.  
  473.                     //IF YOUR ON ZERO ON EITHER OF THE AXIS THEN SET IT TO THE LAST DIRECTION YOU WHERE HEADING BEFORE IT WAS ZERO
  474.  
  475.                     //FIND THE DOMINATING DIRECTION AND STEP OUT THAT DIRECTION
  476.                     if (Math.abs(distanceonx)<Math.abs(distanceony))
  477.                     {
  478.                         npclist[s][1]=npclist[s][1]+Math.ceil(distanceonx);
  479.                     }
  480.                     else if (Math.abs(distanceony)<Math.abs(distanceonx))
  481.                     {
  482.                         npclist[s][2]=npclist[s][2]+Math.ceil(distanceony);
  483.                     }
  484.                 }
  485.                 //IF A CAT NPC
  486.                 if (npclist[s][4]==198 || npclist[s][4]==199)
  487.                 {
  488.                     //IF A BADDIE
  489.                     if (npclist[h][4]==140 || npclist[s][h]==131)
  490.                     {
  491.                         //TAKE DAMAGE
  492.                         npclist[s][5]=npclist[s][5]-100;
  493.                     }
  494.                     //DO COLLSION FOR WALL NPCS
  495.                     var distanceonx=npclist[s][1]-npclist[h][1];
  496.                     var distanceony=npclist[s][2]-npclist[h][2];
  497.  
  498.                     //FIND THE AMOUNT OF OVERLAP
  499.                     var distanceonxunit=distanceonx/Math.abs(distanceonx);
  500.                     var distanceonyunit=distanceony/Math.abs(distanceony);
  501.                     var overlapdistanceonx=32-Math.abs(distanceonx);
  502.                     var overlapdistanceony=32-Math.abs(distanceony);
  503.                     distanceonx=overlapdistanceonx*distanceonxunit;
  504.                     distanceony=overlapdistanceony*distanceonyunit;
  505.  
  506.                     //IF YOUR ON ZERO ON EITHER OF THE AXIS THEN SET IT TO THE LAST DIRECTION YOU WHERE HEADING BEFORE IT WAS ZERO
  507.                     if (distanceonx==0)
  508.                     {
  509.                         var totakeout=npclist[s][10]/Math.abs(npclist[s][10]);
  510.                         npclist[s][1]=npclist[s][1]+totakeout;//THIS NEEDS TO HAVE THE PREVIOUS HEADING SO IT CAN SEND THEM BACK THE DIRECTION THEY CAME FROM
  511.                     }
  512.                     if (distanceony==0)
  513.                     {
  514.                         var totakeout=npclist[s][11]/Math.abs(npclist[s][11]);
  515.                         npclist[s][2]=npclist[s][2]+totakeout;
  516.                     }
  517.                     //FIND THE DOMINATING DIRECTION AND STEP OUT THAT DIRECTION
  518.                     if (Math.abs(distanceonx)<Math.abs(distanceony))
  519.                     {
  520.                         npclist[s][1]=npclist[s][1]+Math.ceil(distanceonx);
  521.                     }
  522.                     else if (Math.abs(distanceony)<Math.abs(distanceonx))
  523.                     {
  524.                         npclist[s][2]=npclist[s][2]+Math.ceil(distanceony);
  525.                     }
  526.                 }
  527.             }
  528.         }
  529.     }
  530. }
  531. function animation()
  532. {
  533.     //IF THE TIMER FOR THIS IS UP AND ITS A TYPE 150 ANIMATION
  534.     if (aitime[0]>2&&npclist[s][9]!=0&&s!=2)
  535.     {
  536.         var i=0;
  537.         if (npclist[s][9]==1)
  538.         {
  539.             i=0;
  540.         }
  541.         else if (npclist[s][9]==2)
  542.         {
  543.             i=4;
  544.         }
  545.         else if (npclist[s][9]==3)
  546.         {
  547.             i=8;
  548.         }
  549.         //IF THE CHARACTER IS NOT MOVING SWITCH TO ALT IF SET USING DIF LIST
  550.         if (npclist[s][7]==0&&npclist[s][8]==0)
  551.         {
  552.             if (npclist[s][9]==1)
  553.             {
  554.                 i=0;
  555.             }
  556.             else if (npclist[s][9]==2)
  557.             {
  558.                 i=1;
  559.             }
  560.             else if (npclist[s][9]==3)
  561.             {
  562.                 i=2;
  563.             }
  564.         }
  565.         //DIR - + LEFT DOWN
  566.         if (npclist[s][7]<0 && npclist[s][8]>0)
  567.         {
  568.         npclist[s][0]=characterdirani[i+1][tomsfrog2];
  569.         }
  570.         //DIR - - LEFT UP
  571.         else if (npclist[s][7]<0 && npclist[s][8]<0)
  572.         {
  573.         npclist[s][0]=characterdirani[i][tomsfrog2];
  574.         }
  575.         //DIR + + RIGHT DOWN
  576.         else if (npclist[s][7]>0 && npclist[s][8]>0)
  577.         {
  578.         npclist[s][0]=characterdirani[i+1][tomsfrog2];
  579.         }
  580.         //DIR + - RIGHT UP
  581.         else if (npclist[s][7]>0 && npclist[s][8]<0)
  582.         {
  583.         npclist[s][0]=characterdirani[i][tomsfrog2];
  584.         }
  585.         //DIR 0 - UP
  586.         else if (npclist[s][7]==0&&npclist[s][8]<0)
  587.         {
  588.         npclist[s][0]=characterdirani[i][tomsfrog2];
  589.         }
  590.         //DIR 0 + DOWN
  591.         else if (npclist[s][7]==0&&npclist[s][8]>0)
  592.         {
  593.         npclist[s][0]=characterdirani[i+1][tomsfrog2];
  594.         }
  595.         //DIR + 0 RIGHT
  596.         else if (npclist[s][7]>0&&npclist[s][8]==0)
  597.         {
  598.         npclist[s][0]=characterdirani[i+2][tomsfrog2];
  599.         }
  600.         //DIR - 0 LEFT
  601.         else if (npclist[s][7]<0&&npclist[s][8]==0)
  602.         {
  603.         npclist[s][0]=characterdirani[i+3][tomsfrog2];
  604.         }
  605.         //DIR 0 0 NOT MOVING
  606.         else if (npclist[s][7]==0 && npclist[s][8]==0)
  607.         {
  608.         npclist[s][0]=animationcharidl[i][tomsfrog2];
  609.         }
  610.         if (time-frogtime>30)
  611.         {
  612.             tomsfrog2++;
  613.             frogtime=time;
  614.         }
  615.         if (tomsfrog2>=animation1.length)
  616.         {
  617.             tomsfrog2=0;
  618.         }
  619.     }
  620. }
  621. function ai001()
  622. {
  623.     //AI FOR WHEN NOT NEAR THE PLAYER
  624.     if (npclist[s][4]==140)
  625.     {
  626.         var distancetoplayerx=npclist[0][1]-npclist[s][1];
  627.         var distancetoplayery=npclist[0][2]-npclist[s][2];
  628.         if (Math.abs(distancetoplayerx)>160&&Math.abs(distancetoplayery)>160)
  629.         {
  630.             if (cycletrack>100)
  631.             {
  632.                 var speedaddonx=Math.floor(3 * Math.random()) + 1;
  633.                 var speedaddony=Math.floor(3 * Math.random()) + 1;
  634.                 var negativeorno=Math.floor(2 * Math.random()) + 1;
  635.                 if (negativeorno==1)
  636.                 {
  637.                     npclist[s][7]=speedaddonx;
  638.                     npclist[s][8]=speedaddony;
  639.                 }
  640.                 else if (negativeorno==2)
  641.                 {
  642.                     speedaddonx=speedaddonx-speedaddonx*2;
  643.                     speedaddony=speedaddony-speedaddony*2;
  644.                     npclist[s][7]=speedaddonx;
  645.                     npclist[s][8]=speedaddony;
  646.                 }
  647.             }
  648.  
  649.         }
  650.         else
  651.         {
  652.             npclist[s][4]=131;
  653.         }
  654.         cycletrack++;
  655.         if (cycletrack>100)
  656.         {
  657.             cycletrack=0;
  658.         }
  659.     }
  660.     //AI FOR WHEN NEAR THE PLAYER
  661.     else if(npclist[s][4]==131)
  662.     {
  663.         if (cycletrackr>60)
  664.         {
  665.             for (h=0;h<npclist.length;h++)
  666.             {
  667.                 if(npclist[h][4]==198||npclist[h][4]==199||npclist[h][4]==999)
  668.                 {
  669.                     var distancetoplayerx=npclist[h][1]-npclist[s][1];
  670.                     var distancetoplayery=npclist[h][2]-npclist[s][2];
  671.                     if (Math.abs(distancetoplayerx)<160&&Math.abs(distancetoplayery)<160)
  672.                     {
  673.                         if (distancetoplayerx!=0&&distancetoplayery!=0)
  674.                         {
  675.                             distancetoplayerx=distancetoplayerx/Math.abs(distancetoplayerx);
  676.                             distancetoplayery=distancetoplayery/Math.abs(distancetoplayery);
  677.                             npclist[s][7]=distancetoplayerx;
  678.                             npclist[s][8]=distancetoplayery;
  679.                         }
  680.                         else
  681.                         {
  682.                             npclist[s][4]=140;
  683.                         }
  684.                     }
  685.                     else
  686.                     {
  687.                         npclist[s][4]=140;
  688.                     }
  689.                 }
  690.             }
  691.         }
  692.         cycletrackr++;
  693.         if (cycletrackr>100)
  694.         {
  695.             cycletrackr=0;
  696.         }
  697.     }
  698.     else if(npclist[s][4]==198)
  699.     {
  700.         var distancetoplayerx=npclist[0][1]-npclist[s][1];
  701.         var distancetoplayery=npclist[0][2]-npclist[s][2];
  702.         if (Math.abs(distancetoplayerx)>160&&Math.abs(distancetoplayery)>160)
  703.         {
  704.             if (cycletrack2>100)
  705.             {
  706.                 var speedaddonx=Math.floor(3 * Math.random()) + 1;
  707.                 var speedaddony=Math.floor(3 * Math.random()) + 1;
  708.                 var negativeorno=Math.floor(2 * Math.random()) + 1;
  709.                 if (negativeorno==1)
  710.                 {
  711.                     npclist[s][7]=speedaddonx;
  712.                     npclist[s][8]=speedaddony;
  713.                 }
  714.                 else if (negativeorno==2)
  715.                 {
  716.                     speedaddonx=speedaddonx-speedaddonx*2;
  717.                     speedaddony=speedaddony-speedaddony*2;
  718.                     npclist[s][7]=speedaddonx;
  719.                     npclist[s][8]=speedaddony;
  720.                 }
  721.             }
  722.         }
  723.         else
  724.         {
  725.             npclist[s][4]=199;
  726.         }
  727.         cycletrack2++;
  728.         if (cycletrack2>100)
  729.         {
  730.             cycletrack2=0;
  731.         }
  732.     }
  733.     else if(npclist[s][4]==199)
  734.     {
  735.         for (h=0;h<npclist.length;h++)
  736.         {
  737.             if(npclist[h][4]==140||npclist[h][4]==131)
  738.             {
  739.                 var distancetoplayerx=npclist[h][1]-npclist[s][1];
  740.                 var distancetoplayery=npclist[h][2]-npclist[s][2];
  741.                 if (Math.abs(distancetoplayerx)<160&&Math.abs(distancetoplayery)<160)
  742.                 {
  743.                     if (distancetoplayerx!=0&&distancetoplayery!=0)
  744.                     {
  745.                         distancetoplayerx=distancetoplayerx/Math.abs(distancetoplayerx);
  746.                         distancetoplayery=distancetoplayery/Math.abs(distancetoplayery);
  747.                         npclist[s][7]=distancetoplayerx;
  748.                         npclist[s][8]=distancetoplayery;
  749.                     }
  750.                     else
  751.                     {
  752.                         npclist[s][4]=198;
  753.                     }
  754.                 }
  755.                 else
  756.                 {
  757.                     npclist[s][4]=198;
  758.                 }
  759.             }
  760.         }
  761.     }
  762. }
  763. function resettimers()
  764. {
  765.     //RESET THE TIMER WHEN ITS OVER 100
  766.     if (aitime[0]>100)
  767.     {
  768.         aitime[0]=0;
  769.     }
  770.     aitime[0]++;
  771.     //RESET THE TIMER WHEN ITS OVER 100
  772.     if (aitime2[0]>100)
  773.     {
  774.         aitime2[0]=0;
  775.     }
  776.     aitime2[0]++;
  777. }
  778. function updatenpcs()
  779. {
  780.     for (s=0;s<npclist.length;s++)
  781.     {
  782.         collsion();
  783.         if (s==0)
  784.         {
  785.             //UPDATE THE CAMERA POSITION AND THE NPCS POSITION
  786.             cameraposx=cameraposx-npclist[s][7];
  787.             cameraposy=cameraposy-npclist[s][8];
  788.             npclist[s][1]=npclist[s][1]+npclist[s][7];
  789.             npclist[s][2]=npclist[s][2]+npclist[s][8];
  790.             //IF THE SWORD HAS BEEN PULLED OUT
  791.             if (swordtoggle==1)
  792.             {
  793.                 //IF THE DIR IS (- +) (LEFT DOWN)
  794.                 if (npclist[s][7]<0 && npclist[s][8]>0)
  795.                 {
  796.                     //SET THE PICTURE OF THE TOOL AND THE POSTION RELATIVE TO THE PLAYER
  797.                     npclist[1][1]=npclist[s][1];
  798.                     npclist[1][2]=npclist[s][2]+33;
  799.                     npclist[1][0]=467;
  800.                     lastone=1;
  801.                 }
  802.                 //DIR - - LEFT UP
  803.                 else if (npclist[s][7]<0 && npclist[s][8]<0)
  804.                 {
  805.                     npclist[1][1]=npclist[s][1];
  806.                     npclist[1][2]=npclist[s][2]-33;
  807.                     npclist[1][0]=466;
  808.                     lastone=2;
  809.                 }
  810.                 //DIR + + RIGHT DOWN
  811.                 else if (npclist[s][7]>0 && npclist[s][8]>0)
  812.                 {
  813.                     npclist[1][1]=npclist[s][1];
  814.                     npclist[1][2]=npclist[s][2]+33;
  815.                     npclist[1][0]=467;
  816.                     lastone=3;
  817.                 }
  818.                 //DIR + - RIGHT UP
  819.                 else if (npclist[s][7]>0 && npclist[s][8]<0)
  820.                 {
  821.                     npclist[1][1]=npclist[s][1];
  822.                     npclist[1][2]=npclist[s][2]-33;
  823.                     npclist[1][0]=466;
  824.                     lastone=4;
  825.                 }
  826.                 //DIR 0 - UP
  827.                 else if (npclist[s][7]==0&&npclist[s][8]<0)
  828.                 {
  829.                     npclist[1][1]=npclist[s][1];
  830.                     npclist[1][2]=npclist[s][2]-33;
  831.                     npclist[1][0]=466;
  832.                     lastone=5;
  833.                 }
  834.                 //DIR 0 + DOWN
  835.                 else if (npclist[s][7]==0&&npclist[s][8]>0)
  836.                 {
  837.                     npclist[1][1]=npclist[s][1];
  838.                     npclist[1][2]=npclist[s][2]+33;
  839.                     npclist[1][0]=467;
  840.                     lastone=6;
  841.                 }
  842.                 //DIR + 0 RIGHT
  843.                 else if (npclist[s][7]>0&&npclist[s][8]==0)
  844.                 {
  845.                     npclist[1][1]=npclist[s][1]+33;
  846.                     npclist[1][2]=npclist[s][2];
  847.                     npclist[1][0]=469;
  848.                     lastone=7;
  849.                 }
  850.                 //DIR - 0 LEFT
  851.                 else if (npclist[s][7]<0&&npclist[s][8]==0)
  852.                 {
  853.                     npclist[1][1]=npclist[s][1]-33;
  854.                     npclist[1][2]=npclist[s][2];
  855.                     npclist[1][0]=468;
  856.                     lastone=8;
  857.                 }
  858.                 //DIR 0 0 NOT MOVING DEFAULT TO DOWN NEEDS TO BE SET TO LAST DIRECTION MOVED
  859.                 else if (npclist[s][7]==0 && npclist[s][8]==0)
  860.                 {
  861.                     if (lastone==1)
  862.                     {
  863.                         npclist[1][1]=npclist[s][1];
  864.                         npclist[1][2]=npclist[s][2]+33;
  865.                         npclist[1][0]=467;
  866.                     }
  867.                     else if (lastone==2)
  868.                     {
  869.                         npclist[1][1]=npclist[s][1];
  870.                         npclist[1][2]=npclist[s][2]-33;
  871.                         npclist[1][0]=466;
  872.                     }
  873.                     else if (lastone==3)
  874.                     {
  875.                         npclist[1][1]=npclist[s][1];
  876.                         npclist[1][2]=npclist[s][2]+33;
  877.                         npclist[1][0]=467;
  878.                     }
  879.                     else if (lastone==4)
  880.                     {
  881.                         npclist[1][1]=npclist[s][1];
  882.                         npclist[1][2]=npclist[s][2]-33;
  883.                         npclist[1][0]=466;
  884.                     }
  885.                     else if (lastone==5)
  886.                     {
  887.                         npclist[1][1]=npclist[s][1];
  888.                         npclist[1][2]=npclist[s][2]-33;
  889.                         npclist[1][0]=466;
  890.                     }
  891.                     else if (lastone==6)
  892.                     {
  893.                         npclist[1][1]=npclist[s][1];
  894.                         npclist[1][2]=npclist[s][2]+33;
  895.                         npclist[1][0]=467;
  896.                     }
  897.                     else if (lastone==7)
  898.                     {
  899.                         npclist[1][1]=npclist[s][1]+33;
  900.                         npclist[1][2]=npclist[s][2];
  901.                         npclist[1][0]=469;
  902.                     }
  903.                     else if (lastone==8)
  904.                     {
  905.                         npclist[1][1]=npclist[s][1]-33;
  906.                         npclist[1][2]=npclist[s][2];
  907.                         npclist[1][0]=468;
  908.                     }
  909.                 }
  910.             }
  911.             //THIS IS THE RAKE TOOL
  912.             if (swordtoggle2==1)
  913.             {
  914.                 //DIR - + LEFT DOWN
  915.                 if (npclist[s][7]<0 && npclist[s][8]>0)
  916.                 {
  917.                     //SET THE PICTURE OF THE TOOL AND THE POSTION RELATIVE TO THE PLAYER
  918.                     //IN THE RAKE YOU ALSO SET THE TILE THE PLAYER IS CURRENTLY ON
  919.                     npclist[2][1]=npclist[s][1];
  920.                     npclist[2][2]=npclist[s][2]+33;
  921.                     tileposx=Math.ceil(npclist[2][1]/32);
  922.                     tileposy=Math.ceil(npclist[2][2]/32);
  923.                     if (tileposx>0&&tileposy>0&&map[tileposy][tileposx]!=51)
  924.                     {
  925.                         var holditman=map[tileposy][tileposx];
  926.                         invlist[holditman]++;
  927.                         map[tileposy][tileposx]=51;
  928.                     }
  929.                     npclist[2][0]=454;
  930.                 }
  931.                 //DIR - - LEFT UP
  932.                 else if (npclist[s][7]<0 && npclist[s][8]<0)
  933.                 {
  934.                     npclist[2][1]=npclist[s][1];
  935.                     npclist[2][2]=npclist[s][2]-33;
  936.                     tileposx=Math.ceil(npclist[2][1]/32);
  937.                     tileposy=Math.ceil(npclist[2][2]/32);
  938.                     if (tileposx>0&&tileposy>0)
  939.                     {
  940.                         var holditman=map[tileposy][tileposx];
  941.                         invlist[holditman]++;
  942.                         map[tileposy][tileposx]=51;
  943.                     }
  944.                     npclist[2][0]=443;
  945.                 }
  946.                 //DIR + + RIGHT DOWN
  947.                 else if (npclist[s][7]>0 && npclist[s][8]>0)
  948.                 {
  949.                     npclist[2][1]=npclist[s][1];
  950.                     npclist[2][2]=npclist[s][2]+33;
  951.                     tileposx=Math.ceil(npclist[2][1]/32);
  952.                     tileposy=Math.ceil(npclist[2][2]/32);
  953.                     if (tileposx>0&&tileposy>0)
  954.                     {
  955.                         var holditman=map[tileposy][tileposx];
  956.                         invlist[holditman]++;
  957.                         map[tileposy][tileposx]=51;
  958.                     }
  959.                     npclist[2][0]=454;
  960.                 }
  961.                 //DIR + - RIGHT UP
  962.                 else if (npclist[s][7]>0 && npclist[s][8]<0)
  963.                 {
  964.                     npclist[2][1]=npclist[s][1];
  965.                     npclist[2][2]=npclist[s][2]-33;
  966.                     tileposx=Math.ceil(npclist[2][1]/32);
  967.                     tileposy=Math.ceil(npclist[2][2]/32);
  968.                     if (tileposx>0&&tileposy>0)
  969.                     {
  970.                         var holditman=map[tileposy][tileposx];
  971.                         invlist[holditman]++;
  972.                         map[tileposy][tileposx]=51;
  973.                     }
  974.                     npclist[2][0]=443;
  975.                 }
  976.                 //DIR 0 - UP
  977.                 else if (npclist[s][7]==0&&npclist[s][8]<0)
  978.                 {
  979.                     npclist[2][1]=npclist[s][1];
  980.                     npclist[2][2]=npclist[s][2]-33;
  981.                     tileposx=Math.ceil(npclist[2][1]/32);
  982.                     tileposy=Math.ceil(npclist[2][2]/32);
  983.                     if (tileposx>0&&tileposy>0)
  984.                     {
  985.                         var holditman=map[tileposy][tileposx];
  986.                         invlist[holditman]++;
  987.                         map[tileposy][tileposx]=51;
  988.                     }
  989.                     npclist[2][0]=443;
  990.                 }
  991.                 //DIR 0 + DOWN
  992.                 else if (npclist[s][7]==0&&npclist[s][8]>0)
  993.                 {
  994.                     npclist[2][1]=npclist[s][1];
  995.                     npclist[2][2]=npclist[s][2]+33;
  996.                     tileposx=Math.ceil(npclist[2][1]/32);
  997.                     tileposy=Math.ceil(npclist[2][2]/32);
  998.                     if (tileposx>0&&tileposy>0)
  999.                     {
  1000.                         var holditman=map[tileposy][tileposx];
  1001.                         invlist[holditman]++;
  1002.                         map[tileposy][tileposx]=51;
  1003.                     }
  1004.                     npclist[2][0]=454;
  1005.                 }
  1006.                 //DIR + 0 RIGHT
  1007.                 else if (npclist[s][7]>0&&npclist[s][8]==0)
  1008.                 {
  1009.                     npclist[2][1]=npclist[s][1]+33;
  1010.                     npclist[2][2]=npclist[s][2];
  1011.                     tileposx=Math.ceil(npclist[2][1]/32);
  1012.                     tileposy=Math.ceil(npclist[2][2]/32);
  1013.                     if (tileposx>0&&tileposy>0)
  1014.                     {
  1015.                         var holditman=map[tileposy][tileposx];
  1016.                         invlist[holditman]++;
  1017.                         map[tileposy][tileposx]=51;
  1018.                     }
  1019.                     npclist[2][0]=455;
  1020.                 }
  1021.                 //DIR - 0 LEFT
  1022.                 else if (npclist[s][7]<0&&npclist[s][8]==0)
  1023.                 {
  1024.                     npclist[2][1]=npclist[s][1]-33;
  1025.                     npclist[2][2]=npclist[s][2];
  1026.                     tileposx=Math.ceil(npclist[2][1]/32);
  1027.                     tileposy=Math.ceil(npclist[2][2]/32);
  1028.                     if (tileposx>0&&tileposy>0)
  1029.                     {
  1030.                         var holditman=map[tileposy][tileposx];
  1031.                         invlist[holditman]++;
  1032.                         map[tileposy][tileposx]=51;
  1033.                     }
  1034.                     npclist[2][0]=444;
  1035.                 }
  1036.                 //DIR 0 0 NOT MOVING
  1037.                 else if (npclist[s][7]==0 && npclist[s][8]==0)
  1038.                 {
  1039.                     npclist[2][1]=npclist[s][1];
  1040.                     npclist[2][2]=npclist[s][2]+33;
  1041.                     tileposx=Math.ceil(npclist[2][1]/32);
  1042.                     tileposy=Math.ceil(npclist[2][2]/32);
  1043.                     if (tileposx>0&&tileposy>0)
  1044.                     {
  1045.                         var holditman=map[tileposy][tileposx];
  1046.                         invlist[holditman]++;
  1047.                         map[tileposy][tileposx]=51;
  1048.                     }
  1049.                     npclist[2][0]=454;
  1050.                 }
  1051.             }
  1052.             animation();
  1053.         }
  1054.         else if (s!=1)
  1055.         {
  1056.             animation();
  1057.             ai001();
  1058.             npclist[s][1]=npclist[s][1]+(time-b[0])*npclist[s][7];
  1059.             npclist[s][2]=npclist[s][2]+(time-b[0])*npclist[s][8];
  1060.         }
  1061.         //IF NO HP THEN MAKE THE CHAR PASSABLE
  1062.         if (npclist[s][5]<0&&npclist[s][3]==1)
  1063.         {
  1064.             npclist[s][3]=2;
  1065.             npclist[s][4]=0;
  1066.             npclist[s][7]=0;
  1067.             npclist[s][8]=0;
  1068.             npclist[s][9]=0;
  1069.             npclist[s][0]=12;
  1070.             tileposx=Math.ceil(npclist[s][1]/32);
  1071.             tileposy=Math.ceil(npclist[s][2]/32);
  1072.             if (tileposx>0&&tileposy>0)
  1073.             {
  1074.                 map[tileposy][tileposx]=459;
  1075.             }
  1076.         }
  1077.         if (npclist[s][7]!=0)
  1078.         {
  1079.             npclist[s][10]=npclist[s][7];
  1080.         }
  1081.         if (npclist[s][8]!=0)
  1082.         {
  1083.             npclist[s][11]=npclist[s][8];
  1084.         }
  1085.     }
  1086.     resettimers();
  1087. }
  1088.  
  1089. function keyboardhan(event)
  1090. {
  1091.     if (event.keyCode=="37")
  1092.     {
  1093.         if (key1!=1)
  1094.         {
  1095.             key1=1;
  1096.             npclist[0][7]=npclist[0][7]-charspeed;
  1097.         }
  1098.     }
  1099.     if (event.keyCode=="38")
  1100.     {
  1101.         if (key2!=1)
  1102.         {
  1103.             key2=1;
  1104.             npclist[0][8]=npclist[0][8]-charspeed;
  1105.         }
  1106.     }
  1107.     if (event.keyCode=="39")
  1108.     {
  1109.         if (key3!=1)
  1110.         {
  1111.             key3=1;
  1112.             npclist[0][7]=npclist[0][7]+charspeed;
  1113.         }
  1114.     }
  1115.     if (event.keyCode=="40")
  1116.     {
  1117.         if (key4!=1)
  1118.         {
  1119.             key4=1;
  1120.             npclist[0][8]=npclist[0][8]+charspeed;
  1121.         }
  1122.     }
  1123.     if (event.keyCode=="87")
  1124.     {
  1125.         if (key5!=1)
  1126.         {
  1127.             key5=1;
  1128.             if (swordtoggle==1)
  1129.             {
  1130.                 npclist[1][0]=12;
  1131.                 swordtoggle=0;
  1132.                 npclist[1][3]=2;
  1133.             }
  1134.             else if (swordtoggle==0)
  1135.             {
  1136.                 npclist[1][3]=2;
  1137.                 swordtoggle=1;
  1138.             }
  1139.         }
  1140.     }
  1141.     if (event.keyCode=="81")
  1142.     {
  1143.         if (key6!=1)
  1144.         {
  1145.             key6=1;
  1146.             if (swordtoggle2==1)
  1147.             {
  1148.                 npclist[2][0]=12;
  1149.                 swordtoggle2=0;
  1150.                 npclist[2][3]=2;
  1151.             }
  1152.             else if (swordtoggle2==0)
  1153.             {
  1154.                 npclist[2][3]=2;
  1155.                 swordtoggle2=1;
  1156.             }
  1157.         }
  1158.     }
  1159.     if (event.keyCode=="16")
  1160.     {
  1161.         if (key7!=1)
  1162.         {
  1163.             key7=1;
  1164.             if (npclist[0][7]!=0&&npclist[0][8]!=0)
  1165.             {
  1166.                 npclist[0][7]=npclist[0][7]/Math.abs(npclist[0][7]);
  1167.                 npclist[0][8]=npclist[0][8]/Math.abs(npclist[0][8]);
  1168.                 npclist[0][7]=npclist[0][7]*5;
  1169.                 npclist[0][8]=npclist[0][8]*5;
  1170.             }
  1171.             else if (npclist[0][7]!=0&&npclist[0][8]==0)
  1172.             {
  1173.                 npclist[0][7]=npclist[0][7]/Math.abs(npclist[0][7]);
  1174.                 npclist[0][7]=npclist[0][7]*5;
  1175.             }
  1176.             else if (npclist[0][7]==0&&npclist[0][8]!=0)
  1177.             {
  1178.                 npclist[0][8]=npclist[0][8]/Math.abs(npclist[0][8]);
  1179.                 npclist[0][8]=npclist[0][8]*5;
  1180.             }
  1181.         }
  1182.     }
  1183.     if (event.keyCode=="66")
  1184.     {
  1185.         selection=selection+3;
  1186.     }
  1187.     if (event.keyCode=="86")
  1188.     {
  1189.         selection=selection-3;
  1190.     }
  1191.     if (event.keyCode=="73")
  1192.     {
  1193.         if (key9!=1)
  1194.         {
  1195.             key9=1;
  1196.             if (swordtoggle4==1)
  1197.             {
  1198.                 swordtoggle4=0;
  1199.             }
  1200.             else if (swordtoggle4==0)
  1201.             {
  1202.                 swordtoggle4=1;
  1203.             }
  1204.         }
  1205.     }
  1206.     if (event.keyCode=="69")
  1207.     {
  1208.         if (npclist[0][7]<0 && npclist[0][8]>0)
  1209.         {var npclistxtemp=[249,npclist[0][1]-32,npclist[0][2]+32,1,1,100,100,0,0,0];npclist.push(npclistxtemp);}
  1210.         else if (npclist[0][7]==0&&npclist[0][8]<0)
  1211.         {var npclistxtemp=[249,npclist[0][1],npclist[0][2]-32,1,1,100,100,0,0,0];npclist.push(npclistxtemp);}
  1212.         else if (npclist[0][7]==0&&npclist[0][8]>0)
  1213.         {var npclistxtemp=[249,npclist[0][1],npclist[0][2]+32,1,1,100,100,0,0,0];npclist.push(npclistxtemp);}
  1214.         else if (npclist[0][7]>0&&npclist[0][8]==0)
  1215.         {var npclistxtemp=[249,npclist[0][1]+32,npclist[0][2],1,1,100,100,0,0,0];npclist.push(npclistxtemp);}
  1216.         else if (npclist[0][7]<0&&npclist[0][8]==0)
  1217.         {var npclistxtemp=[249,npclist[0][1]-32,npclist[0][2],1,1,100,100,0,0,0];npclist.push(npclistxtemp);}
  1218.         else if (npclist[0][7]<0 && npclist[0][8]<0)
  1219.         {var npclistxtemp=[249,npclist[0][1]-32,npclist[0][2]-32,1,1,100,100,0,0,0];npclist.push(npclistxtemp);}
  1220.         else if (npclist[0][7]>0 && npclist[0][8]>0)
  1221.         {var npclistxtemp=[249,npclist[0][1]+32,npclist[0][2]+32,1,1,100,100,0,0,0];npclist.push(npclistxtemp);}
  1222.         else if (npclist[0][7]>0 && npclist[0][8]<0)
  1223.         {var npclistxtemp=[249,npclist[0][1]+32,npclist[0][2]-32,1,1,100,100,0,0,0];npclist.push(npclistxtemp);}
  1224.         else if (npclist[0][7]==0 && npclist[0][8]==0)
  1225.         {var npclistxtemp=[249,npclist[0][1],npclist[0][2]+32,1,1,100,100,0,0,0];npclist.push(npclistxtemp);}
  1226.         mp=mp-5;
  1227.     }
  1228.     if (event.keyCode=="82")
  1229.     {
  1230.         if (npclist[0][7]<0 && npclist[0][8]>0)
  1231.         {var npclistxtemp=[238,npclist[0][1]-32,npclist[0][2]+32,0,0,100,100,0,0,0];npclist.push(npclistxtemp);}
  1232.         else if (npclist[0][7]==0&&npclist[0][8]<0)
  1233.         {var npclistxtemp=[238,npclist[0][1],npclist[0][2]-32,0,0,1,100,0,0,0];npclist.push(npclistxtemp);}
  1234.         else if (npclist[0][7]==0&&npclist[0][8]>0)
  1235.         {var npclistxtemp=[238,npclist[0][1],npclist[0][2]+32,0,0,100,100,0,0,0];npclist.push(npclistxtemp);}
  1236.         else if (npclist[0][7]>0&&npclist[0][8]==0)
  1237.         {var npclistxtemp=[238,npclist[0][1]+32,npclist[0][2],0,0,100,100,0,0,0];npclist.push(npclistxtemp);}
  1238.         else if (npclist[0][7]<0&&npclist[0][8]==0)
  1239.         {var npclistxtemp=[238,npclist[0][1]-32,npclist[0][2],0,0,100,100,0,0,0];npclist.push(npclistxtemp);}
  1240.         else if (npclist[0][7]<0 && npclist[0][8]<0)
  1241.         {var npclistxtemp=[238,npclist[0][1]-32,npclist[0][2]-32,0,0,100,100,0,0,0];npclist.push(npclistxtemp);}
  1242.         else if (npclist[0][7]>0 && npclist[0][8]>0)
  1243.         {var npclistxtemp=[238,npclist[0][1]+32,npclist[0][2]+32,0,0,100,100,0,0,0];npclist.push(npclistxtemp);}
  1244.         else if (npclist[0][7]>0 && npclist[0][8]<0)
  1245.         {var npclistxtemp=[238,npclist[0][1]+32,npclist[0][2]-32,0,0,100,100,0,0,0];npclist.push(npclistxtemp);}
  1246.         else if (npclist[0][7]==0 && npclist[0][8]==0)
  1247.         {var npclistxtemp=[238,npclist[0][1],npclist[0][2]+32,0,0,100,100,0,0,0];npclist.push(npclistxtemp);}
  1248.         mp=mp-10;
  1249.     }
  1250.     if (event.keyCode=="84")
  1251.     {
  1252.         if (npclist[0][7]<0 && npclist[0][8]>0)
  1253.         {var npclistxtemp=[237,npclist[0][1]-32,npclist[0][2]+32,1,198,100,100,1,1,0];npclist.push(npclistxtemp);}
  1254.         else if (npclist[0][7]==0&&npclist[0][8]<0)
  1255.         {var npclistxtemp=[237,npclist[0][1],npclist[0][2]-32,1,198,100,100,1,1,0];npclist.push(npclistxtemp);}
  1256.         else if (npclist[0][7]==0&&npclist[0][8]>0)
  1257.         {var npclistxtemp=[237,npclist[0][1],npclist[0][2]+32,1,198,100,100,1,1,0];npclist.push(npclistxtemp);}
  1258.         else if (npclist[0][7]>0&&npclist[0][8]==0)
  1259.         {var npclistxtemp=[237,npclist[0][1]+32,npclist[0][2],1,198,100,100,1,1,0];npclist.push(npclistxtemp);}
  1260.         else if (npclist[0][7]<0&&npclist[0][8]==0)
  1261.         {var npclistxtemp=[237,npclist[0][1]-32,npclist[0][2],1,198,100,100,1,1,0];npclist.push(npclistxtemp);}
  1262.         else if (npclist[0][7]<0 && npclist[0][8]<0)
  1263.         {var npclistxtemp=[237,npclist[0][1]-32,npclist[0][2]-32,1,198,100,100,1,1,0];npclist.push(npclistxtemp);}
  1264.         else if (npclist[0][7]>0 && npclist[0][8]>0)
  1265.         {var npclistxtemp=[237,npclist[0][1]+32,npclist[0][2]+32,1,198,100,100,1,1,0];npclist.push(npclistxtemp);}
  1266.         else if (npclist[0][7]>0 && npclist[0][8]<0)
  1267.         {var npclistxtemp=[237,npclist[0][1]+32,npclist[0][2]-32,1,198,100,100,1,1,0];npclist.push(npclistxtemp);}
  1268.         else if (npclist[0][7]==0 && npclist[0][8]==0)
  1269.         {var npclistxtemp=[237,npclist[0][1],npclist[0][2]+32,1,198,100,100,1,1,0];npclist.push(npclistxtemp);}
  1270.         helpers++;
  1271.         mp=mp-20;
  1272.     }
  1273. }
  1274. function keyboardhan2(event)
  1275. {
  1276.     if (event.keyCode=="37")
  1277.     {
  1278.         key1=0;
  1279.         npclist[0][7]=0;
  1280.     }
  1281.     if (event.keyCode=="38")
  1282.     {
  1283.         key2=0;
  1284.         npclist[0][8]=0;
  1285.     }
  1286.     if (event.keyCode=="39")
  1287.     {
  1288.         key3=0;
  1289.         npclist[0][7]=0;
  1290.     }
  1291.     if (event.keyCode=="40")
  1292.     {
  1293.         key4=0;
  1294.         npclist[0][8]=0;
  1295.     }
  1296.     if (event.keyCode=="87")
  1297.     {
  1298.         key5=0;
  1299.     }
  1300.     if (event.keyCode=="81")
  1301.     {
  1302.         key6=0;
  1303.     }
  1304.     if (event.keyCode=="16")
  1305.     {
  1306.         key7=0;
  1307.     }
  1308.     if (event.keyCode=="73")
  1309.     {
  1310.         key9=0;
  1311.     }
  1312. }
  1313.  
Expand to see the code.


And now a copy of the images folder from the project so you have the graphics.

http://s000.tinyupload.com/?file_id=78761715115048132625

And to describe the project folder structure for linking

MAIN FOLDER
|
V
images->rpggame->splash1.png tilemap.png tilemawp.png
scripts->script14.js
games->games11.html

Next post I will drop some of the landscape engines I made in ogre3d and ogrepy3d

Original thread: https://www.hbgames.org/forums/viewtopic.php?t=78972