Advertisement
Guest User

Untitled

a guest
Jun 7th, 2014
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.60 KB | None | 0 0
  1. package ;
  2. import flixel.FlxG;
  3. import flixel.FlxSprite;
  4. import flixel.group.FlxGroup;
  5. import flixel.text.FlxText;
  6. import flixel.util.FlxColor;
  7. import flixel.util.FlxTimer;
  8.  
  9. /**
  10. * ...
  11. * @author Victor Grunn
  12. */
  13. class TestGroup extends FlxGroup
  14. {
  15. private var statusText:FlxText;
  16. private var finalStatus:FlxText;
  17. private var mapArray:Array < Array < Occupant_Land >> ;
  18.  
  19. public static var occupantsArray:Array <Occupant_Creature> ;
  20.  
  21. private var mapGroup:FlxTypedGroup<Occupant_Land>;
  22. private var occupantsGroup:FlxTypedGroup<Occupant_Creature>;
  23.  
  24. public static var mapSize:Int = 50;
  25. public static var pieceSize:Int = 5;
  26. public static var mapAccessArray:Array < Array < Occupant_Land >> ;
  27.  
  28. public static var month:Int = 0;
  29. public static var year:Int = 0;
  30.  
  31. public static var TotalWoodThisYear:Int = 0;
  32. public static var TotalWoodEver:Int = 0;
  33.  
  34. public static var totalLivingBears:Int = 0;
  35. public static var totalLivingLumberjacks:Int = 0;
  36.  
  37. public static var totalDeadLumberjacks:Int = 0;
  38. public static var totalDeadLumberjacksThisYear:Int = 0;
  39.  
  40. public static var totalElderTreesLeft:Int = 0;
  41. public static var totalNormalTreesLeft:Int = 0;
  42. public static var totalSaplingsLeft:Int = 0;
  43.  
  44. private var gameTimer:FlxTimer;
  45.  
  46. public function new()
  47. {
  48. super();
  49.  
  50. trace("LOADED.");
  51.  
  52. mapArray = new Array < Array < Occupant_Land >> ();
  53. TestGroup.occupantsArray = new Array <Occupant_Creature > ();
  54.  
  55. mapAccessArray = mapArray;
  56.  
  57. statusText = new FlxText(0, 0, 350);
  58. statusText.setFormat(null, 12, FlxColor.WHITE, "left");
  59. statusText.text = "Test.";
  60. add(statusText);
  61.  
  62. mapGroup = new FlxTypedGroup<Occupant_Land>();
  63. add(mapGroup);
  64.  
  65. occupantsGroup = new FlxTypedGroup<Occupant_Creature>();
  66. add(occupantsGroup);
  67.  
  68. initMap();
  69.  
  70. gameTimer = new FlxTimer(.2, onTimerComplete, 0);
  71.  
  72. FlxG.watch.add(TestGroup.occupantsArray, "length", "Occupants: ");
  73. }
  74.  
  75. public function updateText():Void
  76. {
  77. statusText.text =
  78. "Total Wood Gathered Ever: " + TestGroup.TotalWoodEver +
  79. "\nTotal Wood Gather This Year: " + TestGroup.TotalWoodThisYear +
  80. "\nTotal Dead Lumberjacks Ever: " + TestGroup.totalDeadLumberjacks +
  81. "\nTotal Dead Lumberjacks This Year: " + TestGroup.totalDeadLumberjacksThisYear +
  82. "\nTotal Living Bears: " + TestGroup.totalLivingBears +
  83. "\nTotal Living Lumberjacks: " + TestGroup.totalLivingLumberjacks +
  84. "\nTotal Elder Trees Left: " + TestGroup.totalElderTreesLeft +
  85. "\nTotal Normal Trees Left: " + TestGroup.totalNormalTreesLeft +
  86. "\nTotal Saplings Left: " + TestGroup.totalSaplingsLeft +
  87. "\nYear: " + TestGroup.year +
  88. "\nMonth: " + TestGroup.month;
  89. }
  90.  
  91. private function onTimerComplete(t:FlxTimer):Void
  92. {
  93. TestGroup.totalElderTreesLeft = 0;
  94. TestGroup.totalNormalTreesLeft = 0;
  95. TestGroup.totalSaplingsLeft = 0;
  96.  
  97. for (i in 0...mapArray.length)
  98. {
  99. for (o in 0...mapArray[i].length)
  100. {
  101. mapArray[i][o].ageOneMonth();
  102.  
  103. if (mapArray[i][o].ID_TYPE == OCCTYPE_LAND.TREE_ELDER)
  104. {
  105. totalElderTreesLeft += 1;
  106. }
  107. else if (mapArray[i][o].ID_TYPE == OCCTYPE_LAND.TREE_TREE)
  108. {
  109. totalNormalTreesLeft += 1;
  110. }
  111. else if (mapArray[i][o].ID_TYPE == OCCTYPE_LAND.TREE_SAPLING)
  112. {
  113. totalSaplingsLeft += 1;
  114. }
  115. }
  116. }
  117.  
  118. for (o in 0...TestGroup.occupantsArray.length)
  119. {
  120. if (TestGroup.occupantsArray[o] != null)
  121. {
  122. TestGroup.occupantsArray[o].roam();
  123. }
  124. }
  125.  
  126. TestGroup.totalLivingBears = 0;
  127. TestGroup.totalLivingLumberjacks = 0;
  128.  
  129. for (i in 0...TestGroup.occupantsArray.length)
  130. {
  131. if (TestGroup.occupantsArray[i] != null)
  132. {
  133. if (TestGroup.occupantsArray[i].exists)
  134. {
  135. if (TestGroup.occupantsArray[i].ID_TYPE == OCCTYPE_CREATURE.LUMBERJACK)
  136. {
  137. TestGroup.totalLivingLumberjacks += 1;
  138. }
  139.  
  140. if (TestGroup.occupantsArray[i].ID_TYPE == OCCTYPE_CREATURE.BEAR)
  141. {
  142. TestGroup.totalLivingBears += 1;
  143. }
  144. }
  145. }
  146. }
  147.  
  148. TestGroup.month += 1;
  149.  
  150. if (TestGroup.month == 12)
  151. {
  152. if (TotalWoodThisYear < totalLivingLumberjacks)
  153. {
  154. for (i in 0...occupantsArray.length)
  155. {
  156. if (occupantsArray[i] != null && occupantsArray[i].exists && occupantsArray[i].ID_TYPE == OCCTYPE_CREATURE.LUMBERJACK)
  157. {
  158. occupantsArray[i].currentLocation.removeOccupant(occupantsArray[i]);
  159. occupantsArray[i].exists = false;
  160. occupantsArray.remove(occupantsArray[i]);
  161. break;
  162. }
  163. }
  164. }
  165. else if (TotalWoodThisYear >= totalLivingLumberjacks)
  166. {
  167. var woodScope:Int = TotalWoodThisYear - totalLivingLumberjacks;
  168.  
  169. var hires:Int = Math.floor(woodScope / 10);
  170. if (hires <= 0)
  171. {
  172. hires = 1;
  173. }
  174.  
  175. for (i in 0...hires)
  176. {
  177. generateJack();
  178. }
  179. }
  180.  
  181. if (totalDeadLumberjacksThisYear > 0)
  182. {
  183. for (i in 0...occupantsArray.length)
  184. {
  185. if (occupantsArray[i] != null && occupantsArray[i].exists && occupantsArray[i].ID_TYPE == OCCTYPE_CREATURE.BEAR)
  186. {
  187. occupantsArray[i].currentLocation.removeOccupant(occupantsArray[i]);
  188. occupantsArray[i].exists = false;
  189. occupantsArray.remove(occupantsArray[i]);
  190. break;
  191. }
  192. }
  193. }
  194. else
  195. {
  196. generateBear();
  197. }
  198.  
  199. TestGroup.month = 0;
  200. TestGroup.year += 1;
  201. TestGroup.TotalWoodThisYear = 0;
  202. TestGroup.totalDeadLumberjacksThisYear = 0;
  203. }
  204.  
  205. updateText();
  206.  
  207. if (totalElderTreesLeft == 0 && totalNormalTreesLeft == 0 && totalSaplingsLeft == 0)
  208. {
  209. gameTimer.cancel();
  210. finalStatus = new FlxText(0, 0, FlxG.width, "AND THE LORAX WEPT", 30);
  211. finalStatus.alignment = "center";
  212. finalStatus.draw();
  213. finalStatus.y = FlxG.height - finalStatus.height;
  214. add(finalStatus);
  215. }
  216.  
  217. if (year == 400)
  218. {
  219. gameTimer.cancel();
  220. finalStatus = new FlxText(0, 0, FlxG.width, "AND THE LORAX WEPT", 30);
  221. finalStatus.alignment = "center";
  222. finalStatus.draw();
  223. finalStatus.y = FlxG.height - finalStatus.height;
  224. add(finalStatus);
  225. }
  226. }
  227.  
  228. public function initMap():Void
  229. {
  230. for (i in 0...mapSize)
  231. {
  232. mapArray[i] = new Array<Occupant_Land>();
  233.  
  234. for (o in 0...mapSize)
  235. {
  236. mapArray[i][o] = mapGroup.recycle(Occupant_Land, [OCCTYPE_LAND.EMPTY, mapArray]);
  237. mapArray[i][o].x = (FlxG.width - (mapSize * pieceSize)) + i * pieceSize;
  238. mapArray[i][o].y = o * pieceSize;
  239. mapArray[i][o].xSlot = i;
  240. mapArray[i][o].ySlot = o;
  241. }
  242. }
  243.  
  244. var treeCount:Int = Std.int((mapSize * mapSize) * .5);
  245.  
  246. while (treeCount > 0)
  247. {
  248. var checkX:Int = Math.floor(Math.random() * mapSize);
  249. var checkY:Int = Math.floor(Math.random() * mapSize);
  250.  
  251. if (mapArray[checkX][checkY].ID_TYPE == OCCTYPE_LAND.EMPTY)
  252. {
  253. mapArray[checkX][checkY].setType(OCCTYPE_LAND.TREE_TREE);
  254. treeCount -= 1;
  255. }
  256. }
  257.  
  258. trace("Map initialized.");
  259. initOccupants();
  260. }
  261.  
  262. private function generateJack():Void
  263. {
  264. var checkX:Int = Math.floor(Math.random() * mapSize);
  265. var checkY:Int = Math.floor(Math.random() * mapSize);
  266.  
  267. if (mapArray[checkX][checkY].occupying.length == 0)
  268. {
  269. var jack:Occupant_Creature = occupantsGroup.recycle(Occupant_Creature, [OCCTYPE_CREATURE.LUMBERJACK]);
  270. jack.setType(OCCTYPE_CREATURE.LUMBERJACK);
  271. jack.xSlot = checkX;
  272. jack.ySlot = checkY;
  273. mapArray[checkX][checkY].setOccupant(jack);
  274. TestGroup.occupantsArray.push(jack);
  275. }
  276. }
  277.  
  278. private function generateBear():Void
  279. {
  280. var checkX:Int = Math.floor(Math.random() * mapSize);
  281. var checkY:Int = Math.floor(Math.random() * mapSize);
  282.  
  283. if (mapArray[checkX][checkY].occupying.length == 0)
  284. {
  285. var bear:Occupant_Creature = occupantsGroup.recycle(Occupant_Creature, [OCCTYPE_CREATURE.BEAR]);
  286. bear.setType(OCCTYPE_CREATURE.BEAR);
  287. bear.xSlot = checkX;
  288. bear.ySlot = checkY;
  289. mapArray[checkX][checkY].setOccupant(bear);
  290. TestGroup.occupantsArray.push(bear);
  291. }
  292. }
  293.  
  294. private function initOccupants():Void
  295. {
  296. var jackCount:Int = Std.int((mapSize * mapSize) * .10);
  297. var bearCount:Int = Std.int((mapSize * mapSize) * .02);
  298.  
  299. while (jackCount > 0)
  300. {
  301. jackCount -= 1;
  302. generateJack();
  303. }
  304.  
  305. while (bearCount > 0)
  306. {
  307. bearCount -= 1;
  308. generateBear();
  309. }
  310.  
  311. trace("Occupants initialized.");
  312. }
  313.  
  314. }
  315.  
  316. class Occupant_Land extends FlxSprite
  317. {
  318. private var mapArray:Array < Array < Occupant_Land >> ;
  319. public var ID_TYPE:OCCTYPE_LAND;
  320. public var occupying:Array<Occupant_Creature>;
  321. private var age:Int = 0;
  322.  
  323. public var xSlot:Int = 0;
  324. public var ySlot:Int = 0;
  325.  
  326. public function new(_type:OCCTYPE_LAND, _mapArray:Array<Array<Occupant_Land>>)
  327. {
  328. super();
  329.  
  330. setType(_type);
  331. occupying = new Array<Occupant_Creature>();
  332. }
  333.  
  334. public function ageOneMonth():Void
  335. {
  336. if (ID_TYPE != OCCTYPE_LAND.EMPTY)
  337. {
  338. age += 1;
  339.  
  340. if (age >= 12)
  341. {
  342. age = 0;
  343.  
  344. if (ID_TYPE == OCCTYPE_LAND.TREE_SAPLING)
  345. {
  346. setType(OCCTYPE_LAND.TREE_TREE);
  347. }
  348. else if (ID_TYPE == OCCTYPE_LAND.TREE_TREE)
  349. {
  350. setType(OCCTYPE_LAND.TREE_ELDER);
  351. }
  352. }
  353.  
  354. if (ID_TYPE == OCCTYPE_LAND.TREE_TREE || ID_TYPE == OCCTYPE_LAND.TREE_ELDER)
  355. {
  356. spawnSapling(ID_TYPE);
  357. }
  358. }
  359. }
  360.  
  361. private function spawnSapling(_type:OCCTYPE_LAND):Void
  362. {
  363. var threshold:Float = .8;
  364.  
  365. if (_type == OCCTYPE_LAND.TREE_ELDER)
  366. {
  367. threshold = .8;
  368. }
  369. else
  370. {
  371. threshold = .9;
  372. }
  373.  
  374. if (Math.random() > threshold)
  375. {
  376. var myX:Int = 0;
  377. var myY:Int = 0;
  378.  
  379. myX = xSlot;
  380. myY = ySlot;
  381.  
  382. var testArray = new Array<ArrayChecker>();
  383. var checkArray = new Array<ArrayChecker>();
  384.  
  385. testArray[0] = new ArrayChecker(myX - 1, myY - 1);
  386. testArray[1] = new ArrayChecker(myX, myY - 1);
  387. testArray[2] = new ArrayChecker(myX + 1, myY - 1);
  388. testArray[3] = new ArrayChecker(myX - 1, myY);
  389. testArray[4] = new ArrayChecker(myX + 1, myY);
  390. testArray[5] = new ArrayChecker(myX - 1, myY + 1);
  391. testArray[6] = new ArrayChecker(myX, myY + 1);
  392. testArray[7] = new ArrayChecker(myX + 1, myY + 1);
  393.  
  394. var foundMatch:Bool = false;
  395. var spawnChecker:ArrayChecker = new ArrayChecker(0, 0);
  396.  
  397. for (i in 0...testArray.length)
  398. {
  399. if (testArray[i].x < 0 || testArray[i].x > TestGroup.mapSize - 1 || testArray[i].y < 0 || testArray[i].y > TestGroup.mapSize - 1)
  400. {
  401. checkArray.push(testArray[i]);
  402. }
  403. }
  404.  
  405. for (i in 0...checkArray.length)
  406. {
  407. testArray.remove(checkArray[i]);
  408. }
  409.  
  410. while (checkArray.length > 0)
  411. {
  412. checkArray.pop();
  413. }
  414.  
  415. while (testArray.length > 0 && foundMatch == false)
  416. {
  417. //var tester:ArrayChecker = testArray.pop();
  418. var tester:ArrayChecker = testArray[Math.floor(Math.random() * testArray.length)];
  419. foundMatch = checkSapling(tester.x, tester.y);
  420.  
  421. if (foundMatch)
  422. {
  423. spawnChecker = tester;
  424. for (i in 0...testArray.length)
  425. {
  426. testArray[i] = null;
  427. }
  428. }
  429. else
  430. {
  431. testArray.remove(tester);
  432. }
  433. }
  434.  
  435. if (foundMatch)
  436. {
  437. if (spawnChecker != null)
  438. {
  439. TestGroup.mapAccessArray[spawnChecker.x][spawnChecker.y].setType(OCCTYPE_LAND.TREE_SAPLING);
  440. }
  441. }
  442. }
  443. }
  444.  
  445. private function checkSapling(_x:Int, _y:Int):Bool
  446. {
  447. if (TestGroup.mapAccessArray[_x][_y].ID_TYPE != null && TestGroup.mapAccessArray[_x][_y].ID_TYPE == OCCTYPE_LAND.EMPTY)
  448. {
  449. return true;
  450. }
  451.  
  452. return false;
  453. }
  454.  
  455. public function setOccupant(_creature:Occupant_Creature):Void
  456. {
  457. occupying.push(_creature);
  458. _creature.x = this.x;
  459. _creature.y = this.y;
  460. _creature.xSlot = xSlot;
  461. _creature.ySlot = ySlot;
  462. if (_creature.currentLocation != null)
  463. {
  464. _creature.currentLocation.removeOccupant(_creature);
  465. }
  466. _creature.currentLocation = this;
  467.  
  468. resolveOccupants(_creature);
  469. }
  470.  
  471. public function removeOccupant(_creature:Occupant_Creature):Void
  472. {
  473. occupying.remove(_creature);
  474. }
  475.  
  476. private function resolveOccupants(_creature:Occupant_Creature):Void
  477. {
  478. switch(_creature.ID_TYPE)
  479. {
  480. case OCCTYPE_CREATURE.LUMBERJACK:
  481. for (i in 0...occupying.length)
  482. {
  483. if (occupying[i] != null && occupying[i].ID_TYPE == OCCTYPE_CREATURE.BEAR)
  484. {
  485. TestGroup.totalDeadLumberjacks += 1;
  486. TestGroup.occupantsArray.remove(_creature);
  487. _creature.exists = false;
  488. _creature.stopMoving();
  489. removeOccupant(_creature);
  490. return;
  491. }
  492. }
  493.  
  494. if (ID_TYPE == OCCTYPE_LAND.TREE_TREE)
  495. {
  496. setType(OCCTYPE_LAND.EMPTY);
  497. TestGroup.TotalWoodEver += 1;
  498. TestGroup.TotalWoodThisYear += 1;
  499. _creature.stopMoving();
  500. }
  501.  
  502. if (ID_TYPE == OCCTYPE_LAND.TREE_ELDER)
  503. {
  504. setType(OCCTYPE_LAND.EMPTY);
  505. TestGroup.TotalWoodEver += 2;
  506. TestGroup.TotalWoodThisYear += 2;
  507. _creature.stopMoving();
  508. }
  509.  
  510. case OCCTYPE_CREATURE.BEAR:
  511. for (i in 0...occupying.length)
  512. {
  513. if (occupying[i] != null && occupying[i].ID_TYPE == OCCTYPE_CREATURE.LUMBERJACK)
  514. {
  515. TestGroup.totalDeadLumberjacks += 1;
  516. TestGroup.totalDeadLumberjacksThisYear += 1;
  517. occupying[i].exists = false;
  518. TestGroup.occupantsArray.remove(occupying[i]);
  519. _creature.stopMoving();
  520. removeOccupant(occupying[i]);
  521. }
  522. }
  523.  
  524. }
  525. }
  526.  
  527. public function setType(_type:OCCTYPE_LAND):Void
  528. {
  529. ID_TYPE = _type;
  530.  
  531. switch (ID_TYPE)
  532. {
  533. case OCCTYPE_LAND.EMPTY:
  534. age = 0;
  535. makeGraphic(TestGroup.pieceSize, TestGroup.pieceSize, 0xffccff00);
  536.  
  537. case OCCTYPE_LAND.TREE_ELDER:
  538. age = 0;
  539. makeGraphic(TestGroup.pieceSize, TestGroup.pieceSize, 0xff336633);
  540.  
  541. case OCCTYPE_LAND.TREE_SAPLING:
  542. age = 0;
  543. makeGraphic(TestGroup.pieceSize, TestGroup.pieceSize, 0xff00ff33);
  544.  
  545. case OCCTYPE_LAND.TREE_TREE:
  546. age = 0;
  547. makeGraphic(TestGroup.pieceSize, TestGroup.pieceSize, 0xff009966);
  548. }
  549. }
  550. }
  551.  
  552. class Occupant_Creature extends FlxSprite
  553. {
  554. public var ID_TYPE:OCCTYPE_CREATURE;
  555. public var currentLocation:Occupant_Land;
  556.  
  557. public var xSlot:Int = 0;
  558. public var ySlot:Int = 0;
  559.  
  560. private var movesLeft:Int = 0;
  561.  
  562. public function new(_type:OCCTYPE_CREATURE)
  563. {
  564. super();
  565.  
  566. setType(_type);
  567. }
  568.  
  569. public function setType(_type:OCCTYPE_CREATURE)
  570. {
  571. ID_TYPE = _type;
  572. exists = true;
  573.  
  574. switch (ID_TYPE)
  575. {
  576. case OCCTYPE_CREATURE.BEAR:
  577. makeGraphic(TestGroup.pieceSize, TestGroup.pieceSize, FlxColor.RED);
  578.  
  579. case OCCTYPE_CREATURE.LUMBERJACK:
  580. makeGraphic(TestGroup.pieceSize, TestGroup.pieceSize, FlxColor.BLUE);
  581. }
  582. }
  583.  
  584. public function roam():Void
  585. {
  586. if (ID_TYPE == OCCTYPE_CREATURE.BEAR)
  587. {
  588. movesLeft = 5;
  589. }
  590.  
  591. if (ID_TYPE == OCCTYPE_CREATURE.LUMBERJACK)
  592. {
  593. movesLeft = 3;
  594. }
  595.  
  596. while (movesLeft > 0 && exists == true)
  597. {
  598. makeMove();
  599. }
  600. }
  601.  
  602. public function stopMoving():Void
  603. {
  604. movesLeft = 0;
  605. }
  606.  
  607. private function makeMove():Void
  608. {
  609. var myX:Int = 0;
  610. var myY:Int = 0;
  611.  
  612. myX = xSlot;
  613. myY = ySlot;
  614.  
  615. var testArray = new Array<ArrayChecker>();
  616. var checkArray = new Array<ArrayChecker>();
  617.  
  618. testArray[0] = new ArrayChecker(myX - 1, myY - 1);
  619. testArray[1] = new ArrayChecker(myX, myY - 1);
  620. testArray[2] = new ArrayChecker(myX + 1, myY - 1);
  621. testArray[3] = new ArrayChecker(myX - 1, myY);
  622. testArray[4] = new ArrayChecker(myX + 1, myY);
  623. testArray[5] = new ArrayChecker(myX - 1, myY + 1);
  624. testArray[6] = new ArrayChecker(myX, myY + 1);
  625. testArray[7] = new ArrayChecker(myX + 1, myY + 1);
  626.  
  627. var spawnChecker:ArrayChecker = new ArrayChecker(0, 0);
  628.  
  629. for (i in 0...testArray.length)
  630. {
  631. if (testArray[i].x < 0 || testArray[i].x > TestGroup.mapSize - 1 || testArray[i].y < 0 || testArray[i].y > TestGroup.mapSize - 1)
  632. {
  633. checkArray.push(testArray[i]);
  634. }
  635. }
  636.  
  637. for (i in 0...checkArray.length)
  638. {
  639. testArray.remove(checkArray[i]);
  640. }
  641.  
  642. while (checkArray.length > 0)
  643. {
  644. checkArray.pop();
  645. }
  646.  
  647. var usingCheck:ArrayChecker = testArray[Math.floor(Math.random() * testArray.length)];
  648.  
  649. for (i in 0...testArray.length)
  650. {
  651. testArray[i] = null;
  652. }
  653.  
  654. movesLeft -= 1;
  655. TestGroup.mapAccessArray[usingCheck.x][usingCheck.y].setOccupant(this);
  656. }
  657. }
  658.  
  659. enum OCCTYPE_LAND
  660. {
  661. EMPTY;
  662. TREE_SAPLING;
  663. TREE_TREE;
  664. TREE_ELDER;
  665. }
  666.  
  667. enum OCCTYPE_CREATURE
  668. {
  669. BEAR;
  670. LUMBERJACK;
  671. }
  672.  
  673.  
  674. class ArrayChecker
  675. {
  676. public var x:Int;
  677. public var y:Int;
  678.  
  679. public function new(_x:Int, _y:Int)
  680. {
  681. x = _x;
  682. y = _y;
  683. }
  684. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement