PageRenderTime 105ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/Source/Level1.hx

http://collision-detection-game.googlecode.com/
Haxe | 296 lines | 238 code | 49 blank | 9 comment | 8 complexity | 72b3f7465e103383e03de45ede7d581a MD5 | raw file
  1. import flash.display.Sprite;
  2. import flash.display.Bitmap;
  3. import flash.display.BitmapData;
  4. import flash.events.Event;
  5. import flash.geom.Point;
  6. import flash.geom.Rectangle;
  7. import flash.geom.Matrix;
  8. import flash.utils.Timer;
  9. import haxe.FastList;
  10. import haxe.Timer;
  11. import nme.Assets;
  12. import TileModel;
  13. import HealthBarView;
  14. import BaseLevel;
  15. import Kongregate;
  16. import flash.text.TextField;
  17. import flash.text.TextFieldAutoSize;
  18. import flash.text.TextFormat;
  19. import flash.text.TextFormatAlign;
  20. class Level1 extends BaseLevel
  21. {
  22. private var _pinkMap:Array<Array<Int>>;
  23. public var lastDoor:Int;
  24. private static inline var MAX_TILE_SIZE:Int = 64;
  25. private static inline var MAP_COLUMNS:Int = 20;
  26. private static inline var MAP_ROWS:Int = 8;
  27. //tile ID s in tile sheet
  28. private static inline var BOX:Int = 00;
  29. private static inline var SKY:Int = 10;
  30. private static inline var PLATFORM:Int = 11;
  31. private static inline var MACHINE_GUN:Int = 01;
  32. private static inline var PISTOL:Int = 02;
  33. private static inline var SHOTGUN:Int = 03;
  34. private static inline var DOOR:Int = 13;
  35. private static inline var PINK:Int = 21;
  36. private static inline var BLUE:Int = 20;
  37. private static inline var PinkRadius:Float = 20.5;
  38. private static inline var BlueRadius:Float = 20.5;
  39. private var _SpriteLayer:Sprite;
  40. public var DoorHeader1:TextField;
  41. public var DoorHeader2:TextField;
  42. public var DoorHeader3:TextField;
  43. public var DoorHeader4:TextField;
  44. public var DoorHeader5:TextField;
  45. public var Title:TextField;
  46. public var Credits:TextField;
  47. public var Enter:TextField;
  48. public function new(stage:Dynamic){
  49. super(stage);
  50. MAP_COLUMNS = 20;
  51. removeChild(_staticBackgroundBitmap);
  52. removeChild(_backgroundBitmap);
  53. removeChild(_foregroundBitmap);
  54. _staticBackgroundBitmapData = Assets.getBitmapData("Cardboard_Background.png");
  55. _staticBackgroundBitmap = new Bitmap(_staticBackgroundBitmapData);
  56. _backgroundBitmapData = new BitmapData(MAP_COLUMNS * MAX_TILE_SIZE,
  57. MAP_ROWS * MAX_TILE_SIZE, true, 0);
  58. _backgroundBitmap = new Bitmap(_backgroundBitmapData);
  59. _foregroundBitmapData = new BitmapData(MAP_COLUMNS * MAX_TILE_SIZE,
  60. MAP_ROWS * MAX_TILE_SIZE, true, 0);
  61. _foregroundBitmap = new Bitmap(_foregroundBitmapData);
  62. _camera = new Rectangle (0, 0, stage.stageWidth, stage.stageHeight);
  63. _SpriteLayer = new Sprite();
  64. _SpriteLayer.graphics.beginFill(0x0000000,0);
  65. _SpriteLayer.graphics.drawRect(0,0,MAP_COLUMNS * MAX_TILE_SIZE,MAP_ROWS * MAX_TILE_SIZE);
  66. _SpriteLayer.visible = true;
  67. addChild(_staticBackgroundBitmap);
  68. addChild(_backgroundBitmap);
  69. addChild(_SpriteLayer);
  70. addChild(_foregroundBitmap);
  71. _platformMap =
  72. [
  73. [PLATFORM,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,PLATFORM],
  74. [PLATFORM,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,PLATFORM],
  75. [PLATFORM,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,PLATFORM],
  76. [PLATFORM,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,PLATFORM],
  77. [PLATFORM,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,PLATFORM],
  78. [PLATFORM,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,PLATFORM],
  79. [PLATFORM,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,PLATFORM],
  80. [PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM]
  81. ];
  82. _gameObjectMap =
  83. [
  84. [-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],
  85. [-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],
  86. [-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],
  87. [-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],
  88. [-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],
  89. [-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],
  90. [-1,-1,BOX,DOOR,-1,-1,DOOR,-1,-1,DOOR,-1,-1,DOOR,-1,-1,DOOR,-1,-1,-1,-1],
  91. [-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],
  92. ] ;
  93. buildReserveEnemies();
  94. DoorHeader1 = new TextField();
  95. DoorHeader1.htmlText ="Circle Collisions";
  96. DoorHeader1.x = 186;DoorHeader1.y = 360;
  97. DoorHeader1.width = 82;DoorHeader1.height = 24;
  98. _SpriteLayer.addChild(DoorHeader1);
  99. DoorHeader2 = new TextField();
  100. DoorHeader2.htmlText ="Square Collisions";
  101. DoorHeader2.x = 378;DoorHeader2.y = 360;
  102. DoorHeader2.width = 82;DoorHeader2.height = 24;
  103. _SpriteLayer.addChild(DoorHeader2);
  104. DoorHeader3 = new TextField();
  105. DoorHeader3.htmlText ="Circle vs Square";
  106. DoorHeader3.x = 570;DoorHeader3.y = 360;
  107. DoorHeader3.width = 82;DoorHeader3.height = 24;
  108. _SpriteLayer.addChild(DoorHeader3);
  109. DoorHeader4 = new TextField();
  110. DoorHeader4.htmlText ="Circle vs Line";
  111. DoorHeader4.x = 762;DoorHeader4.y = 360;
  112. DoorHeader4.width = 82;DoorHeader4.height = 24;
  113. _SpriteLayer.addChild(DoorHeader4);
  114. DoorHeader5 = new TextField();
  115. DoorHeader5.htmlText ="Collision Arrays";
  116. DoorHeader5.x = 954;DoorHeader5.y = 360;
  117. DoorHeader5.width = 82;DoorHeader5.height = 24;
  118. _SpriteLayer.addChild(DoorHeader5);
  119. Title = new TextField();
  120. var myFormat:TextFormat = new TextFormat();
  121. myFormat.align = TextFormatAlign.CENTER;
  122. Title.defaultTextFormat = myFormat;
  123. Title.htmlText ="<center><font size='24'>Collision Detection and You!</font></center>";
  124. Title.x = 0;Title.y = 0;
  125. Title.width = 1280;Title.height = 48;
  126. _SpriteLayer.addChild(Title);
  127. Credits = new TextField();
  128. var myFormat:TextFormat = new TextFormat();
  129. myFormat.align = TextFormatAlign.CENTER;
  130. Credits.defaultTextFormat = myFormat;
  131. Credits.htmlText ="<center><font size='16'>Music is 'Virtutes Instrumenti' by Kevin MacLeod (incompetech.com)
  132. Licensed under Creative Commons Attribution 3.0 http://creativecommons.org/licenses/by/3.0/
  133. Everything else by zerocreativity1</font></center>";
  134. Credits.x = 0;Credits.y = 55;
  135. Credits.width = 1280;Credits.height = 88;
  136. _SpriteLayer.addChild(Credits);
  137. Enter = new TextField();
  138. var myFormat:TextFormat = new TextFormat();
  139. myFormat.align = TextFormatAlign.CENTER;
  140. Enter.defaultTextFormat = myFormat;
  141. Enter.htmlText ="<font size='16'>Press Enter when next to a door.</font>";
  142. Enter.x = 0;Enter.y = 192;
  143. Enter.width = 1280;Enter.height = 48;
  144. _SpriteLayer.addChild(Enter);
  145. // myTimer = new Timer(12);
  146. // myTimer.addEventListener("timer", OnEnter);
  147. // myTimer.start();
  148. //Add the game bitmaps
  149. //Run the buildMap method to convert the
  150. //map array data into a visual display
  151. }
  152. public function GetExit():String
  153. {
  154. if(!_boxModel.exit)
  155. {return "";}
  156. var Door1 = new SquareModel();
  157. Door1.xPos = 3*64;
  158. Door1.yPos = 6*64;
  159. Door1.width = 64;
  160. Door1.height = 64;
  161. var boxSquare = new SquareModel();
  162. boxSquare.xPos = _boxModel.xPos;
  163. boxSquare.yPos = _boxModel.yPos;
  164. boxSquare.width = _boxModel.width;
  165. boxSquare.height = _boxModel.height;
  166. if(_collisionController.squareModelCollision(Door1,boxSquare))
  167. {
  168. lastDoor = 1;
  169. return "Circle";
  170. }
  171. var Door2 = new SquareModel();
  172. Door2.xPos = 6*64;
  173. Door2.yPos = 6*64;
  174. Door2.width = 64;
  175. Door2.height = 64;
  176. if(_collisionController.squareModelCollision(Door2,boxSquare))
  177. {
  178. lastDoor = 2;
  179. return "Square";
  180. //return "";
  181. }
  182. var Door3 = new SquareModel();
  183. Door3.xPos = 9*64;
  184. Door3.yPos = 6*64;
  185. Door3.width = 64;
  186. Door3.height = 64;
  187. if(_collisionController.squareModelCollision(Door3,boxSquare))
  188. {
  189. lastDoor = 3;
  190. return "CircleSquare";
  191. //return "";
  192. }
  193. var Door4 = new SquareModel();
  194. Door4.xPos = 12*64;
  195. Door4.yPos = 6*64;
  196. Door4.width = 64;
  197. Door4.height = 64;
  198. if(_collisionController.squareModelCollision(Door4,boxSquare))
  199. {
  200. lastDoor = 4;
  201. return "CircleLine";
  202. }
  203. var Door5 = new SquareModel();
  204. Door5.xPos = 15*64;
  205. Door5.yPos = 6*64;
  206. Door5.width = 64;
  207. Door5.height = 64;
  208. if(_collisionController.squareModelCollision(Door5,boxSquare))
  209. {
  210. lastDoor = 5;
  211. return "CollisionArray";
  212. }
  213. return "";
  214. }
  215. override function OnEnter(e:flash.events.Event)
  216. {
  217. super.OnEnter(e);
  218. var level = GetExit();
  219. if(level != "")
  220. {
  221. try
  222. {
  223. kongVar.SubmitStat("NewLevel",1);
  224. }
  225. catch (e:Dynamic)
  226. {
  227. }
  228. _boxModel.exit = false;
  229. UnLoad();
  230. this.removeEventListener(Event.ENTER_FRAME, OnEnter);
  231. var requestLevel = new RequestLevelEvent("RequestLevel",level);
  232. dispatchEvent(requestLevel);return;
  233. }
  234. _boxModel.exit = false;
  235. _SpriteLayer.scrollRect = _camera;
  236. }
  237. public override function Load(inVolume:Bool,inSound:Bool,inKongVar:CKongregate,inDifficulty:Int)
  238. {
  239. super.Load(inVolume,inSound,inKongVar,inDifficulty);
  240. buildMap(_platformMap);
  241. buildMap(_gameObjectMap);
  242. _boxModel.setX = (lastDoor * 3 +2)* MAX_TILE_SIZE;
  243. }
  244. public override function buildReserveEnemies()
  245. {
  246. }
  247. }