Guest User

Untitled

a guest
Oct 21st, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 31.70 KB | None | 0 0
  1. function onGameTick(evt:Event) {
  2. if (!dying) {
  3. if (!talking) {
  4. //if we arent dying
  5. //########################### Movement ################################
  6. //## Input Handling
  7. xMove *= RESISTANCE;
  8.  
  9. if (KEYLEFTIN) {
  10. if (!climbing) {
  11. targetAnimation = "RUN";
  12. xMove -= SPEED;
  13. } else {
  14. if (onLadder) {
  15. if (!KEYCLIMBUPIN) {
  16. //canClimb=false
  17. }
  18. }
  19. xMove -= CLIMBSPEED;
  20. }
  21. }
  22. if (KEYRIGHTIN) {
  23. if (!climbing) {
  24. targetAnimation = "RUN";
  25. xMove += SPEED;
  26. } else {
  27. if (onLadder) {
  28. if (!KEYCLIMBUPIN) {
  29. //canClimb=false
  30. }
  31. }
  32. xMove += CLIMBSPEED;
  33. }
  34. }
  35. if (!canClimb) {
  36. climbing = false;
  37. }
  38. if (KEYCLIMBUPIN && canClimb) {
  39. if (!KEYJUMPIN) {
  40. yMove -= CLIMBSPEED;
  41. climbing = true;
  42. touchingSlope = "not";
  43. } else {
  44. if (yMove>0) {
  45. yMove -= CLIMBSPEED;
  46. climbing = true;
  47. }
  48. }
  49. }
  50. if (KEYCLIMBDOWNIN && canClimb) {
  51. if (!KEYJUMPIN) {
  52. yMove += CLIMBSPEED;
  53. if (!grounded) {
  54. climbing = true;
  55. }
  56. } else {
  57. if (yMove>0) {
  58. yMove += CLIMBSPEED;
  59. climbing = true;
  60. }
  61. }
  62. }
  63. if (onLadder) {
  64. xMove = 0;
  65. }
  66. //## Gravity and Jumping
  67.  
  68. /*
  69. REPLACE CODE YOU GAVE ME WITH THIS
  70. */
  71.  
  72. if (!grounded) {
  73. yMove += GRAVITY;
  74. if (KEYJUMPIN) {
  75. if (jumpTimer < jumpDelay) {
  76. jumpTimer++;
  77. yMove -= HOVERSTRENGTH;
  78. }
  79. } else {
  80. jumpTimer = jumpDelay;
  81. }
  82. } else {
  83. jumpTimer = 0;
  84. if (KEYJUMPIN) {
  85. climbing = false;
  86. grounded = false;
  87. touchingSlope = "not";
  88. yMove = -JUMPSTRENGTH;
  89. }
  90. }
  91.  
  92. if (inUpdraft) {
  93. yMove -= currentUpdraftStrength;
  94. }
  95. //## Movement
  96. if ( touchingSlope == "not" ) {
  97. //Move player normally
  98. mcLevel.mcPlayer.x += xMove;
  99. mcLevel.mcPlayer.y += yMove;
  100. } else {
  101. //move the player at the angle of the slope
  102.  
  103. tempPointA = mcLevel.slopeArray[touchingSlope].localToGlobal( mcLevel.slopeArray[touchingSlope].pointA );
  104. tempPointA = mcLevel.globalToLocal( tempPointA );
  105. tempPointB = mcLevel.slopeArray[touchingSlope].localToGlobal( mcLevel.slopeArray[touchingSlope].pointB );
  106. tempPointB = mcLevel.globalToLocal( tempPointB );
  107.  
  108. var slopeAngle = Math.atan2( tempPointA.y-tempPointB.y, tempPointA.x-tempPointB.x );
  109. mcLevel.mcPlayer.x -= xMove * Math.cos( slopeAngle );
  110. mcLevel.mcPlayer.y -= xMove * Math.sin( slopeAngle );
  111. }
  112. //########################### COLLISION DETECTION & RESONSE ##############################
  113. foundGround = false;//assume we arent touching anything
  114. inUpdraft = false;//assume we arent in an updraft
  115. mcLevel.mcPlayer.y += 1;//move the player down a pixel to see if there is ground there
  116.  
  117. //## Trampolines
  118. for (i=0; i<mcLevel.trampArray.length; i++) {
  119. if (mcLevel.trampArray[i].hitTestObject( mcLevel.mcPlayer.collisionBox )) {
  120. if (!grounded) {
  121. if ( lastPos.y < mcLevel.trampArray[i].y ) {
  122. mcLevel.mcPlayer.y = mcLevel.trampArray[i].y;
  123. yMove = -mcLevel.trampArray[i].power;
  124. }
  125. }
  126. }
  127. }
  128. //## Platforms
  129. for (i=0; i<mcLevel.platformArray.length; i++) {
  130. if (mcLevel.platformArray[i].hitTestObject( mcLevel.mcPlayer.collisionBox )) {
  131. if ( lastPos.y <= mcLevel.platformArray[i].y ) {
  132. //coming from above
  133. climbing=false;
  134. mcLevel.mcPlayer.y = mcLevel.platformArray[i].y;
  135. foundGround = true;
  136. grounded = true;
  137. yMove = 0;
  138. } else if (lastPos.x <= mcLevel.platformArray[i].x ) {
  139. //coming from left
  140. if ( touchingSlope == "not" ) {
  141. //regular collision
  142. mcLevel.mcPlayer.x = mcLevel.platformArray[i].x - (mcLevel.mcPlayer.collisionBox.width / 2);
  143. xMove *= -0.8;
  144. } else {
  145. //collision from a slope
  146. if ( mcLevel.mcPlayer.y < mcLevel.platformArray[i].y + 30 && mcLevel.mcPlayer.y > mcLevel.platformArray[i].y - 5 ) {
  147. //platform is next to slope so go onto that
  148. mcLevel.mcPlayer.y = mcLevel.platformArray[i].y;
  149. foundGround = true;
  150. grounded = true;
  151. yMove = 0;
  152. touchingSlope = "not";
  153. } else {
  154. //platform is not next to slope so bounce
  155. mcLevel.mcPlayer.x = lastPos.x;
  156. mcLevel.mcPlayer.y = lastPos.y;
  157. xMove *= -0.8;
  158. }
  159. }
  160. } else if ( lastPos.x >= mcLevel.platformArray[i].x + mcLevel.platformArray[i].width ) {
  161. //coming from right
  162. if ( touchingSlope == "not" ) {
  163. //regular collision
  164. mcLevel.mcPlayer.x = mcLevel.platformArray[i].x + mcLevel.platformArray[i].width + (mcLevel.mcPlayer.collisionBox.width / 2);
  165. xMove *= -0.8;
  166. } else {
  167. //collision from a slope
  168. if ( mcLevel.mcPlayer.y < mcLevel.platformArray[i].y + 30 && mcLevel.mcPlayer.y > mcLevel.platformArray[i].y - 5 ) {
  169. //platform is next to slope so go onto that
  170. mcLevel.mcPlayer.y = mcLevel.platformArray[i].y - 1;//the -1 fixes a glitch, though it might just have been a wonky slope
  171. foundGround = true;
  172. grounded = true;
  173. yMove = 0;
  174. touchingSlope = "not";
  175. } else {
  176. //platform is not next to slope so bounce
  177. mcLevel.mcPlayer.x = lastPos.x;
  178. mcLevel.mcPlayer.y = lastPos.y;
  179. xMove *= -0.8;
  180. }
  181. }
  182. } else if ( lastPos.y >= mcLevel.platformArray[i].y + mcLevel.platformArray[i].height ) {
  183. //coming from below
  184. if ( touchingSlope == "not" ) {
  185. mcLevel.mcPlayer.y = lastPos.y + 1;
  186. yMove = 0;
  187. if (!inUpdraft) {
  188. mcLevel.mcPlayer.x = lastPos.x;
  189. } else {
  190. yMove *= -0.9;
  191. }
  192. } else {
  193. mcLevel.mcPlayer.x = lastPos.x;
  194. mcLevel.mcPlayer.y = lastPos.y;
  195. yMove = 0;
  196. xMove *= -0.8;
  197. }
  198. } else {
  199. //player was inside the platform both this frame and last so bump them
  200. mcLevel.mcPlayer.y = mcLevel.platformArray[i].y+1;
  201. lastPos.y = mcLevel.platformArray[i].y;
  202. //this is usually caused by an overlapping slope
  203. touchingSlope = "not";
  204. }
  205. }
  206. }
  207. //## One-Way Platforms
  208. if ( yMove >= 0 ) {
  209. for (i=0; i<mcLevel.oneWayPlatformArray.length; i++) {
  210. if ( lastPos.y <= mcLevel.oneWayPlatformArray[i].y ) {
  211. if ( mcLevel.oneWayPlatformArray[i].hitTestObject( mcLevel.mcPlayer.collisionBox ) ) {
  212. climbing=false;
  213. mcLevel.mcPlayer.y = mcLevel.oneWayPlatformArray[i].y;
  214. foundGround = true;
  215. grounded = true;
  216. yMove = 0;
  217. }
  218. }
  219. }
  220. }
  221. //## Climbable surfaces
  222. canClimb = false;
  223. for (i=0; i<mcLevel.climbableArray.length; i++) {
  224. if ( mcLevel.climbableArray[i].hitTestObject( mcLevel.mcPlayer.collisionBox ) ) {
  225. canClimb = true;
  226. if (climbing) {
  227. foundGround = true;
  228. grounded = true;
  229. //mcLevel.mcPlayer.y--
  230. lastClimbX = mcLevel.mcPlayer.x;
  231. lastClimbY = mcLevel.mcPlayer.y;
  232. yMove = 0;
  233. xMove = 0;
  234. }
  235. }
  236. }
  237. onLadder = false;
  238. var ClimbIsLadder = false;
  239. for (i=0; i<mcLevel.ladderArray.length; i++) {
  240. if ( mcLevel.ladderArray[i].hitTestObject( mcLevel.mcPlayer.collisionBox ) ) {
  241. canClimb = true;
  242. ClimbIsLadder = true;
  243. if (climbing) {
  244. onLadder = true;
  245. foundGround = true;
  246. grounded = true;
  247. //mcLevel.mcPlayer.y--
  248. lastClimbX = mcLevel.mcPlayer.x;
  249. lastClimbY = mcLevel.mcPlayer.y;
  250. yMove = 0;
  251. xMove = 0;
  252. mcLevel.mcPlayer.x = mcLevel.ladderArray[i].x;
  253. }
  254. }
  255. }
  256. if (climbing && canClimb==false) {
  257. //if we were climbing but we have gone somewhere we
  258. //cant climb, put the player back on the climbing surface
  259.  
  260. foundGround = true;
  261. grounded = true;
  262. yMove = 0;
  263. xMove = 0;
  264. canClimb=true
  265. ;
  266. if (mcLevel.mcPlayer.y < lastClimbY) {
  267. //the player climbed off the top into thin air
  268. mcLevel.mcPlayer.y = lastClimbY + 1;
  269. }
  270. //check to see if we are back on ladder,
  271. //otherwise we have gone off the side or bottom
  272. canClimb = false;
  273. for (i=0; i<mcLevel.climbableArray.length; i++) {
  274. if ( mcLevel.climbableArray[i].hitTestObject( mcLevel.mcPlayer.collisionBox ) ) {
  275. canClimb = true;
  276. }
  277. }
  278. for (i=0; i<mcLevel.ladderArray.length; i++) {
  279. if ( mcLevel.ladderArray[i].hitTestObject( mcLevel.mcPlayer.collisionBox ) ) {
  280. canClimb = true;
  281. }
  282. }
  283. //mcLevel.mcPlayer.x = lastClimbX
  284. }
  285. if (climbing && canClimb) {
  286. mcLevel.mcPlayer.y--;
  287. }
  288. if (climbing && onLadder==false && ClimbIsLadder) {
  289. onLadder=true;
  290. }
  291. //## Conveyor belts
  292. conveying = false;
  293. if ( yMove >= 0 ) {
  294. for (i=0; i<mcLevel.conveyorArray.length; i++) {
  295. if ( lastPos.y <= mcLevel.conveyorArray[i].y ) {
  296. if ( mcLevel.conveyorArray[i].hitTestObject( mcLevel.mcPlayer.collisionBox ) ) {
  297. mcLevel.mcPlayer.y = mcLevel.conveyorArray[i].y;
  298. foundGround = true;
  299. climbing=false;
  300. grounded = true;
  301. yMove = 0;
  302. if ( touchingSlope == "not" ) {
  303. if ( mcLevel.conveyorArray[i].pushDirection == "R" ) {
  304. if ( mcLevel.conveyorArray[i].cSpeed==0 ) {
  305. mcLevel.mcPlayer.x += CONVEYORSPEED;
  306. } else {
  307. mcLevel.mcPlayer.x += mcLevel.conveyorArray[i].cSpeed;
  308. }
  309. } else {
  310. if ( mcLevel.conveyorArray[i].cSpeed==0 ) {
  311. mcLevel.mcPlayer.x -= CONVEYORSPEED;
  312. } else {
  313. mcLevel.mcPlayer.x -= mcLevel.conveyorArray[i].cSpeed;
  314. }
  315. }
  316. }
  317. conveying = true;
  318. }
  319. }
  320. }
  321. }
  322. //## Moving Platforms
  323.  
  324. for (i=0; i<mcLevel.movingPlatformArray.length; i++) {
  325. // do collision
  326. hitThis = false;
  327. if ( lastPos.y <= mcLevel.movingPlatformArray[i].y ) {
  328. if ( mcLevel.movingPlatformArray[i].movingCollision.hitTestObject( mcLevel.mcPlayer.collisionBox ) ) {
  329. //if ( yMove >= 0 ){
  330. hitThis = true;
  331. //}
  332. }
  333. }
  334. // then do platform movement
  335. for (k=0; k<mcLevel.controlArray.length; k++) {
  336. if ( mcLevel.movingPlatformArray[i].movingCollision.hitTestObject( mcLevel.controlArray[k] ) ) {
  337. //change platform direction
  338. mcLevel.movingPlatformArray[i].moveDirection = mcLevel.controlArray[k].moveDirection;
  339. }
  340. }
  341. mcLevel.movingPlatformArray[i].lastX = mcLevel.movingPlatformArray[i].x;
  342. mcLevel.movingPlatformArray[i].lastY = mcLevel.movingPlatformArray[i].y;
  343. if ( mcLevel.movingPlatformArray[i].moveDirection == "RIGHT" ) {
  344. mcLevel.movingPlatformArray[i].x += mcLevel.movingPlatformArray[i].moveSpeed;
  345. } else if ( mcLevel.movingPlatformArray[i].moveDirection == "LEFT" ) {
  346. mcLevel.movingPlatformArray[i].x -= mcLevel.movingPlatformArray[i].moveSpeed;
  347. } else if ( mcLevel.movingPlatformArray[i].moveDirection == "UP" ) {
  348. mcLevel.movingPlatformArray[i].y -= mcLevel.movingPlatformArray[i].moveSpeed;
  349. } else if ( mcLevel.movingPlatformArray[i].moveDirection == "DOWN" ) {
  350. mcLevel.movingPlatformArray[i].y += mcLevel.movingPlatformArray[i].moveSpeed;
  351. }
  352. // double check collision with the new place too
  353. // otherwise falling onto an upwards moving platform wont register
  354. if ( lastPos.y <= mcLevel.movingPlatformArray[i].y ) {
  355. if ( mcLevel.movingPlatformArray[i].movingCollision.hitTestObject( mcLevel.mcPlayer.collisionBox ) ) {
  356. //if ( yMove >= 0 ){
  357. hitThis = true;
  358. //}
  359. }
  360. }
  361. //then if we hit, we move the player to the new position
  362. if (hitThis) {
  363. mcLevel.mcPlayer.y = mcLevel.movingPlatformArray[i].y;
  364. mcLevel.mcPlayer.x += mcLevel.movingPlatformArray[i].x - mcLevel.movingPlatformArray[i].lastX;
  365. foundGround = true;
  366. climbing=false;
  367. grounded = true;
  368. yMove = 0;
  369. }
  370. }
  371. //## Moving Fryers
  372.  
  373. for (i=0; i<mcLevel.movingFryerArray.length; i++) {
  374. if (mcLevel.movingFryerArray[i].bopped==false) {
  375.  
  376. // do collision
  377. hitThis = false
  378. ;
  379. if ( mcLevel.movingFryerArray[i].movingCollision.hitTestObject( mcLevel.mcPlayer.collisionBox ) ) {
  380. if ( lastPos.y <= mcLevel.movingFryerArray[i].y ) {
  381. hitThis = true;
  382. } else {
  383. xMove = 0;
  384. yMove = 0;
  385. grounded = false;
  386. touchingSlope = "not";
  387. climbing=false;
  388. dying = true;
  389. }
  390. }
  391. // then do platform movement
  392. for (k=0; k<mcLevel.fryerControlArray.length; k++) {
  393. if ( mcLevel.movingFryerArray[i].movingCollision.hitTestObject( mcLevel.fryerControlArray[k] ) ) {
  394. //change platform direction
  395. mcLevel.movingFryerArray[i].moveDirection = mcLevel.fryerControlArray[k].moveDirection;
  396. }
  397. }
  398. mcLevel.movingFryerArray[i].lastX = mcLevel.movingFryerArray[i].x;
  399. mcLevel.movingFryerArray[i].lastY = mcLevel.movingFryerArray[i].y;
  400. if ( mcLevel.movingFryerArray[i].moveDirection == "RIGHT" ) {
  401. mcLevel.movingFryerArray[i].x += mcLevel.movingFryerArray[i].moveSpeed;
  402. } else if ( mcLevel.movingFryerArray[i].moveDirection == "LEFT" ) {
  403. mcLevel.movingFryerArray[i].x -= mcLevel.movingFryerArray[i].moveSpeed;
  404. } else if ( mcLevel.movingFryerArray[i].moveDirection == "UP" ) {
  405. mcLevel.movingFryerArray[i].y -= mcLevel.movingFryerArray[i].moveSpeed;
  406. } else if ( mcLevel.movingFryerArray[i].moveDirection == "DOWN" ) {
  407. mcLevel.movingFryerArray[i].y += mcLevel.movingFryerArray[i].moveSpeed;
  408. }
  409. // double check collision with the new place too
  410. // otherwise falling onto an upwards moving platform wont register
  411. if ( mcLevel.movingFryerArray[i].movingCollision.hitTestObject( mcLevel.mcPlayer.collisionBox ) ) {
  412. if ( lastPos.y <= mcLevel.movingFryerArray[i].y ) {
  413. hitThis = true;
  414. } else {
  415. /*xMove = 0;
  416. yMove = 0;
  417. grounded = false;
  418. touchingSlope = "not";
  419. climbing=false
  420. dying = true*/
  421. }
  422. }
  423. //then if we hit, we move the player to the new position
  424. if (hitThis) {
  425. if (mcLevel.movingFryerArray[i].fryerBehaviour=="KILLER") {
  426. xMove = 0;
  427. yMove = 0;
  428. grounded = false;
  429. touchingSlope = "not";
  430. climbing=false;
  431. dying = true;
  432. }
  433. if (mcLevel.movingFryerArray[i].fryerBehaviour=="BOP") {
  434. mcLevel.movingFryerArray[i].bopped = true;
  435. /*mcLevel.mcPlayer.y = mcLevel.movingFryerArray[i].y;
  436. yMove = -JUMPSTRENGTH;*/
  437. }
  438. if (mcLevel.movingFryerArray[i].fryerBehaviour=="PLATFORM") {
  439. mcLevel.mcPlayer.y = mcLevel.movingFryerArray[i].y;
  440. mcLevel.mcPlayer.x += mcLevel.movingFryerArray[i].x - mcLevel.movingFryerArray[i].lastX;
  441. foundGround = true;
  442. climbing=false;
  443. grounded = true;
  444. yMove = 0;
  445. }
  446. }
  447. }
  448. }
  449. //## Slopes
  450. for (i=0; i<mcLevel.slopeArray.length; i++) {
  451. if ( mcLevel.slopeArray[i].hitTestObject( mcLevel.mcPlayer.collisionBox ) ) {
  452.  
  453. if ( mcLevel.slopeArray[i].pointA.y == 50 ) {
  454. //slope wall on right
  455. if ( lastPos.y > mcLevel.slopeArray[i].y && lastPos.x > mcLevel.slopeArray[i].x + mcLevel.slopeArray[i].width ) {
  456. //hit the wall
  457. if ( touchingSlope == "not" ) {
  458. //regular collision
  459. if ( mcLevel.mcPlayer.y < mcLevel.slopeArray[i].y + 30 ) {
  460. mcLevel.mcPlayer.y = mcLevel.slopeArray[i].y;
  461. foundGround = true;
  462. grounded = true;
  463. yMove = 0;
  464. touchingSlope = "not";
  465. } else {
  466. mcLevel.mcPlayer.x = mcLevel.slopeArray[i].x + mcLevel.slopeArray[i].width + (mcLevel.mcPlayer.collisionBox.width / 2) + 2;
  467. xMove *= -0.8;
  468. }
  469. } else {
  470. //collision from a slope
  471. if ( mcLevel.mcPlayer.y < mcLevel.slopeArray[i].y + 30 && mcLevel.mcPlayer.y > mcLevel.slopeArray[i].y - 5 ) {
  472. //platform is next to slope so go onto that
  473. mcLevel.mcPlayer.y = mcLevel.slopeArray[i].y - 1;//the -1 fixes a glitch, though it might just be a wonky slope
  474. foundGround = true;
  475. grounded = true;
  476. yMove = 0;
  477. touchingSlope = "not";
  478. } else {
  479. //platform is not next to slope so bounce
  480. mcLevel.mcPlayer.x = lastPos.x;
  481. mcLevel.mcPlayer.y = lastPos.y;
  482. xMove *= -0.8;
  483. }
  484. }
  485. } else {
  486. //slope wall on right, not hitting wall side
  487. if ( lastPos.x < mcLevel.slopeArray[i].x ) {
  488. if ( mcLevel.mcPlayer.x >= mcLevel.slopeArray[i].x ) {
  489. if ( mcLevel.mcPlayer.y > mcLevel.slopeArray[i].y + mcLevel.slopeArray[i].height ) {
  490. //bubmped into the corner from below
  491. mcLevel.mcPlayer.x = lastPos.x;
  492. mcLevel.mcPlayer.y = lastPos.y;
  493. xMove *= -0.8;
  494. }
  495. }
  496. }
  497. }
  498. } else if ( mcLevel.slopeArray[i].pointA.y == 0 ) {
  499. //slope wall on left
  500. if ( lastPos.y > mcLevel.slopeArray[i].y && lastPos.x < mcLevel.slopeArray[i].x ) {
  501. //hit the wall
  502. if ( touchingSlope == "not" ) {
  503. //regular collision
  504. if ( mcLevel.mcPlayer.y < mcLevel.slopeArray[i].y + 30 ) {
  505. mcLevel.mcPlayer.y = mcLevel.slopeArray[i].y;
  506. foundGround = true;
  507. grounded = true;
  508. yMove = 0;
  509. touchingSlope = "not";
  510. } else {
  511. mcLevel.mcPlayer.x = mcLevel.slopeArray[i].x - (mcLevel.mcPlayer.collisionBox.width / 2) - 2;
  512. xMove *= -0.8;
  513. }
  514. } else {
  515. //collision from a slope
  516. if ( mcLevel.mcPlayer.y < mcLevel.slopeArray[i].y + 30 && mcLevel.mcPlayer.y > mcLevel.slopeArray[i].y - 5) {
  517. //platform is next to slope so go onto that
  518. mcLevel.mcPlayer.y = mcLevel.slopeArray[i].y;
  519. foundGround = true;
  520. grounded = true;
  521. yMove = 0;
  522. touchingSlope = "not";
  523. } else {
  524. //platform is not next to slope so bounce
  525. mcLevel.mcPlayer.x = lastPos.x;
  526. mcLevel.mcPlayer.y = lastPos.y;
  527. xMove *= -0.8;
  528. }
  529. }
  530. } else {
  531. //slope wall on left, not hitting wall side
  532. if ( lastPos.x >= mcLevel.slopeArray[i].x + mcLevel.slopeArray[i].width ) {
  533. if ( mcLevel.mcPlayer.x <= mcLevel.slopeArray[i].x + mcLevel.slopeArray[i].width ) {
  534. if ( mcLevel.mcPlayer.y > mcLevel.slopeArray[i].y + mcLevel.slopeArray[i].height ) {
  535. //bubmped into the corner from below
  536. mcLevel.mcPlayer.x = lastPos.x;
  537. mcLevel.mcPlayer.y = lastPos.y;
  538. xMove *= -0.8;
  539. }
  540. }
  541. }
  542. }
  543. }
  544. if ( lastPos.y-10 > mcLevel.slopeArray[i].y + mcLevel.slopeArray[i].height ) {
  545. //collision from below
  546. if ( touchingSlope == "not" ) {
  547. mcLevel.mcPlayer.y = lastPos.y + 1;
  548. yMove = 0;
  549. if (!inUpdraft) {
  550. mcLevel.mcPlayer.x = lastPos.x;
  551. } else {
  552. yMove *= -0.9;
  553. }
  554. } else {
  555. mcLevel.mcPlayer.x = lastPos.x;
  556. mcLevel.mcPlayer.y = lastPos.y;
  557. yMove = 0;
  558. xMove *= -0.8;
  559. }
  560. } else {
  561. //not from below
  562.  
  563. if ( mcLevel.mcPlayer.x > mcLevel.slopeArray[i].x && mcLevel.mcPlayer.x < mcLevel.slopeArray[i].x + mcLevel.slopeArray[i].width && mcLevel.mcPlayer.y > mcLevel.slopeArray[i].y && mcLevel.mcPlayer.y < mcLevel.slopeArray[i].y + mcLevel.slopeArray[i].height ) {
  564. //player co-ordinats overlap slope bounding box
  565.  
  566. tempPointA = mcLevel.slopeArray[i].localToGlobal( mcLevel.slopeArray[i].pointA );
  567. tempPointA = mcLevel.globalToLocal( tempPointA );
  568. tempPointB = mcLevel.slopeArray[i].localToGlobal( mcLevel.slopeArray[i].pointB );
  569. tempPointB = mcLevel.globalToLocal( tempPointB );
  570.  
  571. var lineAngle = Math.atan2( tempPointA.y-tempPointB.y, tempPointA.x-tempPointB.x );
  572. var pointAngle = Math.atan2( tempPointA.y-mcLevel.mcPlayer.y, tempPointA.x-mcLevel.mcPlayer.x );
  573.  
  574. if ( pointAngle < lineAngle ) {
  575. //no slope collision
  576. } else {
  577. //slope collision
  578.  
  579. if ( mcLevel.mcPlayer.x > mcLevel.slopeArray[i].x && mcLevel.mcPlayer.x < mcLevel.slopeArray[i].x + mcLevel.slopeArray[i].width ) {
  580. foundGround = true;
  581. grounded = true;
  582. yMove = 0;
  583. mcLevel.mcPlayer.y -= 1;
  584. if ( touchingSlope != i ) {
  585. tempPointC = lineIntersectLine( tempPointA, tempPointB, new Point(mcLevel.mcPlayer.x, mcLevel.mcPlayer.y), lastPos );
  586. mcLevel.mcPlayer.y = tempPointC.y;
  587. mcLevel.mcPlayer.x = tempPointC.x;
  588. touchingSlope = i;
  589. climbing=false;
  590. }
  591. }
  592. }
  593. }
  594. }
  595. }
  596. }
  597. //## One-Way Ceilings
  598. for (i=0; i<mcLevel.oneWayCeilingArray.length; i++) {
  599. if ( mcLevel.oneWayCeilingArray[i].hitTestObject( mcLevel.mcPlayer.collisionBox ) ) {
  600. if ( touchingSlope == "not" ) {
  601. if ( yMove < 0 ) {
  602. if ( lastPos.y - mcLevel.mcPlayer.collisionBox.height > mcLevel.oneWayCeilingArray[i].y + mcLevel.oneWayCeilingArray[i].height ) {
  603. mcLevel.mcPlayer.y = mcLevel.oneWayCeilingArray[i].y + mcLevel.oneWayCeilingArray[i].height + mcLevel.mcPlayer.collisionBox.height + 3;
  604. yMove = 0;
  605. if (!inUpdraft) {
  606. mcLevel.mcPlayer.x = lastPos.x;
  607. } else {
  608. yMove *= -0.9;
  609. }
  610. }
  611. }
  612. } else {
  613. if ( mcLevel.mcPlayer.y < lastPos.y ) {
  614. mcLevel.mcPlayer.x = lastPos.x;
  615. mcLevel.mcPlayer.y = lastPos.y;
  616. yMove = 0;
  617. xMove *= -0.8;
  618. }
  619. }
  620. }
  621. }
  622. //## updrafts
  623. currentUpdraftStrength = 0;
  624. for (i=0; i<mcLevel.updraftArray.length; i++) {
  625. if ( mcLevel.updraftArray[i].hitTestObject( mcLevel.mcPlayer.collisionBox ) ) {
  626. inUpdraft = true;
  627. if ( mcLevel.updraftArray[i].power == 0 ) {
  628. if ( UPDRAFTSTRENGTH > currentUpdraftStrength ) {
  629. currentUpdraftStrength = UPDRAFTSTRENGTH;
  630. }
  631. } else {
  632. if ( mcLevel.updraftArray[i].power > currentUpdraftStrength ) {
  633. currentUpdraftStrength = mcLevel.updraftArray[i].power;
  634. }
  635. }
  636. if (grounded) {
  637. mcLevel.mcPlayer.y -= 2;
  638. }
  639. }
  640. }
  641. //## Checkpoints
  642. for (i=0; i<mcLevel.checkPointArray.length; i++) {
  643. if ( mcLevel.checkPointArray[i].hitTestObject( mcLevel.mcPlayer.collisionBox ) ) {
  644. safeSpot.x = mcLevel.checkPointArray[i].x;
  645. safeSpot.y = mcLevel.checkPointArray[i].y;
  646. }
  647. }
  648. //## Messages
  649. for (i=0; i<mcLevel.messageArray.length; i++) {
  650. if ( mcLevel.messageArray[i].hitTestObject( mcLevel.mcPlayer.collisionBox ) ) {
  651. if (mcLevel.messageArray[i].talkType=="CONTACT") {
  652. mcLevel.messageArray[i].Talk();
  653. } else {
  654. if (KEYACTIONIN) {
  655. mcLevel.messageArray[i].Talk();
  656. }
  657. }
  658. }
  659. }
  660. //## Dangerous surfaces
  661. for (i=0; i<mcLevel.fryArray.length; i++) {
  662. if ( mcLevel.fryArray[i].hitTestObject( mcLevel.mcPlayer.collisionBox ) ) {
  663. //mcLevel.mcPlayer.x = safeSpot.x;
  664. //mcLevel.mcPlayer.y = safeSpot.y;
  665. xMove = 0;
  666. yMove = 0;
  667. grounded = false;
  668. touchingSlope = "not";
  669. climbing=false;
  670. dying = true;
  671. }
  672. }
  673. //## Keys
  674. for (i=0; i<mcLevel.keyArray.length; i++) {
  675. for (k=0; k<keysOwned.length; k++) {
  676. if ( keysOwned[i] == mcLevel.keyArray[i].keyName.text ) {
  677. mcLevel.keyArray[i].visible = false;
  678. }
  679. }
  680. if ( mcLevel.keyArray[i].hitTestObject( mcLevel.mcPlayer.collisionBox ) ) {
  681. keysOwned.push( mcLevel.keyArray[i].keyName.text );
  682. mcLevel.keyArray[i].visible = false;
  683. }
  684. }
  685. //## Collectables
  686. for (i=0; i<mcLevel.collectablesArray.length; i++) {
  687. if (mcLevel.collectablesArray[i].collected==false) {
  688. if ( mcLevel.collectablesArray[i].hitTestObject( mcLevel.mcPlayer.collisionBox ) ) {
  689.  
  690. var knownType = false;
  691. for (k=0; k<collectedStuff.length; k++) {
  692. if (collectedStuff[k][0] == mcLevel.collectablesArray[i].collectableName.text) {
  693. knownType = true;
  694. collectedStuff[k][1] += Number(mcLevel.collectablesArray[i].collectableValue.text);
  695. mcLevel.collectablesArray[i].collected = true;
  696. }
  697. }
  698. if (!knownType) {
  699. collectedStuff.push( [String(mcLevel.collectablesArray[i].collectableName.text), Number(mcLevel.collectablesArray[i].collectableValue.text) ] );
  700. mcLevel.collectablesArray[i].collected = true;
  701. }
  702. }
  703. }
  704. }
  705. //##Animations
  706. for (i=0; i<mcLevel.animationsArray.length; i++) {
  707. if ( mcLevel.animationsArray[i].collisionBox.hitTestObject( mcLevel.mcPlayer.collisionBox ) ) {
  708. if (String(mcLevel.animationsArray[i].triggerType.text)=="TOUCH") {
  709. if (mcLevel.animationsArray[i].animationKey.text == "NONE") {
  710. mcLevel.animationsArray[i].triggered = true;
  711. } else {
  712. for (k=0; k<keysOwned.length; k++) {
  713. if ( keysOwned[i] == mcLevel.animationsArray[i].animationKey.text ) {
  714. mcLevel.animationsArray[i].triggered = true;
  715. }
  716. }
  717. }
  718. } else {
  719. if (KEYACTIONIN) {
  720. if (mcLevel.animationsArray[i].animationKey.text == "NONE") {
  721. mcLevel.animationsArray[i].triggered = true;
  722. } else {
  723. for (k=0; k<keysOwned.length; k++) {
  724. if ( keysOwned[i] == mcLevel.animationsArray[i].animationKey.text ) {
  725. mcLevel.animationsArray[i].triggered = true;
  726. }
  727. }
  728. }
  729. } else {
  730. mcLevel.animationsArray[i].triggered = false;
  731. }
  732. }
  733. } else {
  734. mcLevel.animationsArray[i].triggered = false;
  735. }
  736. }
  737. //## Teleports
  738. for (i=0; i<mcLevel.teleportArray.length; i++) {
  739. if ( mcLevel.teleportArray[i].hitTestObject( mcLevel.mcPlayer.collisionBox ) ) {
  740. if ( mcLevel.teleportArray[i].teleportType.text == "INSTANT" ) {
  741. if ( mcLevel.teleportArray[i].teleportKey.text != "NONE" ) {
  742. for (k=0; k<keysOwned.length; k++) {
  743. if ( keysOwned[i] == mcLevel.teleportArray[i].teleportKey.text ) {
  744. teleporting = true;
  745. }
  746. }
  747. } else {
  748. teleporting = true;
  749. }
  750. } else {
  751. if ( mcLevel.teleportArray[i].teleportKey.text != "NONE" ) {
  752. for (k=0; k<keysOwned.length; k++) {
  753. if ( keysOwned[i] == mcLevel.teleportArray[i].teleportKey.text ) {
  754. if (KEYACTIONIN) {
  755. teleporting = true;
  756. }
  757. }
  758. }
  759. } else {
  760. if (KEYACTIONIN) {
  761. teleporting = true;
  762. }
  763. }
  764. }
  765. teleportFrame = Number( mcLevel.teleportArray[i].targetLevel.text );
  766. teleportX = mcLevel.teleportArray[i].targetX.text;
  767. teleportY = mcLevel.teleportArray[i].targetY.text;
  768. }
  769. }
  770. if (!foundGround) {
  771. //We didnt collide
  772. grounded = false;
  773. touchingSlope = "not";
  774. //put the player back to where it was before collision tests
  775. mcLevel.mcPlayer.y -= 1;
  776. }
  777. //## make a record of the players position for next frames collision tests
  778. lastPos.x = mcLevel.mcPlayer.x;
  779. lastPos.y = mcLevel.mcPlayer.y;
  780.  
  781. } else {
  782. //talking
  783.  
  784. }
  785. }
  786. //############################# Player Animation ################################
  787. targetAnimation = "IDLE";
  788. if (KEYRIGHTIN) {
  789. if (!onLadder) {
  790. facingDirection = "R";
  791. }
  792. } else if (KEYLEFTIN) {
  793. if (!onLadder) {
  794. facingDirection = "L";
  795. }
  796. }
  797. if (grounded) {
  798. if (KEYRIGHTIN || KEYLEFTIN) {
  799. targetAnimation = "RUN";
  800. } else {
  801. tempVar = Math.sqrt(xMove*xMove);
  802. if (tempVar > 0.5) {
  803. targetAnimation = "SLIDE";
  804. }
  805. }
  806. } else {
  807. //not grounded
  808. if ( yMove < 0 ) {
  809. targetAnimation = "RISE";
  810. } else {
  811. targetAnimation = "FALL";
  812. }
  813. }
  814. if (facingDirection == "R") {
  815. mcLevel.mcPlayer.scaleX = 1;
  816. } else {
  817. mcLevel.mcPlayer.scaleX = -1;
  818. }
  819. if (dying) {
  820. targetAnimation = "DIE";
  821. }
  822. if (climbing) {
  823. targetAnimation = "CLIMB";
  824. }
  825. if (targetAnimation != "FALL") {
  826. //prevent slope spazing glitch
  827. targetAnimationB = targetAnimation;
  828. }
  829. if ( targetAnimation != currentAnimation && targetAnimation == targetAnimationB ) {
  830. switch (targetAnimation) {
  831. case "IDLE" :
  832. mcLevel.mcPlayer.gotoAndStop(1);
  833. break;
  834. case "RUN" :
  835. mcLevel.mcPlayer.gotoAndStop(2);
  836. break;
  837. case "SLIDE" :
  838. mcLevel.mcPlayer.gotoAndStop(3);
  839. break;
  840. case "RISE" :
  841. mcLevel.mcPlayer.gotoAndStop(4);
  842. break;
  843. case "FALL" :
  844. mcLevel.mcPlayer.gotoAndStop(5);
  845. break;
  846. case "DIE" :
  847. mcLevel.mcPlayer.gotoAndStop(6);
  848. break;
  849. case "CLIMB" :
  850. //sort out the animations below
  851. //should work if I put it here but it doesent
  852. //I'll figure it out later ^___^
  853. break;
  854. }
  855. currentAnimation = targetAnimation;
  856. }
  857. if (climbing) {
  858. if (KEYLEFTIN && !onLadder) {
  859. if (!onLadder) {
  860. if (mcLevel.mcPlayer.currentFrame!=9) {
  861. mcLevel.mcPlayer.gotoAndStop(9);
  862. }
  863. }
  864. } else if (KEYRIGHTIN && !onLadder) {
  865. if (!onLadder) {
  866. if (mcLevel.mcPlayer.currentFrame!=9) {
  867. mcLevel.mcPlayer.gotoAndStop(9);
  868. }
  869. }
  870. } else if (KEYCLIMBUPIN) {
  871. if (mcLevel.mcPlayer.currentFrame!=8) {
  872. mcLevel.mcPlayer.gotoAndStop(8);
  873. }
  874. } else if (KEYCLIMBDOWNIN) {
  875. if (mcLevel.mcPlayer.currentFrame!=10) {
  876. mcLevel.mcPlayer.gotoAndStop(10);
  877. }
  878. } else {
  879. if (mcLevel.mcPlayer.currentFrame!=7) {
  880. mcLevel.mcPlayer.gotoAndStop(7);
  881. }
  882. }
  883. }
  884. //prevent slope spazing glitch
  885. targetAnimationB = targetAnimation;
  886.  
  887. if (conveying) {
  888. if (targetAnimationB == "SLIDE") {
  889. targetAnimationB = "IDLE";
  890. targetAnimation = "IDLE";
  891. mcLevel.mcPlayer.gotoAndStop(1);
  892. }
  893. }
  894. //respawn after dying
  895. if (targetAnimation == "DIE") {
  896. if (mcLevel.mcPlayer.deathAnim != null && mcLevel.mcPlayer.deathAnim.currentFrame == mcLevel.mcPlayer.deathAnim.totalFrames) {
  897. dying = false;
  898. mcLevel.mcPlayer.x = safeSpot.x;
  899. mcLevel.mcPlayer.y = safeSpot.y;
  900. }
  901. }
  902. //############################# Level Positioning ################################
  903. //## Center player in screen
  904. if (scrollingMode) {
  905. mcLevel.x = -mcLevel.mcPlayer.x + (STAGEWIDTH / 2);
  906. mcLevel.y = -mcLevel.mcPlayer.y + (STAGEHEIGHT / 2);
  907. //# parralax Scrolling
  908. for (i=0; i<parralaxArray.length; i++) {
  909. parralaxArray[i][0].x = -(mcLevel.mcPlayer.x*parralaxArray[i][1]);
  910. parralaxArray[i][0].y = -(mcLevel.mcPlayer.y*parralaxArray[i][1]);
  911. }
  912. if (ScrollLimitLeft.x!=0) {
  913. if (mcLevel.x > -ScrollLimitLeft.x) {
  914. mcLevel.x = -ScrollLimitLeft.x;
  915. }
  916. }
  917. if (ScrollLimitTop.y!=0) {
  918. if (mcLevel.y > -ScrollLimitTop.y) {
  919. mcLevel.y = -ScrollLimitTop.y;
  920. }
  921. }
  922. if (ScrollLimitBottom.y!=0) {
  923. if (mcLevel.y < stage.stageHeight - ScrollLimitBottom.y) {
  924. mcLevel.y = stage.stageHeight - ScrollLimitBottom.y;
  925. }
  926. }
  927. if (ScrollLimitRight.x!=0) {
  928. if (mcLevel.x < stage.stageWidth - ScrollLimitRight.x) {
  929. mcLevel.x = stage.stageWidth - ScrollLimitRight.x;
  930. }
  931. }
  932. }
  933. //############################# Level Teleporting ################################
  934. if (teleporting) {
  935. if ( teleportFrame != currentFrame ) {
  936. //teleport to different level
  937. ScrollLimitLeft = new MovieClip();
  938. ScrollLimitRight = new MovieClip();
  939. ScrollLimitBottom = new MovieClip();
  940. ScrollLimitTop = new MovieClip();
  941.  
  942.  
  943. KEYLEFTIN = false;
  944. KEYRIGHTIN = false;
  945. KEYJUMPIN = false;
  946. KEYACTIONIN = false;
  947. KEYCLIMBUPIN = false;
  948. KEYCLIMBDOWNIN = false;
  949.  
  950. stage.removeEventListener( Event.ENTER_FRAME, onGameTick );
  951. gotoAndStop( teleportFrame );
  952. } else {
  953. //teleport to somewhere in current level
  954. if ( teleportX != "-" ) {
  955. mcLevel.mcPlayer.x = Number(teleportX);
  956. lastPos.x = mcLevel.mcPlayer.x;
  957. }
  958. if ( teleportY != "-" ) {
  959. mcLevel.mcPlayer.y = Number(teleportY);
  960. lastPos.y = mcLevel.mcPlayer.y;
  961. }
  962. }
  963. //reset some variables
  964.  
  965. grounded = false;
  966. touchingSlope = "not";
  967. teleporting = false;
  968. }
  969. }
Add Comment
Please, Sign In to add comment