Advertisement
Guest User

platforming game v7

a guest
Jan 21st, 2015
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 49.92 KB | None | 0 0
  1. <!doctype html>
  2. <head>
  3. <title>Platformer</title>
  4. <meta charset="UTF-8">
  5. <link rel="stylesheet" href="styles.css">
  6. <link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
  7. <link rel="icon" href="/favicon.ico" type="image/x-icon">
  8. </head>
  9.  
  10. <body>
  11.  
  12. <h1>Platformer</h1>
  13.  
  14. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
  15. <script type="text/javascript">
  16.    
  17. // canvas
  18. var canvas = document.createElement("canvas");
  19. var ctx = canvas.getContext("2d");
  20. canvas.width = 700;
  21. canvas.height = 450;
  22. document.body.appendChild(canvas);
  23.  
  24. canvas.addEventListener("mousedown", getClickPosition, false);
  25.  
  26. var framecount = 0;
  27. var activecount = 0; //wie framecount aber wird für fadein fadeout zwecke benutzt
  28. var debug=true; //enable for debug stats
  29. var insideloadingzone=false;
  30. var currentroom=0;
  31. var nextroom=0;  //this is used to save room name of loadingzone you touch
  32. var deathtimer=0;
  33. var dmgtimer=0; //set to 5 when taking damage, used to draw graphical effect
  34. var timeattack=true;
  35. var gamestate=0; // 0 = loading   1 = title screen   2 = ingame     3 = gameover    4 = config     5 = help
  36. var standingtime = 0; // increments when hero stands without moving. If moves, is reset. Used to trigger standing animation.
  37. var helpscreen=0; // which helpscreen is shown  1~5
  38. var xscrolled=0;
  39. var yscrolled=0;
  40. var FPS=0;
  41.  
  42. // These values can later be changed to balance the game:
  43. var globalFallStun = 26;
  44. var globalMaxHealthMaximum = 200; //soviel maxhealth kann der spieler maximal haben
  45. var globalMaxHealthMinimum = 15; //soviel maxhealth wird der spieler mindestens immer haben
  46. var globalFallDamageFallDuration = 46; //solange muss player fallen bevor er damage nimmt beim landen
  47. var globalPlayerAcceleration = 20; // langsam 5 ~ ca. 60 schnell
  48. var globalStrongPoison=0.25;  //soviel damage per frame mit strong poison (übertrumpft weak poison)
  49. var globalWeakPoison=0.1;     // soviel damage per frame mit weak poison
  50. var globalFrictionGround=1;  //Friction 0-40, the higher the more momentum is applied
  51. var globalFrictionAir=18;
  52. var globalFrictionIce=38;
  53. var globalFrictionConstant=0.3; //How much Friction plays a role. 0.3 is a good value, 0=no friction 0.6 = huge friction
  54. var globalDamageFlashTime=10; //how long does the taking dmg graphical effect show
  55. var globalDeathTime=100; //waiting time after death
  56.  
  57. //Keycodes todo
  58.  
  59. var keySettingLeft=37;
  60. var keySettingRight=39;
  61. var keySettingUp=38;
  62. var keySettingConfirm=13;  //used on gameover->resetgame, titlescreen->start
  63. var keySettingAttack=17;  //17=ctrl
  64.  
  65.  
  66.  
  67.  
  68. // Images
  69.  
  70.  
  71.   //titlescreen
  72. var titleReady = false;
  73. var titleImg = new Image();
  74. titleImg.onload = function () {
  75.     titleReady = true;
  76. };
  77. titleImg.src = "img/titlescreen.png";
  78.  
  79.  
  80.  
  81.   //config
  82. var configReady = false;
  83. var configImg = new Image();
  84. configImg.onload = function () {
  85.     configReady = true;
  86. };
  87. configImg.src = "img/configscreen.png";
  88.  
  89.   //help1
  90. var help1Ready = false;
  91. var help1Img = new Image();
  92. help1Img.onload = function () {
  93.     help1Ready = true;
  94. };
  95. help1Img.src = "img/helpscreen1.png";
  96.  
  97.   //help2
  98. var help2Ready = false;
  99. var help2Img = new Image();
  100. help2Img.onload = function () {
  101.     help2Ready = true;
  102. };
  103. help2Img.src = "img/helpscreen2.png";
  104.  
  105.   //help3
  106. var help3Ready = false;
  107. var help3Img = new Image();
  108. help3Img.onload = function () {
  109.     help3Ready = true;
  110. };
  111. help3Img.src = "img/helpscreen3.png";
  112.  
  113.   //help4
  114. var help4Ready = false;
  115. var help4Img = new Image();
  116. help4Img.onload = function () {
  117.     help4Ready = true;
  118. };
  119. help4Img.src = "img/helpscreen4.png";
  120.  
  121.   //help1
  122. var help1Ready = false;
  123. var help11Img = new Image();
  124. help1Img.onload = function () {
  125.     help1Ready = true;
  126. };
  127. help1Img.src = "img/helpscreen1.png";
  128.  
  129.   //help1
  130. var help1Ready = false;
  131. var help11Img = new Image();
  132. help1Img.onload = function () {
  133.     help1Ready = true;
  134. };
  135. help1Img.src = "img/helpscreen1.png";
  136.  
  137.  
  138.  
  139.  
  140.  
  141.   //Char Template
  142. var charTemplateReady = false;
  143. var charTemplate = new Image();
  144. charTemplate.onload = function () {
  145.     charTemplateReady = true;
  146. };
  147. charTemplate.src = "img/charTemplate.png";
  148.  
  149.  
  150.   //bg room1
  151. var bg1Ready = false;
  152. var bg1Img = new Image();
  153. bg1Img.onload = function () {
  154.     bg1Ready = true;
  155. };
  156. bg1Img.src = "img/bg1.png";
  157.  
  158.   //bg room2
  159. var bg2Ready = false;
  160. var bg2Img = new Image();
  161. bg2Img.onload = function () {
  162.     bg2Ready = true;
  163. };
  164. bg2Img.src = "img/bg2.png";
  165.  
  166.   //gameover
  167. var gameoverReady = false;
  168. var gameoverImg = new Image();
  169. gameoverImg.onload = function () {
  170.     gameoverReady = true;
  171. };
  172. gameoverImg.src = "img/gameover.png";
  173.  
  174.   //itemslot border
  175. var itemslotborderReady = false;
  176. var itemslotborderImg = new Image();
  177. itemslotborderImg.onload = function () {
  178.     itemslotborderReady = true;
  179. };
  180. itemslotborderImg.src = "img/itemslotborder.png";
  181.  
  182.   //Item:Key
  183. var keyReady = false;
  184. var keyImg = new Image();
  185. keyImg.onload = function () {
  186.     keyReady = true;
  187. };
  188. keyImg.src = "img/itemkey.png";
  189.  
  190.   //Item:nothing
  191. var nothingReady = false;
  192. var nothingImg = new Image();
  193. nothingImg.onload = function () {
  194.     nothingReady = true;
  195. };
  196. nothingImg.src = "img/itemnothing.png";
  197.  
  198.   //Item:ball
  199. var ballReady = false;
  200. var ballImg = new Image();
  201. ballImg.onload = function () {
  202.     ballReady = true;
  203. };
  204. ballImg.src = "img/itemball.png";
  205.  
  206.   //Item:rope
  207. var ropeReady = false;
  208. var ropeImg = new Image();
  209. ropeImg.onload = function () {
  210.     ropeReady = true;
  211. };
  212. ropeImg.src = "img/itemrope.png";
  213.  
  214.   //Item:plank
  215. var plankReady = false;
  216. var plankImg = new Image();
  217. plankImg.onload = function () {
  218.     plankReady = true;
  219. };
  220. plankImg.src = "img/itemplank.png";
  221.  
  222.   //Weapon:sword
  223. var swordReady = false;
  224. var swordImg = new Image();
  225. swordImg.onload = function () {
  226.     swordReady = true;
  227. };
  228. swordImg.src = "img/weaponsword.png";
  229.  
  230.  
  231.   //Life Icon
  232. var LifeReady = false;
  233. var LifeImg = new Image();
  234. LifeImg.onload = function () {
  235.     LifeReady = true;
  236. };
  237. LifeImg.src = "img/lifeIcon.png";
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246. //Game objects
  247. var hero = {
  248.         direction: 0, //facing direction   0:right 1:left
  249.         state: 0, //0:stand 1:jump 2:fall 3:ded
  250.     jumpingframe: 0, // if jump, increments. if 30, resets
  251.         fallingframe: 0, //if fall, increments. if >50, dies on impact
  252.     x:0,
  253.     y:0,
  254.         xspeed:0,
  255.         yspeed:0,
  256.         xbefore: 0, // these are to see if player has moved
  257.         ybefore: 0,
  258.         hasmoved:false,
  259.         lives:2,
  260.         health:100,
  261.         healthbefore:100, // used, to know if dmg is taken
  262.         maxhealth:100,
  263.         hurtImg:0,
  264.         onleftslope:false,
  265.         onrightslope:false,
  266.         items:["nothing","nothing","nothing"],
  267.         weapon:"sword",
  268.         selectedslot:-1, //0~3 falls -1 dann kein slot ausgewählt
  269.         stun:0,
  270.         poisoned:false,     //weak poison damage over time
  271.         strongpoisoned:false,   //really strong poison damage over time
  272.         friction:1,
  273.         insidelava:false, //falls inside lava dann true. Benutzt zum rendern des chars
  274.        
  275.         movesLeft:function(modif) {
  276.                                     if (hero.xspeed>0) hero.xspeed=hero.xspeed-(40-hero.friction); //falls xspeed positiv, setze auf 0
  277.                                  
  278.                                     hero.xspeed = hero.xspeed -globalPlayerAcceleration+hero.friction*globalFrictionConstant;
  279.                                     if (hero.xspeed < -320) hero.xspeed = -320; //xspeed negative grenze
  280.  
  281.                                    hero.x = hero.x - (7 * modif) * -hero.xspeed / 320;
  282.                                    hero.direction = 1;     },
  283.                            
  284.        movesRight:function(modif) {
  285.                                     if (hero.xspeed<0) hero.xspeed=hero.xspeed+(40-hero.friction); //falls xspeed negativ, setze auf 0
  286.            
  287.                                     hero.xspeed = hero.xspeed +globalPlayerAcceleration-hero.friction*globalFrictionConstant;
  288.                                     if (hero.xspeed > 320) hero.xspeed = 320; //xspeed positive grenze
  289.            
  290.                                      hero.x = hero.x + (7 * modif) * hero.xspeed / 320;
  291.                                      hero.direction = 0;   },
  292.                                  
  293.         applyMomentum:function(modif)  {    hero.x = hero.x + 4*  hero.xspeed / 320;
  294.                                                                                },
  295.                                  
  296.         descends:function(modif) {   hero.state =2;      
  297.                                      hero.y = hero.y + 7 * modif;          
  298.                                      hero.fallingframe++;
  299.                                      hero.friction=globalFrictionAir;},
  300.                                  
  301.         ascends:function(modif) {    hero.state=1;
  302.                                      hero.y = hero.y - 7 * modif;
  303.                                      hero.jumpingframe++;
  304.                                      hero.friction=globalFrictionAir;},
  305.                                  
  306.         landsWithFallDamage:function(modif) {      hero.health = hero.health-hero.fallingframe+15; //je länger gefallen desto mehr damage
  307.                                      hero.state=0;
  308.                                      hero.stun=hero.stun+globalFallStun;
  309.                                      dmgtimer=globalFallStun;
  310.                                      hero.fallingframe=0;
  311.                                      hero.jumpingframe=0;
  312.                                      hero.friction=globalFrictionGround;},
  313.                                  
  314.         lands:function(modif) {      hero.state=0;    
  315.                                      hero.fallingframe=0;  
  316.                                      hero.jumpingframe=0;
  317.                                      hero.friction=globalFrictionGround;}
  318.                                  
  319.  
  320. };
  321.  
  322. var leftwalls = {   //wände die einen nach rechts pushen
  323.                     //coordinates ist ein array; jew. 4 values machen x,y,width,height aus eines rects
  324.     coordinates:0
  325. };
  326.  
  327. var rightwalls = {  
  328.     coordinates:0
  329. };
  330.  
  331. var grounds= {  
  332.     coordinates: 0
  333. };
  334.  
  335. var ceilings= {  
  336.     coordinates: 0
  337. };
  338.  
  339. var icegrounds= {   //note: diese teile bezwecken keine wall collision checks sondern lediglich dass die friction erhöht wird.
  340.     coordinates: 0
  341. };
  342.  
  343. var warps = { //i.e. loading zones
  344.     coordinates: 0
  345. };
  346.  
  347. var leftslopes = {  
  348.     coordinates:0    //coordinates ist array, jew 4 values geben 2 punkte an
  349. };
  350.  
  351. var rightslopes = {
  352.     coordinates:0
  353. };
  354.  
  355. var liquid = {
  356.     coordinates:0
  357. };
  358.  
  359. var spikes = {
  360.     coordinates:0
  361. };
  362.  
  363.  
  364.  
  365.  
  366.  
  367.  
  368. // MAUS KLICK
  369. function getClickPosition(event) {
  370.  
  371.  if (gamestate===3) { //falls gameover screen, dann bezweckt der click den neustart
  372.  
  373.    resetGame();
  374.  
  375.      }
  376.      
  377.  
  378.         if (event.x !== undefined && event.y !== undefined)
  379.        {
  380.          x = event.x;
  381.           y = event.y;
  382.         }
  383.         else // Firefox method to get the position
  384.         {
  385.           x = event.clientX + document.body.scrollLeft +
  386.               document.documentElement.scrollLeft;
  387.           y = event.clientY + document.body.scrollTop +
  388.               document.documentElement.scrollTop;
  389.         }
  390.  
  391.   x -= canvas.offsetLeft;
  392.   y -= canvas.offsetTop;
  393.  
  394.  
  395. //click on slot => slot gets selected
  396.  
  397. if (gamestate===2) {
  398.  
  399. if (y>390)
  400. {hero.selectedslot=-1;}
  401.  
  402. if (   x>460
  403.     && x<500
  404.    && y>400
  405.    && y<440)
  406. hero.selectedslot=0;
  407.  
  408. if (   x>503
  409.     && x<543
  410.    && y>400
  411.    && y<440)
  412. hero.selectedslot=1;
  413.  
  414. if (   x>546
  415.     && x<586
  416.    && y>400
  417.    && y<440)
  418. hero.selectedslot=2;
  419.  
  420. if (   x>613
  421.     && x<653
  422.    && y>400
  423.    && y<440)
  424. hero.selectedslot=3;
  425.  
  426. }
  427.  
  428. if (gamestate===1 && activecount > 120) { //falls titlescreen
  429.  
  430. if (y>325 && y<412 && x>40 && x < 212) {
  431.     getNewroom("r1");
  432.      framecount=0;
  433.      activecount=0;
  434.      gamestate=2;
  435.      hero.health=hero.maxhealth;
  436.      }
  437.      
  438.      if (y>330 && y<420 && x>264 && x < 440) {
  439.    
  440.     gamestate=4; //configscreen
  441.      }
  442.      
  443.      if (y>330 && y<420 && x>490 && x < 670) {
  444.    
  445.     gamestate=5; //helpscreen
  446.      }
  447.  
  448.  }
  449.  
  450.  
  451.  
  452. if (x>0 && x <20 && y >0 && y < 50) debug= !debug; // todo delete later
  453.  
  454.  
  455.  
  456.  
  457. //todo add ability to pick up nearby item
  458.  
  459. //todo add attack
  460.  
  461.  
  462. }
  463.  
  464.  
  465.  
  466. // draw wallbox function
  467.  
  468. var drawWallboxes = function () {
  469.    
  470. ctx.globalAlpha=0.4;
  471.  
  472.  
  473. for (var i = 0; i < leftwalls.coordinates.length; i=i+4 )  //leftwalls  
  474. {
  475. ctx.fillStyle ="orange";
  476. ctx.fillRect(leftwalls.coordinates[i] - xscrolled,   leftwalls.coordinates[i+1]-yscrolled,   leftwalls.coordinates[i+2],   leftwalls.coordinates[i+3]) ;
  477.  
  478. ctx.beginPath();
  479. ctx.moveTo(leftwalls.coordinates[i] - xscrolled,                           leftwalls.coordinates[i+1] - yscrolled);
  480. ctx.lineTo(leftwalls.coordinates[i] - xscrolled,                           leftwalls.coordinates[i+1] + leftwalls.coordinates[i+3] - yscrolled);
  481. ctx.lineTo(leftwalls.coordinates[i]+leftwalls.coordinates[i+2] - xscrolled ,leftwalls.coordinates[i+1]+leftwalls.coordinates[i+3] - yscrolled);
  482. ctx.lineTo(leftwalls.coordinates[i]+leftwalls.coordinates[i+2] - xscrolled ,leftwalls.coordinates[i+1] - yscrolled);
  483. ctx.closePath();
  484. ctx.strokeStyle="brown";
  485. ctx.stroke();    
  486. }
  487.  
  488.  
  489. ctx.fillStyle = "purple"; //rightwalls
  490. for (var i = 0; i < rightwalls.coordinates.length; i=i+4 )
  491. {
  492.    ctx.fillStyle ="pink";
  493. ctx.fillRect(rightwalls.coordinates[i] - xscrolled,   rightwalls.coordinates[i+1]-yscrolled,   rightwalls.coordinates[i+2],rightwalls.coordinates[i+3]) ;
  494.  
  495. ctx.beginPath();
  496. ctx.moveTo(rightwalls.coordinates[i] - xscrolled,                           rightwalls.coordinates[i+1] - yscrolled);
  497. ctx.lineTo(rightwalls.coordinates[i] - xscrolled,                           rightwalls.coordinates[i+1] + rightwalls.coordinates[i+3] - yscrolled);
  498. ctx.lineTo(rightwalls.coordinates[i]+rightwalls.coordinates[i+2] - xscrolled,   rightwalls.coordinates[i+1]+ rightwalls.coordinates[i+3] - yscrolled);
  499. ctx.lineTo(rightwalls.coordinates[i]+rightwalls.coordinates[i+2] - xscrolled,   rightwalls.coordinates[i+1] - yscrolled);
  500. ctx.closePath();
  501. ctx.strokeStyle="purple";
  502. ctx.stroke();    
  503.        
  504. }
  505.  
  506.  
  507. ctx.fillStyle = "black"; //grounds
  508. for (var i = 0; i < grounds.coordinates.length; i=i+4 )
  509. {
  510. ctx.fillStyle ="rgb(80,80,80)";
  511. ctx.fillRect(grounds.coordinates[i]-xscrolled,   grounds.coordinates[i+1]-yscrolled,   grounds.coordinates[i+2],grounds.coordinates[i+3]) ;
  512.  
  513. ctx.beginPath();
  514. ctx.moveTo(grounds.coordinates[i]-xscrolled,                           grounds.coordinates[i+1] - yscrolled);
  515. ctx.lineTo(grounds.coordinates[i]-xscrolled,                           grounds.coordinates[i+1] + grounds.coordinates[i+3] - yscrolled);
  516. ctx.lineTo(grounds.coordinates[i]+grounds.coordinates[i+2]-xscrolled,   grounds.coordinates[i+1]+ grounds.coordinates[i+3] - yscrolled);
  517. ctx.lineTo(grounds.coordinates[i]+grounds.coordinates[i+2]-xscrolled,   grounds.coordinates[i+1] - yscrolled);
  518. ctx.closePath();
  519. ctx.strokeStyle="rgb(120,120,120)";
  520. ctx.stroke();  
  521.   }
  522.  
  523.  
  524. ctx.fillStyle = "brown"; //ceilings
  525. for (var i = 0; i < ceilings.coordinates.length; i=i+4 )
  526. {
  527. ctx.fillStyle ="brown";
  528. ctx.fillRect(ceilings.coordinates[i]-xscrolled,   ceilings.coordinates[i+1]-yscrolled,   ceilings.coordinates[i+2],ceilings.coordinates[i+3]) ;
  529.  
  530. ctx.beginPath();
  531. ctx.moveTo(ceilings.coordinates[i]-xscrolled,                           ceilings.coordinates[i+1]-yscrolled);
  532. ctx.lineTo(ceilings.coordinates[i]-xscrolled,                           ceilings.coordinates[i+1] + ceilings.coordinates[i+3]-yscrolled);
  533. ctx.lineTo(ceilings.coordinates[i]+ceilings.coordinates[i+2]-xscrolled,   ceilings.coordinates[i+1]+ ceilings.coordinates[i+3]-yscrolled);
  534. ctx.lineTo(ceilings.coordinates[i]+ceilings.coordinates[i+2]-xscrolled,   ceilings.coordinates[i+1]-yscrolled);
  535. ctx.closePath();
  536. ctx.strokeStyle="darkred";
  537. ctx.stroke();  
  538. ;}
  539.  
  540.  
  541. ctx.fillStyle = "white"; //icegrounds
  542. for (var i = 0; i < icegrounds.coordinates.length; i=i+4 )
  543. { ctx.fillRect(icegrounds.coordinates[i]-xscrolled,   icegrounds.coordinates[i+1]-yscrolled,   icegrounds.coordinates[i+2],icegrounds.coordinates[i+3]) ;
  544.  
  545.   ctx.fillStyle = "rgba(250, 250, 250,0.9)";
  546.  ctx.fillText("ice", icegrounds.coordinates[i]-xscrolled, icegrounds.coordinates[i+1]-yscrolled);  
  547. }
  548.  
  549.  
  550. ctx.fillStyle = "green"; //loadingzones
  551. for (var i = 0; i < warps.coordinates.length; i=i+5 )
  552. { ctx.fillRect(warps.coordinates[i+1]-xscrolled,warps.coordinates[i+2]-yscrolled,warps.coordinates[i+3],warps.coordinates[i+4]) ;
  553.  ctx.fillStyle = "rgba(250, 250, 250,0.9)";
  554.  ctx.fillText(warps.coordinates[i], warps.coordinates[i+1]-xscrolled, warps.coordinates[i+2]-yscrolled);}
  555.  
  556.  
  557.  
  558. for (var i = 0; i < liquid.coordinates.length; i=i+5 )  //liquid
  559. {
  560.  if (liquid.coordinates[i] === "lava") ctx.fillStyle = "rgb(200,0,0)"; //lava
  561.  else if (liquid.coordinates[i] === "water") ctx.fillStyle = "rgb(100,100,200)"; //water
  562.  ctx.fillRect(liquid.coordinates[i+1] - xscrolled,   liquid.coordinates[i+2]-yscrolled,   liquid.coordinates[i+3],liquid.coordinates[i+4]) ;
  563.  
  564.   ctx.fillStyle = "rgba(250, 250, 250,0.9)";
  565.  ctx.fillText(liquid.coordinates[i], liquid.coordinates[i+1]-xscrolled, liquid.coordinates[i+2]-yscrolled);
  566. }
  567.  
  568.  
  569.  
  570. ctx.fillStyle = "white"; //slopes
  571. for (var i = 0; i < leftslopes.coordinates.length; i=i+4 )
  572. {
  573. ctx.beginPath();
  574. ctx.moveTo(leftslopes.coordinates[i]-xscrolled,    leftslopes.coordinates[i+1]-yscrolled);
  575. ctx.lineTo(leftslopes.coordinates[i]-xscrolled,    leftslopes.coordinates[i+3]-yscrolled);
  576. ctx.lineTo(leftslopes.coordinates[i+2]-xscrolled,  leftslopes.coordinates[i+3]-yscrolled);
  577. ctx.closePath();   ctx.fill();
  578. }
  579.  
  580. ctx.fillStyle = "pink"; //slopes
  581. for (var i = 0; i < rightslopes.coordinates.length; i=i+4 )
  582. {
  583. ctx.beginPath();
  584. ctx.moveTo(rightslopes.coordinates[i]-xscrolled,    rightslopes.coordinates[i+1]-yscrolled);
  585. ctx.lineTo(rightslopes.coordinates[i]-xscrolled,    rightslopes.coordinates[i+3]-yscrolled);
  586. ctx.lineTo(rightslopes.coordinates[i+2]-xscrolled,  rightslopes.coordinates[i+3]-yscrolled);
  587. ctx.closePath();   ctx.fill();
  588. }
  589.  
  590. ctx.globalAlpha=1;
  591. };
  592.  
  593.  
  594. // draw stats function
  595.  
  596. var drawStats = function() {
  597.  
  598.         ctx.fillStyle = "rgb(250, 250, 250)";
  599.     ctx.font = "11px Helvetica";
  600.     ctx.fillText("framecount: " + framecount, 232, 10);
  601.        ctx.fillText("player X " + hero.x, 232, 20);
  602.        ctx.fillText("player Y " + hero.y, 232, 30);
  603.        ctx.fillText("jump frames " + hero.jumpingframe, 232, 40);
  604.        ctx.fillText("fall frames " + hero.fallingframe, 232, 50);
  605.        if (hero.state===0) ctx.fillText("status: Standing", 232, 60);
  606.        if (hero.state===1) ctx.fillText("status: Jumping", 232, 60);
  607.        if (hero.state===2) ctx.fillText("status: Falling", 232, 60);
  608.        if (hero.state===3) ctx.fillText("status: Dead", 232, 60);
  609.        if (hero.direction===0) ctx.fillText("direction: right", 232,70);
  610.        if (hero.direction===1) ctx.fillText("direction: left", 232,70);
  611.        if (hero.hasmoved===true) ctx.fillText("moving?: yes", 232,80);
  612.        if (hero.hasmoved===false) ctx.fillText("moving?: no", 232,80);
  613.        if (insideloadingzone===false) ctx.fillText("in warp?: no", 232,90);
  614.        if (insideloadingzone===true) ctx.fillText("in warp?: yes", 232,90);
  615.        ctx.fillText("current room: " + currentroom, 232, 100);
  616.        ctx.fillText("next room: " + nextroom, 232, 110);
  617.        ctx.fillText("player lives: " + hero.lives, 232, 120);
  618.        ctx.fillText("deathtimer: " + deathtimer, 472, 10);
  619.        ctx.fillText("gamestate: "+ gamestate, 472, 20);
  620.        ctx.fillText("slope: " + hero.onleftslope + " " + hero.onrightslope, 472, 30);
  621.        ctx.fillText("health: " + hero.health, 472, 40);
  622.        ctx.fillText("dmgtimer: " + dmgtimer, 472, 50);
  623.        ctx.fillText("stun: " + hero.stun, 472, 60);
  624.        ctx.fillText("dmgImg?: " + hero.hurtImg, 472, 70);
  625.        ctx.fillText("xspeed: " + hero.xspeed, 472, 90);
  626.        ctx.fillText("yspeed?: " + hero.yspeed, 472, 100);
  627.        ctx.fillText("friction: " + hero.friction, 472, 120);
  628.        ctx.fillText("keys: " + printObject(keysDown),472, 160);
  629.        ctx.fillText("scrolled x: " + xscrolled, 472, 180);
  630.        ctx.fillText("scrolled y: " + yscrolled, 472, 190);
  631.        ctx.fillText("FPS: " + FPS, 472, 200);
  632.        
  633.      
  634. };
  635.  
  636. // print Object function
  637.  
  638. function printObject(o) {
  639.  var out = '';
  640.  for (var p in o) {
  641.    out += p + ': ' + o[p] + '\n';
  642.  }
  643.  return(out);
  644. }
  645.  
  646.  
  647. // framecount to time function
  648.  
  649. var framecountToTime = function(framecount) {
  650. var ms = (framecount%60)*16;
  651. var s = framecount;
  652. var m = framecount;
  653.  
  654. return m+":"+s+"."+ms;
  655. };
  656.  
  657.  
  658.  
  659.  
  660. // get room function (after death or after touching loading zone
  661.  
  662. var getNewroom = function(room) {
  663.    
  664.             if (room==="r1") return r1(100,200);
  665.             else if (room==="r2") return r2(100,200); //todo change    
  666. };
  667.  
  668.  
  669.  
  670.  
  671. // Handle keyboard controls
  672. var keysDown = {};
  673.  
  674. addEventListener("keydown", function (e) {
  675.     keysDown[e.keyCode] = true;
  676. }, false);
  677.  
  678. addEventListener("keyup", function (e) {
  679.     delete keysDown[e.keyCode];
  680. }, false);
  681.  
  682.  
  683.  
  684. // reset the whole game function
  685.  
  686. var resetGame = function () {
  687.    
  688.     gamestate=0;
  689.     framecount=0;
  690.     hero.lives = 3;  
  691.    
  692.    };
  693.  
  694.  
  695.  
  696. // room preset functions
  697.  
  698. var r1 = function (x,y) { //room1
  699.     hero.x = x;
  700.     hero.y = y;
  701.        hero.state = 2;
  702.        hero.direction = 0;
  703.        leftwalls.coordinates = [0,0,70,360,  388,350,10,290 ]  ;
  704.        rightwalls.coordinates = [480,464,10,300]  ;
  705.        grounds.coordinates = [0,342,390,600,  490,450,500,200,  790,382,130,68]  ;
  706.        ceilings.coordinates = [0,0,900,30];  
  707.        icegrounds.coordinates = [220,339,130,10];
  708.        leftslopes.coordinates = [65,250,110,350];  
  709.        rightslopes.coordinates = [794,382,690,458];
  710.        warps.coordinates = ["r2", 200,200,10,10]; // room name, xpos,ypos,xlength,ylength
  711.        liquid.coordinates = ["water",350,280,150,170, "lava", 500,300,150,170];
  712.        spikes.coordinates = [0];
  713.        currentroom="r1";
  714.      
  715. };
  716.  
  717.  
  718. var r2 = function (x,y,dir) { //room1
  719.     hero.x = x;
  720.     hero.y = y;
  721.        hero.state = 2;
  722.        hero.direction = 0;
  723.        leftwalls.coordinates = [0,0,70,360,  388,350,10,290 ]  ;
  724.        rightwalls.coordinates = [480,450,10,300,  780, 382, 10,88]  ;
  725.        grounds.coordinates = [0,342,390,600,  490,450,500,200,  790,382,130,68]  ;
  726.        ceilings.coordinates = [0];  
  727.        icegrounds.coordinates = [0];
  728.        leftslopes.coordinates = [0];
  729.        rightslopes.coordinates = [0];
  730.        warps.coordinates = ["r2",863,290,48,90];
  731.        liquid.coordinates = [0];
  732.        spikes.coordinates = [0];
  733.        currentroom="r2";
  734. };
  735.  
  736.  
  737.  
  738.  
  739.  
  740.  
  741.  
  742.  
  743.  
  744.  
  745. // Update objects function
  746. var update = function (modif) {
  747.    
  748.    if (gamestate===2 || gamestate===3) {
  749.    
  750. hero.healthbefore = hero.health;
  751. hero.xbefore = hero.x;
  752. hero.ybefore = hero.y;
  753.    
  754.  
  755.    if (hero.state===0) { //player stood
  756.        
  757.             if (keySettingUp in keysDown && hero.stun <1) { // player holds "up"
  758.            
  759.                hero.ascends(modif);
  760.  
  761.             }
  762.            
  763.             if (keySettingLeft in keysDown && hero.stun <1) { // Player holding left
  764.             hero.movesLeft(modif);
  765.          }
  766.  
  767.          if (keySettingRight in keysDown && hero.stun <1) { // Player holding right
  768.             hero.movesRight(modif);
  769.             }  
  770.            
  771.             if (hero.xspeed !== 0
  772.                 && !(keySettingLeft in keysDown ||keySettingRight in keysDown)
  773.                 && hero.stun<1) {
  774.             hero.applyMomentum(modif); //glide effekt
  775.              }
  776.             }
  777.        
  778.    else if (hero.state ===1) { //player was jumping
  779.                
  780.             if (keySettingUp in keysDown) { // wenn spieler "oben" drückt
  781.        
  782.                  if (hero.jumpingframe > 32 || hero.stun > 0) {  //wenn jumpframes überschritten oder stun
  783.                  
  784.                     //starte das fallen
  785.                     hero.descends(modif);
  786.                    
  787.                      } else {                      //ansonsten springe weiter
  788.                     hero.ascends(modif);
  789.                      }
  790.              }
  791.              else {                    //wenn nicht "oben" gedrückt, dan falle
  792.                  hero.descends(modif);
  793.                  
  794.                     }
  795.            
  796.              if (keySettingLeft in keysDown && hero.stun <1) { // Player holding left
  797.         hero.movesLeft(modif);
  798.          }
  799.  
  800.          if (keySettingRight in keysDown && hero.stun <1) { // Player holding right
  801.                hero.movesRight(modif);
  802.              }
  803.              
  804.              if (hero.xspeed !== 0
  805.                  && !(keySettingLeft in keysDown ||keySettingRight in keysDown)
  806.                 && hero.stun<1) {
  807.             hero.applyMomentum(modif); //glide effekt
  808.               }
  809.                      
  810. }      
  811.  
  812.     else if (hero.state ===2) { //player was falling
  813.  
  814.             hero.descends(modif);
  815.            
  816.              if (keySettingLeft in keysDown && hero.stun <1) { // Player holding left
  817.         hero.movesLeft(modif);
  818.          }
  819.  
  820.          if (keySettingRight in keysDown && hero.stun <1) { // Player holding right
  821.         hero.movesRight(modif);
  822.              }
  823.              
  824.               if (hero.xspeed !== 0 && !(keySettingLeft in keysDown ||keySettingRight in keysDown)) {
  825.             hero.applyMomentum(modif); //glide effekt
  826.               }
  827. }      
  828.  
  829.  
  830.  
  831. // during gameover, player can press key to continue
  832.  
  833. if (gamestate===3) // if gameover
  834. {
  835.  if (keySettingConfirm in keysDown)
  836.    {
  837.      resetGame();
  838.     }
  839.  else
  840.  {
  841.      deathtimer=1;  //because it mustn restart until user action
  842.      dmgtimer=0;
  843.  }
  844.  
  845. }
  846.  
  847.  
  848.  
  849. // wall collision:    
  850.  
  851.  
  852. for (var i = 0; i < leftwalls.coordinates.length; i=i+4 ) //leftwalls
  853. {
  854.  if ( hero.x > leftwalls.coordinates[i] //if player is inside any of them...
  855.        && hero.x <  leftwalls.coordinates[i]+leftwalls.coordinates[i+2]
  856.       && hero.y > leftwalls.coordinates[i+1]
  857.       && hero.y < leftwalls.coordinates[i+1]+leftwalls.coordinates[i+3])
  858.   {
  859.      hero.x = leftwalls.coordinates[i]+leftwalls.coordinates[i+2]; //... put player to the right of it
  860.       hero.xspeed = 0;
  861.    }
  862. }
  863.  
  864.  
  865. for (var i = 0; i < rightwalls.coordinates.length; i=i+4 ) //rightwalls
  866. {
  867.  if ( hero.x > rightwalls.coordinates[i] //if player is inside any of them...
  868.        && hero.x <  rightwalls.coordinates[i]+rightwalls.coordinates[i+2]
  869.       && hero.y > rightwalls.coordinates[i+1]
  870.       && hero.y < rightwalls.coordinates[i+1]+rightwalls.coordinates[i+3])
  871.   {
  872.      hero.x = rightwalls.coordinates[i]; //... put player to the left of it
  873.       hero.xspeed=0;
  874.            
  875.    }
  876. }
  877.  
  878.  
  879. for (var i = 0; i < grounds.coordinates.length; i=i+4 ) //grounds
  880. {
  881.  if ( hero.x > grounds.coordinates[i] //if player is inside any of them...
  882.        && hero.x <  grounds.coordinates[i]+grounds.coordinates[i+2]
  883.       && hero.y > grounds.coordinates[i+1]
  884.       && hero.y < grounds.coordinates[i+1]+grounds.coordinates[i+3])
  885.   {
  886.      hero.y = grounds.coordinates[i+1];  //... put player on top of it
  887.      
  888.       if (hero.fallingframe > globalFallDamageFallDuration) { //wenn spieler zu lange fällt, dann tot
  889.          
  890.           hero.landsWithFallDamage(modif);
  891.          
  892.       } else {
  893.           hero.lands(modif);
  894.    }
  895. }
  896. }
  897.  
  898.  
  899. for (var i = 0; i < icegrounds.coordinates.length; i=i+4 ) //ice Note: this is only to apply friction and for nothing else
  900. {
  901.  if ( hero.x > icegrounds.coordinates[i] //if player is inside any of them...
  902.        && hero.x <  icegrounds.coordinates[i]+icegrounds.coordinates[i+2]
  903.       && hero.y > icegrounds.coordinates[i+1]
  904.       && hero.y < icegrounds.coordinates[i+1]+icegrounds.coordinates[i+3])
  905.   {
  906.      hero.friction = globalFrictionIce;  //increase friction
  907. } else if (hero.state===0) hero.friction = globalFrictionGround;  //für den fall dass man sich aus dem rutschigen bereich rausbewegt
  908.      
  909. }
  910.  
  911.  
  912.  
  913. for (var i = 0; i < ceilings.coordinates.length; i=i+4 ) //ceilings
  914. {
  915.  if ( hero.x > ceilings.coordinates[i] //if player is inside any of them...
  916.        && hero.x <  ceilings.coordinates[i]+ceilings.coordinates[i+2]
  917.       && hero.y > ceilings.coordinates[i+1]
  918.       && hero.y < ceilings.coordinates[i+1]+ceilings.coordinates[i+3])
  919.   {
  920.      hero.y = ceilings.coordinates[i+1]+ceilings.coordinates[i+3]; //... put player below it
  921.       hero.state = 2; //state:falling
  922.       hero.y = hero.y + 7 * modif;
  923.    }
  924. }
  925.  
  926.        for (var i = 0; i < leftslopes.coordinates.length; i=i+4 ) //leftslopes
  927. {
  928.  var m = (leftslopes.coordinates[i+3] - leftslopes.coordinates[i+1]) / (leftslopes.coordinates[i+2] - leftslopes.coordinates[i]);
  929.  
  930.  if ( hero.x > leftslopes.coordinates[i] //if player is inside any of them...
  931.        && hero.x <  leftslopes.coordinates[i+2]
  932.       && hero.y > leftslopes.coordinates[i+1]
  933.       && hero.y < leftslopes.coordinates[i+3]
  934.                           //hero is below slope:  (note: revert the equation for opposite check)
  935.       && hero.y > m* (hero.x- leftslopes.coordinates[i]) + leftslopes.coordinates[i+1]
  936.       )
  937.   {        if (hero.fallingframe > globalFallDamageFallDuration) { //wenn spieler zu lange fällt, dann tot
  938.    
  939.      hero.landsWithFallDamage(modif);
  940.    
  941.       } else {
  942.       hero.y = m* (hero.x -leftslopes.coordinates[i]) + leftslopes.coordinates[i+1];
  943.       hero.lands(modif);
  944.       hero.onleftslope=true; }
  945.    }
  946. }
  947.  
  948. for (var i = 0; i < rightslopes.coordinates.length; i=i+4 ) //rightslopes
  949. {
  950.  var m = (rightslopes.coordinates[i+1] - rightslopes.coordinates[i+3]) / (rightslopes.coordinates[i] - rightslopes.coordinates[i+2]);
  951.  
  952.    
  953.  if ( hero.x > rightslopes.coordinates[i+2] //if player is inside any of them...
  954.        && hero.x <  rightslopes.coordinates[i]
  955.       && hero.y > rightslopes.coordinates[i+1]
  956.       && hero.y < rightslopes.coordinates[i+3]
  957.                          
  958.       && hero.y > m* (hero.x- rightslopes.coordinates[i]) + rightslopes.coordinates[i+1]
  959.       )
  960.   {   if (hero.fallingframe > 52) { //wenn spieler zu lange fällt, dann tot
  961.          
  962.           hero.landsWithFallDamage(modif);
  963.            
  964.       } else {
  965.       hero.y = m* (hero.x- rightslopes.coordinates[i]) + rightslopes.coordinates[i+1];
  966.       hero.lands(modif);
  967.       hero.onrightslope=true; }
  968.    }
  969. }
  970.        
  971.        
  972.        
  973.        
  974.        
  975.        
  976.  
  977.  
  978.  
  979.  // If no ground below player, give him fall status:
  980.  // i.e. when player walks off a ledge
  981.  
  982.  if (hero.state===0 && !hero.onleftslope && !hero.onrightslope) { //spieler steht und zwar nicht auf einer slope.
  983.    
  984. for (var i = 0; i < grounds.coordinates.length; i=i+4 ) //grounds
  985. {
  986.       if (
  987.          hero.x > grounds.coordinates[i] //if player does not have a ground tile below him
  988.        && hero.x <  grounds.coordinates[i]+grounds.coordinates[i+2]
  989.       && hero.y +3 > grounds.coordinates[i+1]
  990.       && hero.y < grounds.coordinates[i+1]+grounds.coordinates[i+3]
  991.           )
  992.       {
  993.         hero.state=0;
  994.          hero.jumpingframe=0;
  995.          break;
  996.        }  
  997.        else
  998.        {
  999.          hero.state=2; //player falls
  1000.        }
  1001. }
  1002. };
  1003.  
  1004.  
  1005.  if (hero.state===0 && hero.onleftslope) { //wenn spieler steht nicht mehr über slope -> entferne onslope attribut
  1006.    
  1007. for (var i = 0; i < leftslopes.coordinates.length; i=i+4 )
  1008. {
  1009.       if (
  1010.          hero.x > leftslopes.coordinates[i]
  1011.        && hero.x <  leftslopes.coordinates[i+2]
  1012.       && hero.y +1> (leftslopes.coordinates[i+3] - leftslopes.coordinates[i+1]) / (leftslopes.coordinates[i+2] - leftslopes.coordinates[i])*(hero.x - leftslopes.coordinates[i])+leftslopes.coordinates[i+1]
  1013.       && hero.y < leftslopes.coordinates[i+3]
  1014.           )
  1015.       {
  1016.         hero.state=0;
  1017.          hero.jumpingframe=0;
  1018.          break;
  1019.        }  
  1020.        else
  1021.        {
  1022.          hero.state=2; //player falls
  1023.          hero.onleftslope=false;
  1024.        }
  1025. }
  1026. }
  1027.  
  1028. if (hero.state===0 && hero.onrightslope) { //wenn spieler steht nicht mehr über slope -> entferne onslope attribut
  1029.    
  1030. for (var i = 0; i < rightslopes.coordinates.length; i=i+4 )
  1031. {
  1032.       if (
  1033.          hero.x > rightslopes.coordinates[i+2]
  1034.        && hero.x <  rightslopes.coordinates[i]
  1035.       && hero.y +1> (rightslopes.coordinates[i+1] - rightslopes.coordinates[i+3]) / (rightslopes.coordinates[i] - rightslopes.coordinates[i+2])*(hero.x - rightslopes.coordinates[i])+rightslopes.coordinates[i+1]
  1036.       && hero.y < rightslopes.coordinates[i+3]
  1037.           )
  1038.       {
  1039.         hero.state=0;
  1040.          hero.jumpingframe=0;
  1041.          break;
  1042.        }  
  1043.        else
  1044.        {
  1045.          hero.state=2; //player falls
  1046.          hero.onrightslope=false;
  1047.        }
  1048. }
  1049. };
  1050.  
  1051.  
  1052.  
  1053.  
  1054. //Misc:
  1055.  
  1056.  
  1057.   // camera pos: xscrolled und yscrolled berechnen
  1058.   if (hero.x > 350) {
  1059.       xscrolled = hero.x - 350;
  1060.       if (hero.x > 1050) xscrolled = 700;
  1061.   } else xscrolled=0;
  1062.  
  1063.  if (hero.y > 225) {
  1064.      yscrolled = hero.y - 225;
  1065.      if (hero.y > 675) yscrolled = 450;
  1066.   } else yscrolled=0;
  1067.  
  1068.  
  1069.  
  1070.   //keep in boundary
  1071.   if (hero.x > 1375) hero.x =1375;
  1072.   if (hero.x < 25) hero.x =25;
  1073.  if (hero.y > 925) hero.health=0; //dies
  1074.   if (hero.y < 25) hero.y =25;
  1075.  
  1076.  
  1077.  //update other things:
  1078.  framecount++;
  1079.  if (dmgtimer>0) dmgtimer--;
  1080.  
  1081.   //has player moved?
  1082.   if (hero.xbefore === hero.x || ! (keySettingLeft in keysDown || keySettingRight in keysDown))  hero.hasmoved=false;
  1083.   else hero.hasmoved=true;
  1084.    
  1085.  // has player taken damage?
  1086.   if (hero.healthbefore > hero.health)
  1087.   {
  1088.      dmgtimer=globalDamageFlashTime;
  1089.      
  1090.       if (Math.random()<0.5 )
  1091.      {      hero.hurtImg = 1;}
  1092.      else hero.hurtImg = 2;
  1093.      
  1094.      
  1095.  }
  1096.  
  1097. if (hero.stun===0) hero.hurtImg=0;
  1098.  
  1099.  hero.stun--;
  1100.  if (hero.stun >60) hero.stun =60;
  1101.   else if (hero.stun <0) hero.stun=0;
  1102.  
  1103.  
  1104.  // loadingzone collision:      
  1105. for (var i = 0; i < warps.coordinates.length; i=i+5 )
  1106. {
  1107.  if ( hero.x > warps.coordinates[i+1] //if player is inside any of them...
  1108.        && hero.x <  warps.coordinates[i+1]+warps.coordinates[i+3]
  1109.       && hero.y > warps.coordinates[i+2]
  1110.       && hero.y < warps.coordinates[i+2]+warps.coordinates[i+4])
  1111.   {  
  1112.      insideloadingzone=true;//trigger next room
  1113.       nextroom=warps.coordinates[i];
  1114.    }
  1115. }
  1116.  
  1117.  
  1118. // maxhealth checks
  1119. if (hero.maxhealth < hero.health) hero.health = hero.maxhealth;
  1120.  
  1121. if (hero.maxhealth > globalMaxHealthMaximum) hero.maxhealth = globalMaxHealthMaximum;
  1122. else if (hero.maxhealth<globalMaxHealthMinimum) hero.maxhealth = globalMaxHealthMinimum;
  1123.  
  1124. // xspeed reduces if not moving
  1125. if (! hero.hasmoved) {
  1126.  
  1127.      if (hero.xspeed > 40) hero.xspeed = hero.xspeed - (40-hero.friction);
  1128.       else if ( hero.xspeed < -40) hero.xspeed = hero.xspeed + (40-hero.friction);
  1129.      else hero.xspeed = 0;
  1130. }
  1131.  
  1132. // health = 0 triggers death
  1133. if (hero.health <1) {
  1134.        hero.state=3;
  1135.        activecount++;
  1136. }
  1137.  
  1138. //poison
  1139. if (gamestate===2) {
  1140. if (hero.strongpoisoned) hero.health=hero.health -globalStrongPoison;
  1141. else if (hero.poisoned) hero.health=hero.health-globalWeakPoison;
  1142. }
  1143.   }
  1144.  
  1145.  
  1146. else if (gamestate===0) {  // LOADING SCREEN
  1147.  
  1148. activecount++;
  1149.  
  1150.  if ( help1Ready && help2Ready && help3Ready && help4Ready && configReady &&  LifeReady && titleReady && charTemplate && bg1Ready && bg2Ready && gameoverReady
  1151.       && keyReady && nothingReady && ballReady && ropeReady && plankReady && swordReady)
  1152.  {
  1153.  if (activecount>100)
  1154.     {
  1155.       gamestate=1;
  1156.       activecount=0;
  1157.     }   //wenn alle bilder geladen dann wechsle zum titlescreen
  1158.   }
  1159. }
  1160.    
  1161. else if (gamestate===1) {  // TITLESCREEN
  1162.   //todo zeige title screen und zeige start aufforderung
  1163.   //bei klick oder knopfdruck, change gamestate to 2
  1164.  
  1165.   activecount++;
  1166.  
  1167.   if (keySettingConfirm in keysDown && activecount > 120) { //wenn enter gedrückt = start
  1168.     getNewroom("r1");
  1169.      framecount=0;
  1170.      activecount=0;
  1171.      gamestate=2;
  1172.      hero.health=hero.maxhealth;
  1173.      hero.poisoned=false;
  1174.      hero.strongpoisoned=false;
  1175.   }
  1176.    
  1177. }
  1178.  
  1179. else if (gamestate === 4) { // config
  1180.    
  1181.     // todo
  1182.    
  1183. }
  1184.  
  1185. else if (gamestate === 5) { //help
  1186.    
  1187.     // todo
  1188.    
  1189. }
  1190.  
  1191. }; // end of "update function"
  1192.  
  1193.  
  1194.  
  1195.  
  1196.  
  1197.  
  1198.  
  1199.  
  1200.  
  1201.  
  1202. // Draw everything function
  1203. var render = function () {
  1204.    
  1205.         if (gamestate===2 || gamestate===3) {
  1206.    
  1207.         //draw level bg
  1208.  
  1209.  
  1210.             if (currentroom==="r1") { //wenn der raum "r1" ist, dann zeichne dessen grafik
  1211.                 ctx.drawImage(bg1Img, xscrolled, yscrolled, 900, 600, 0, 0, 900, 600);
  1212.             }
  1213.             else if (currentroom==="r2") {
  1214.                 ctx.drawImage(bg2Img,xscrolled,yscrolled, 900, 600, 0, 0, 900, 600);
  1215.             }
  1216.  
  1217.      
  1218.         //draw hero
  1219.        
  1220.         if (hero.stun>1 && hero.health >0)
  1221.        {                                        //sx sy     swidth sheight    dx            dy      dwidth dheight
  1222. if (hero.hurtImg === 1) ctx.drawImage(charTemplate, 0, 500,       50, 50,     hero.x-25-xscrolled, hero.y-25-yscrolled,   50,50);
  1223. else if (hero.hurtImg === 2) ctx.drawImage(charTemplate, 50, 500,  50, 50,     hero.x-25-xscrolled, hero.y-25-yscrolled,   50,50);
  1224.         }
  1225.        
  1226. else {
  1227.            
  1228.             if (hero.state===3) //if dead
  1229.             {
  1230.                 if (hero.insidelaval===true) { // when dying inside of lava
  1231.                    
  1232.                             if (activecount < 10)  ctx.drawImage(charTemplate, 0, 350, 50, 50, hero.x-25-xscrolled, hero.y-25-yscrolled, 50, 50);  //deathsprite lava 1
  1233.                            else ctx.drawImage(charTemplate, 50, 350, 50, 50, hero.x-25-xscrolled, hero.y-25-yscrolled, 50, 50);                   //deathsprite lava 2
  1234.                
  1235.                }
  1236.                
  1237.           //     else if (hero.deathcause==="spikes") { // when dying in spikes          
  1238.           //     }
  1239.                
  1240.                else {   // when dying from fall dmg, enemy projectiles, being squeezed etc. etc.
  1241.                
  1242.                     if (hero.direction === 0)  {
  1243.                            if (activecount < 10)  ctx.drawImage(charTemplate, 0, 250, 50, 50, hero.x-25-xscrolled, hero.y-25-yscrolled, 50, 50); //deathsprite right normal1
  1244.                            else ctx.drawImage(charTemplate, 50, 250, 50, 50, hero.x-25-xscrolled, hero.y-25-yscrolled, 50, 50);                   //deathsprite right normal2
  1245.                            
  1246.                     }
  1247.                     else {
  1248.                            if (activecount < 10)  ctx.drawImage(charTemplate, 0, 300, 50, 50, hero.x-25-xscrolled, hero.y-25-yscrolled, 50, 50); //deathsprite left normal1
  1249.                            else ctx.drawImage(charTemplate, 50, 300, 50, 50, hero.x-25-xscrolled, hero.y-25-yscrolled, 50, 50);                   //deathsprite left normal2
  1250.                     }
  1251.                        
  1252.                }
  1253.                
  1254.                
  1255.                
  1256.            }
  1257.            else
  1258.            {
  1259.            if (hero.direction===0) //player looks right
  1260.            {
  1261.              if (hero.state===0) //stands
  1262.              {
  1263.                if (hero.hasmoved) {
  1264.                    
  1265.                    if (framecount % 30 < 15) ctx.drawImage(charTemplate, 0, 200, 50, 50, hero.x-25-xscrolled, hero.y-25-yscrolled, 50, 50); // walking right on ground 1
  1266.                    else                      ctx.drawImage(charTemplate, 50, 200, 50, 50, hero.x-25-xscrolled, hero.y-25-yscrolled, 50, 50); // walking right on ground 2
  1267.                    
  1268.                }
  1269.                else
  1270.                {
  1271.                   if (framecount % 160 < 40) ctx.drawImage(charTemplate, 0, 0, 50, 50, hero.x-25-xscrolled, hero.y-25-yscrolled, 50, 50);  // stand right 1
  1272.                   else if (framecount % 160 < 80) ctx.drawImage(charTemplate,50, 0, 50, 50, hero.x-25-xscrolled, hero.y-25-yscrolled, 50, 50);  // stand right 2
  1273.                   else if (framecount % 160 < 120) ctx.drawImage(charTemplate,100,0,50,50, hero.x-25-xscrolled, hero.y-25-yscrolled,50,50);  // stand right 3
  1274.                   else if (framecount % 160 < 160) ctx.drawImage(charTemplate, 50,0,50,50,hero.x-25-xscrolled, hero.y-25-yscrolled,50,50);  // stand right 4
  1275.                    
  1276.                }
  1277.              }
  1278.              else {
  1279.                  
  1280.                   if (hero.jumpingframe >0 && hero.fallingframe <1) { //jumping right
  1281.                  
  1282.                   ctx.drawImage(charTemplate, 50,450, 50,50, hero.x-25-xscrolled, hero.y-25-yscrolled, 50, 50);  
  1283.                    
  1284.                    }
  1285.                    else if (hero.fallingframe >0) { //falling right
  1286.  
  1287.                    if (framecount % 26 < 13) ctx.drawImage(charTemplate, 100, 400, 50, 50, hero.x-25-xscrolled, hero.y-25-yscrolled, 50, 50);  
  1288.                   else                      ctx.drawImage(charTemplate, 100, 450, 50, 50, hero.x-25-xscrolled, hero.y-25-yscrolled, 50, 50);  
  1289.      
  1290.                   }
  1291.              
  1292.  
  1293.          }
  1294.                  
  1295.                  
  1296.            }
  1297.            else                       //otherwiselooks left
  1298.            {  
  1299.              if (hero.state===0)
  1300.              {
  1301.                if (hero.hasmoved) {
  1302.                    
  1303.                                        if (framecount % 30 < 15) ctx.drawImage(charTemplate, 0, 150, 50, 50, hero.x-25-xscrolled, hero.y-25-yscrolled, 50, 50); // walking left on ground 1
  1304.                    else                      ctx.drawImage(charTemplate, 50, 150, 50, 50, hero.x-25-xscrolled, hero.y-25-yscrolled, 50, 50); // walking left on ground 2
  1305.                    
  1306.                    }
  1307.                else {
  1308.                    if (framecount % 160 < 40) ctx.drawImage(charTemplate, 0, 50, 50, 50, hero.x-25-xscrolled, hero.y-25-yscrolled, 50, 50);  // stand left 1
  1309.                    else if (framecount % 160 < 80) ctx.drawImage(charTemplate,50, 50, 50, 50, hero.x-25-xscrolled, hero.y-25-yscrolled, 50, 50);  // stand left 2
  1310.                    else if (framecount % 160 < 120) ctx.drawImage(charTemplate,100,50,50,50, hero.x-25-xscrolled, hero.y-25-yscrolled,50,50);  // stand left 3
  1311.                    else if (framecount % 160 < 160) ctx.drawImage(charTemplate, 50,50,50,50,hero.x-25-xscrolled, hero.y-25-yscrolled,50,50);  // stand left 4
  1312.                    
  1313.                     }
  1314.              }
  1315.              else  {  
  1316.                  
  1317.                   if (hero.jumpingframe >0 && hero.fallingframe <1) { //jumping left
  1318.                  
  1319.                   ctx.drawImage(charTemplate, 0,450, 50,50, hero.x-25-xscrolled, hero.y-25-yscrolled, 50, 50);  
  1320.                    
  1321.                    }
  1322.                    else if (hero.fallingframe >0) { //falling left
  1323.  
  1324.                    if (framecount % 26 < 13) ctx.drawImage(charTemplate, 0, 400, 50, 50, hero.x-25-xscrolled, hero.y-25-yscrolled, 50, 50);  
  1325.                   else                      ctx.drawImage(charTemplate, 50, 400, 50, 50, hero.x-25-xscrolled, hero.y-25-yscrolled, 50, 50);  
  1326.      
  1327.                   }
  1328.                  
  1329.                  
  1330.               }
  1331.              }
  1332.            }
  1333.        }
  1334.  
  1335.  
  1336.  
  1337.  
  1338.  
  1339.  
  1340.       // draw foreground elements
  1341.        
  1342.  
  1343.         // todo
  1344.  
  1345.  
  1346.  
  1347.  
  1348.  
  1349.        // draw red overlay when health is low
  1350.        
  1351.        if (hero.health < 1) {
  1352.        ctx.fillStyle="rgba(60,0,0,0.5)";    
  1353.        ctx.fillRect(0,0,canvas.width,canvas.height);
  1354.        }
  1355.        else if (hero.health <30) {
  1356.        ctx.globalAlpha =  (1 - (hero.health/30))*0.25 ;
  1357.        ctx.fillStyle="red";
  1358.        ctx.fillRect(0,0,canvas.width,canvas.height);
  1359.        }
  1360.  
  1361.        
  1362.        
  1363.  
  1364.  
  1365.        // draw items
  1366.       ctx.globalAlpha=1;
  1367.       ctx.drawImage(itemslotborderImg,440,canvas.height-53);
  1368.  
  1369.       ctx.globalAlpha = 0.8;
  1370.  
  1371.       for (var i = 0; i < hero.items.length; i++)
  1372.       {            
  1373.  
  1374.          
  1375.           if (hero.items[i] === "nothing")
  1376.           {                    
  1377.              ctx.drawImage(nothingImg, 460+i*43, canvas.height-50);
  1378.           }
  1379.            if (hero.items[i] === "ball")
  1380.           {
  1381.              ctx.drawImage(ballImg, 460+i*43, canvas.height-50);
  1382.           }
  1383.           if (hero.items[i] === "rope")
  1384.           {
  1385.               ctx.drawImage(ropeImg, 460+i*43, canvas.height-50);
  1386.           }
  1387.           if (hero.items[i] === "plank")
  1388.           {
  1389.               ctx.drawImage(plankImg, 460+i*43, canvas.height-50);
  1390.           }
  1391.           if (hero.items[i] === "key")
  1392.           {
  1393.              ctx.drawImage(keyImg, 460+i*43, canvas.height-50);
  1394.           }
  1395.          
  1396.           //umrandung
  1397.          
  1398.                 if (hero.selectedslot===i) //i ist der ausgewählte slot, dann => hervorhebung
  1399.            {ctx.lineWidth=3;
  1400.             ctx.strokeStyle="#ff0";}
  1401.            else
  1402.            {ctx.strokeStyle="rgba(50,50,250,0.7)";
  1403.             ctx.lineWidth=1;}
  1404.            
  1405.       ctx.strokeRect(460+i*43,canvas.height-50,40,40);
  1406.          
  1407.        }
  1408.        
  1409.  
  1410.        // draw weapons
  1411.                
  1412.            if (hero.weapon === "nothing")
  1413.            {                    
  1414.                ctx.drawImage(nothingImg, 614, canvas.height-50);
  1415.            }
  1416.             if (hero.weapon === "sword")
  1417.            {
  1418.                ctx.drawImage(swordImg, 614, canvas.height-50);
  1419.            }  
  1420.            
  1421.            //umrandung
  1422.            
  1423.                       if (hero.selectedslot===3) //i ist der ausgewählte slot, dann => hervorhebung
  1424.            {ctx.lineWidth=3;
  1425.             ctx.strokeStyle="#ff0";}
  1426.            else
  1427.            {ctx.strokeStyle="rgba(250,20,20,0.6)";
  1428.             ctx.lineWidth=1;}
  1429.            
  1430.       ctx.strokeRect(614,canvas.height-50,40,40);
  1431.        
  1432.        
  1433.        
  1434.  
  1435.      
  1436.      
  1437.       // draw lives
  1438.      
  1439.       for (var i = 0 ; i<hero.lives ; i++) {
  1440.          
  1441.      ctx.drawImage(LifeImg, hero.maxhealth*1.2+i*16+46, canvas.height-28);
  1442.  
  1443.      }
  1444.    
  1445.    
  1446.  
  1447.      // draw time
  1448.      
  1449.      if (timeattack) {
  1450.     ctx.fillStyle = "rgb(220, 220, 220)";
  1451.     ctx.font = "16px Calibri";
  1452.  
  1453.     ctx.fillText(framecountToTime(framecount), 20, canvas.height-36);
  1454.  
  1455.      }
  1456.  
  1457.  
  1458.              ctx.globalAlpha=1;
  1459.      
  1460.      
  1461.      
  1462.      // draw health
  1463.      
  1464.        ctx.fillStyle="black";
  1465.        ctx.fillRect(20,canvas.height-25,hero.maxhealth*1.2,10);  //black healthbar background
  1466.      
  1467.      if (hero.health >35 || (hero.health>0 && (framecount%34) > 17 ))
  1468.      {  
  1469.        if (hero.poisoned || hero.strongpoisoned) ctx.fillStyle="rgba(180,30,240,0.8)"; //purple bar if poisoned
  1470.         else ctx.fillStyle="rgba(180,30,30,0.8)";
  1471.         ctx.fillRect(20,canvas.height-25,hero.health*1.2,10); //draw red bar
  1472.       }
  1473.         ctx.lineWidth=2;
  1474.         ctx.strokeStyle="rgba(180,180,180,0.5)";
  1475.         ctx.strokeRect(20,canvas.height-25,hero.maxhealth*1.2,10); //draw white stroke
  1476.  
  1477.      
  1478.  
  1479.  
  1480.  
  1481.  
  1482.        // draw gameover
  1483.        
  1484.      
  1485.        if (gamestate===3) ctx.drawImage(gameoverImg, 100,100);
  1486.  
  1487.        ctx.fillRect(hero.x,hero.y,1,1);
  1488.  
  1489.  
  1490.  
  1491.  
  1492.     // Debug info/stats
  1493.         if (debug) {
  1494.                    
  1495.        drawWallboxes();
  1496.        drawStats();
  1497.        
  1498.             }
  1499.         }
  1500.        
  1501.         else if (gamestate===0) { // zeige den Loadingscreen
  1502.            
  1503.             ctx.fillStyle="black";
  1504.             ctx.fillRect(0,0,canvas.width,canvas.height);
  1505.  
  1506.         }
  1507.        
  1508.         else if (gamestate===1) { // zeige den titlescreen
  1509.            
  1510.             ctx.globalAlpha=1;
  1511.            
  1512.             ctx.fillStyle="black";
  1513.             ctx.fillRect(0,0,canvas.width,canvas.height);
  1514.            
  1515.             ctx.drawImage(titleImg, 0,0);
  1516.            
  1517.             if (activecount<100)  {
  1518.            ctx.globalAlpha= 1- (0.01*activecount);
  1519.            ctx.fillStyle="gba(0,0,0)";
  1520.            ctx.fillRect(0,0,canvas.width,canvas.height);
  1521.            }
  1522.  
  1523.  
  1524.        }
  1525.        
  1526.        else if (gamestate===4) {  //config
  1527.        
  1528.           ctx.drawImage(configImg,0,0);
  1529.          
  1530.           //todo screen 2 3 4
  1531.        
  1532.        }
  1533.        
  1534.        else if (gamestate===5) { //helpscreen
  1535.        
  1536.            ctx.drawImage(help1Img,0,0);
  1537.            
  1538.            //todo screen 2 3 4
  1539.            
  1540.        }
  1541.        
  1542. };
  1543.  
  1544.  
  1545. // The main game loop
  1546. var main = function () {
  1547.    
  1548.        
  1549.   now = Date.now();
  1550.  
  1551. while (now - then < 16) {
  1552.  now = Date.now();  
  1553.  } // auf jedenfall warten bis so viele millisec um sind. Dies soll entgegenwirken dass schnellere PCs das spiel beschleunigen
  1554.  
  1555.  
  1556.  
  1557.     update( 0.7 );
  1558.     render();
  1559.        frames_rendered_this_second++;            // nach jedem update und render, increment
  1560.      
  1561.   if ( now - timestamp_per_second > 1000)
  1562.    {
  1563.        timestamp_per_second = now;
  1564.        FPS = frames_rendered_this_second;
  1565.        frames_rendered_this_second=0;             // und nach einer sekunde, reset to zero
  1566.    }
  1567.    
  1568.    then = now;
  1569.  
  1570.         //wenn loadingzone berührt wurde-> nextroom handler
  1571.         if (gamestate===2 && insideloadingzone)
  1572.           {
  1573.             getNewroom(nextroom);
  1574.              insideloadingzone=false;
  1575.            }
  1576.            
  1577.         if (gamestate===2 && hero.state===3)
  1578.        {
  1579.            deathtimer++;
  1580.             if (deathtimer > globalDeathTime)
  1581.             {
  1582.                 deathtimer=0;
  1583.                 hero.fallingframe=0;
  1584.                 activecount=0;
  1585.                 hero.health=hero.maxhealth;
  1586.                 hero.strongpoisoned=false;
  1587.                 hero.strongpoisoned=false;
  1588.                 hero.lives--;
  1589.                
  1590.                 if (hero.lives < 1)
  1591.                { gamestate=3;
  1592.                  
  1593.                }
  1594.                else getNewroom(currentroom);   //else, reload room
  1595.            }
  1596.        }
  1597.        
  1598.          
  1599.     // Request to do this again ASAP
  1600.     requestAnimationFrame(main);
  1601. };
  1602.  
  1603.  
  1604. // Cross-browser support for requestAnimationFrame
  1605. var w = window;
  1606. requestAnimationFrame = w.requestAnimationFrame || w.webkitRequestAnimationFrame || w.msRequestAnimationFrame || w.mozRequestAnimationFrame;
  1607.  
  1608.  
  1609. // start the game
  1610. var now = Date.now();
  1611. var then = Date.now();
  1612. var timestamp_per_second= Date.now();
  1613. var frames_rendered_this_second=0;
  1614.  
  1615. resetGame();
  1616. main();
  1617.  
  1618.  
  1619.  
  1620.  
  1621.  
  1622. </script>
  1623. </body>
  1624. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement