Guest User

Untitled

a guest
Jun 24th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.24 KB | None | 0 0
  1. var PLAYGROUND_WIDTH = 800 ;
  2. var PLAYGROUND_HEIGHT = 200;
  3.  
  4. $(function(){
  5. // This is the AI that determines the next move
  6. // level=0: totaly random
  7. // level=1: totaly "rational"
  8.  
  9. // possible move;
  10. IDLE= 0;
  11. WALK_FORWARD= 1;
  12. WALK_BACKWARD= 2;
  13. PUNCH= 3;
  14. KICK= 4;
  15. BLOCK= 5;
  16. BEATEN= 6;
  17.  
  18. //constantes:
  19. NEAR= 100;
  20.  
  21. // this is a methods that returns a random element from the given array
  22. function or(choice){
  23. return choice[Math.round(Math.random()*(choice.length-1))];
  24. };
  25.  
  26. // return the distance between the opponents
  27. function distance(a, b){
  28. return Math.abs(a.position().left-b.position().left);
  29. };
  30.  
  31. function nextMove(level, a, b){
  32. if(Math.random() > level){
  33. return Math.round(Math.random()*5);
  34. }
  35. switch(b.data("fighter").currentState){
  36. // if the adversary is idle or moves away from us we get near him or attack ihm
  37. case IDLE:
  38. case WALK_BACKWARD:
  39. case BLOCK:
  40. if(distance(a,b) < NEAR){
  41. return or([KICK, PUNCH, WALK_BACKWARD]);
  42. } else {
  43. return or([WALK_FORWARD, IDLE]);
  44. }
  45. break;
  46. // if the adversary moves toward us we get away or attack ihm
  47. case WALK_FORWARD:
  48. if(distance(a,b) < NEAR){
  49. return or([KICK, PUNCH, WALK_BACKWARD]);
  50. } else {
  51. return or([WALK_FORWARD, IDLE]);
  52. }
  53. break;
  54. // if we are under attack we either block go back or try to fight back
  55. case PUNCH:
  56. case KICK:
  57. return or([BLOCK, PUNCH, KICK, IDLE]);
  58. break;
  59. // if beaten we block or go back
  60. case BEATEN:
  61. return or([BLOCK, WALK_BACKWARD, IDLE]);
  62. break;
  63. }
  64. }
  65.  
  66. function animate(sprite){
  67. sprite = $(sprite);
  68. fighter = sprite.data("fighter");
  69. adversary = $(fighter.adversary);
  70. adversaryFighter = adversary.data("fighter");
  71.  
  72. var nextState = nextMove(0.8, sprite, adversary);
  73.  
  74. changeAnimation(sprite, fighter.animations, nextState, fighter.currentState);
  75.  
  76. if(nextState == PUNCH || nextState == KICK){
  77. sprite.css("z-index", 20);
  78. } else if(fighter.currentState == PUNCH || fighter.currentState == KICK){
  79. sprite.css("z-index", 0);
  80. }
  81.  
  82. fighter.currentState = nextState;
  83. }
  84.  
  85. var scrollStage = function (offset){
  86.  
  87. if(offset > 50){
  88. offset = 50;
  89. } else if(offset < -50) {
  90. offset = -50;
  91. }
  92. $("#foreground").css("left", ""+(-800 + offset/0.5)+"px");
  93.  
  94. $("#ground").css("left", ""+(-300 + offset)+"px");
  95. $("#fighters").css("left", ""+ offset +"px");
  96.  
  97. $("#background1").css("left", ""+(50 + offset/2)+"px");
  98. $("#background2").css("left", ""+(30 + offset/4)+"px");
  99. $("#background3").css("left", ""+(90 + offset/5)+"px");
  100.  
  101. }
  102.  
  103. /*replace with new*/
  104. var changeAnimation = function(sprite, animationArry, newAnimation , oldAnimation){
  105. sprite
  106. .setAnimation(animationArry[newAnimation].animation)
  107. .width(animationArry[newAnimation].width)
  108. .height(animationArry[newAnimation].height)
  109. .css("top", sprite.position().top + animationArry[newAnimation].deltaY - animationArry[oldAnimation].deltaY)
  110. .css("left", sprite.position().left + animationArry[newAnimation].deltaX - animationArry[oldAnimation].deltaX);
  111. };
  112.  
  113. // the game
  114. $("#playground").playground({height: PLAYGROUND_HEIGHT, width: PLAYGROUND_WIDTH, refreshRate: 30, keyTracker: true});
  115.  
  116. //Playground Sprites
  117. var foreground = new $.gameQuery.Animation({imageURL: "./stage/foreground.png", type: $.gameQuery.ANIMATION_VERTICAL});
  118. var ground = new $.gameQuery.Animation({imageURL: "./stage/ground.png"});
  119. var background1 = new $.gameQuery.Animation({imageURL: "./stage/background1.png"});
  120. var background2 = new $.gameQuery.Animation({imageURL: "./stage/background2.png"});
  121. var background3 = new $.gameQuery.Animation({imageURL: "./stage/background3.png"});
  122. $.playground().addSprite( "background3",
  123. {posx: 90, posy: 0,
  124. height: 200, width: 534,
  125. animation: background3})
  126. .addSprite( "background2",
  127. {posx:30, posy: -50,
  128. height: 180, width: 432,
  129. animation: background2})
  130. .addSprite( "background1",
  131. {posx:50, posy: -150,
  132. height: 317, width: 749,
  133. animation: background1})
  134. .addSprite( "ground",
  135. {posx: -300, posy: 0,
  136. height: 200, width: 1493,
  137. animation: ground}).addGroup("fighters").end()
  138. .addSprite( "foreground",
  139. {posx:-800, posy: 165,
  140. height: 44, width: 2000,
  141. animation: foreground});
  142. $("#sceengraph").css("background-color","#121423");
  143.  
  144. //Fighters
  145. var cvs = {
  146. currentState : IDLE,
  147. position: 250,
  148. adversary: "#abobo",
  149. animations: [ {animation: new $.gameQuery.Animation({ imageURL: "./cvs/cvs_idle_59x106x6.png",
  150. numberOfFrame: 6,
  151. delta: 59,
  152. rate:240,
  153. type: $.gameQuery.ANIMATION_HORIZONTAL | $.gameQuery.ANIMATION_CALLBACK}),
  154. deltaX: 0, deltaY: 0, width: 59, height: 106},
  155. {animation: new $.gameQuery.Animation({ imageURL: "./cvs/cvs_walk_forward_58x106x5.png",
  156. numberOfFrame: 5,
  157. delta: 58,
  158. rate:240,
  159. type: $.gameQuery.ANIMATION_HORIZONTAL | $.gameQuery.ANIMATION_CALLBACK}),
  160. deltaX: 0, deltaY: 0, width: 58, height: 106},
  161. {animation: new $.gameQuery.Animation({ imageURL: "./cvs/cvs_walk_backward_58x106x5.png",
  162. numberOfFrame: 5,
  163. delta: 58,
  164. rate:240,
  165. type: $.gameQuery.ANIMATION_HORIZONTAL | $.gameQuery.ANIMATION_CALLBACK}),
  166. deltaX: 0, deltaY: 0, width: 58, height: 106},
  167. {animation: new $.gameQuery.Animation({ imageURL: "./cvs/cvs_punch_120x104x6.png",
  168. numberOfFrame: 6,
  169. delta: 120,
  170. rate:120,
  171. type: $.gameQuery.ANIMATION_HORIZONTAL | $.gameQuery.ANIMATION_CALLBACK}),
  172. deltaX: 0, deltaY: 2, width: 120, height: 104},
  173. {animation: new $.gameQuery.Animation({ imageURL: "./cvs/cvs_kick_156x106x9.png",
  174. numberOfFrame: 9,
  175. delta: 156,
  176. rate:90,
  177. type: $.gameQuery.ANIMATION_HORIZONTAL | $.gameQuery.ANIMATION_CALLBACK}),
  178. deltaX: -20, deltaY: 0, width: 156, height: 106},
  179. {animation: new $.gameQuery.Animation({ imageURL: "./cvs/cvs_block_69x99x2.png",
  180. numberOfFrame: 2,
  181. delta: 69,
  182. rate:480,
  183. type: $.gameQuery.ANIMATION_HORIZONTAL | $.gameQuery.ANIMATION_CALLBACK}),
  184. deltaX: 0, deltaY: 7, width: 69, height: 99},
  185. {animation: new $.gameQuery.Animation({ imageURL: "./cvs/cvs_hit_59x103x1.png",
  186. rate: 720,
  187. type: $.gameQuery.ANIMATION_CALLBACK}),
  188. deltaX: 0, deltaY: 3, width: 59, height: 103}]
  189. }
  190. $("#fighters").addSprite("cvs",
  191. {posx: 250,
  192. posy: 70,
  193. height: 106,
  194. width: 58,
  195. animation: cvs.animations[0].animation,
  196. geometry: $.gameQuery.GEOMETRY_RECTANGLE,
  197. callback: animate});
  198. $("#cvs").data("fighter", cvs);
  199.  
  200. var abobo = {
  201. currentState : IDLE,
  202. position: 500,
  203. adversary: "#cvs",
  204. animations: [ {animation: new $.gameQuery.Animation({ imageURL: "./abobo/abobo_idle_100x121x3.png",
  205. numberOfFrame: 3,
  206. delta: 100,
  207. rate:190,
  208. type: $.gameQuery.ANIMATION_HORIZONTAL | $.gameQuery.ANIMATION_CALLBACK}),
  209. deltaX: 0, deltaY: 49, width: 100, height: 121},
  210. {animation: new $.gameQuery.Animation({ imageURL: "./abobo/abobo_walk_forward_94x126x6.png",
  211. numberOfFrame: 6,
  212. delta: 94,
  213. rate:240,
  214. type: $.gameQuery.ANIMATION_HORIZONTAL | $.gameQuery.ANIMATION_CALLBACK}),
  215. deltaX: 0, deltaY: 44, width: 94, height: 126},
  216. {animation: new $.gameQuery.Animation({ imageURL: "./abobo/abobo_walk_backward_94x126x6.png",
  217. numberOfFrame: 6,
  218. delta: 94,
  219. rate:240,
  220. type: $.gameQuery.ANIMATION_HORIZONTAL | $.gameQuery.ANIMATION_CALLBACK}),
  221. deltaX: 0, deltaY: 44, width: 94, height: 126},
  222. {animation: new $.gameQuery.Animation({ imageURL: "./abobo/abobo_punch_131x170x4.png",
  223. numberOfFrame: 4,
  224. delta: 131,
  225. rate:150,
  226. type: $.gameQuery.ANIMATION_HORIZONTAL | $.gameQuery.ANIMATION_CALLBACK}),
  227. deltaX: -30, deltaY: 0, width: 131, height: 170},
  228. {animation: new $.gameQuery.Animation({ imageURL: "./abobo/abobo_kick_137x130x2.png",
  229. numberOfFrame: 2,
  230. delta: 137,
  231. rate:500,
  232. type: $.gameQuery.ANIMATION_HORIZONTAL | $.gameQuery.ANIMATION_CALLBACK}),
  233. deltaX: 20, deltaY: 40, width: 137, height: 130},
  234. {animation: new $.gameQuery.Animation({ imageURL: "./abobo/abobo_block_81x130x1.png",
  235. rate:700,
  236. type: $.gameQuery.ANIMATION_CALLBACK}),
  237. deltaX: 0, deltaY: 40, width: 81, height: 130},
  238. {animation: new $.gameQuery.Animation({ imageURL: "./abobo/abobo_hit_108x120x3.png",
  239. numberOfFrame: 3,
  240. delta: 108,
  241. rate:240,
  242. type: $.gameQuery.ANIMATION_HORIZONTAL | $.gameQuery.ANIMATION_CALLBACK}),
  243. deltaX: 0, deltaY: 50, width: 108, height: 120}]
  244. }
  245. $("#fighters").addSprite("abobo",
  246. {posx: 550,
  247. posy: 60,
  248. height: 121,
  249. width: 100,
  250. animation: abobo.animations[0].animation,
  251. geometry: $.gameQuery.GEOMETRY_RECTANGLE,
  252. callback: animate});
  253. $("#abobo").data("fighter", abobo);
  254.  
  255. //register the main callback
  256. $.playground().registerCallback(function(){
  257. var cvs = $("#cvs");
  258. var cvsF = cvs.data("fighter");
  259. var cvsLeft = cvs.position().left;
  260.  
  261. var abobo = $("#abobo");
  262. var aboboF = abobo.data("fighter");
  263. var aboboLeft = abobo.position().left;
  264.  
  265. //hit?
  266. if(cvsLeft+cvsF.animations[cvsF.currentState].width - 2 > aboboLeft){
  267. if((cvsF.currentState == KICK || cvsF.currentState == PUNCH) && aboboF.currentState != BEATEN){
  268. if (aboboF.currentState == KICK || aboboF.currentState == PUNCH) {
  269. changeAnimation(abobo, aboboF.animations, BEATEN, aboboF.currentState);
  270. aboboF.currentState = BEATEN;
  271. changeAnimation(cvs, cvsF.animations, BEATEN, cvsF.currentState);
  272. cvsF.currentState = BEATEN;
  273. } else {
  274. changeAnimation(abobo, aboboF.animations, BEATEN, aboboF.currentState);
  275. aboboF.currentState = BEATEN;
  276. }
  277. } else if ((aboboF.currentState == KICK || aboboF.currentState == PUNCH) && cvsF.currentState != BEATEN) {
  278. changeAnimation(cvs, cvsF.animations, BEATEN, cvsF.currentState);
  279. cvsF.currentState = BEATEN;
  280. }
  281. }
  282.  
  283. //Move
  284.  
  285.  
  286. if(cvsF.currentState == WALK_FORWARD){
  287. if((cvsLeft+cvsF.animations[cvsF.currentState].width+2) < aboboLeft){
  288. cvs.css("left", cvsLeft+2);
  289. }
  290. } else if ((cvsLeft > 50) && (cvsF.currentState == WALK_BACKWARD)){
  291. cvs.css("left", cvsLeft-2)
  292. }
  293.  
  294. if(aboboF.currentState == WALK_FORWARD){
  295. if((cvsLeft+cvsF.animations[cvsF.currentState].width+2) < aboboLeft){
  296. abobo.css("left", aboboLeft - 2);
  297. }
  298. } else if ((aboboLeft < 650) && (aboboF.currentState == WALK_BACKWARD)){
  299. abobo.css("left", aboboLeft + 2);
  300. }
  301.  
  302. var al = abobo.position().left - aboboF.animations[aboboF.currentState].deltaX;
  303. var cl = cvs.position().left - cvsF.animations[cvsF.currentState].deltaX;
  304.  
  305. var centerPos = (al - cl)/2 + cl;
  306. scrollStage(-(centerPos-400)*0.5);
  307.  
  308. return false;
  309. }, 30);
  310.  
  311.  
  312.  
  313.  
  314. //start loading!
  315. $().setLoadBar("loadingBar", 600);
  316. //initialize the start button
  317. $.playground().startGame(function(){
  318. $("#welcomMessage").fadeOut(2000, function(){$(this).remove()});
  319. });
  320. });
Add Comment
Please, Sign In to add comment