Guest User

Untitled

a guest
Feb 20th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.34 KB | None | 0 0
  1. import PathDuration from "@/core/pathfinder/PathDuration";
  2. import { MapChangeDirections } from "@/game/managers/movements/MapChangeDirections";
  3. import DTConstants from "@/protocol/DTConstants";
  4. import Color from "@/utils/Color";
  5. import Point from "@/utils/Point";
  6. import { sleep } from "@/utils/Time";
  7. import Account from "@account";
  8. import { List } from "linqts";
  9. import * as React from "react";
  10. import { Col, Container, Row } from "reactstrap";
  11. import MapViewerCell from "./MapViewerCell";
  12.  
  13. interface IMapViewerProps {
  14. account: Account;
  15. }
  16.  
  17. interface IMapViewerStates {
  18. path: number[];
  19. selectedCellId: number;
  20. showCellIds: boolean;
  21. }
  22.  
  23. export default class MapViewer extends React.Component<IMapViewerProps, IMapViewerStates> {
  24. public cells: List<MapViewerCell>;
  25.  
  26. private readonly walkableCellBrush = new Color(145, 145, 148);
  27. private readonly losCellBrush = new Color(77, 77, 77);
  28. private readonly obstacleCellBrush = new Color(45, 45, 48);
  29. private readonly selectedCellBrush = new Color("rgba(255, 91, 90, 0.35)");
  30. private readonly ourPlayerBrush = new Color(0, 0, 255);
  31. private readonly monstersGroupsBrush = new Color("rgba(255, 139, 0, 0)");
  32. private readonly playersBrush = new Color("rgba(255, 81, 113, 0.79)");
  33. private readonly doorsBrush = new Color("rgba(255, 150, 75, 0.05)");
  34. private readonly interactivesBrush = new Color("rgba(255, 1, 143, 0.55)");
  35. private readonly npcsBrush = new Color("rgba(255, 179, 120, 0.82)");
  36. private readonly sunImage = "21000.png";
  37. private readonly phenixImage = "7521.png";
  38. private readonly lockedStorageImage = "12367.png";
  39.  
  40. constructor(props: IMapViewerProps) {
  41. super(props);
  42.  
  43. this.state = {
  44. path: [],
  45. selectedCellId: -1,
  46. showCellIds: true,
  47. };
  48.  
  49. this.initCells();
  50. }
  51.  
  52. public componentDidMount() {
  53. this.props.account.game.map.MapChanged.on(this.refreshMapViewer.bind(this));
  54. this.props.account.game.map.EntitiesUpdated.on(this.refreshMapViewer.bind(this));
  55. this.props.account.game.map.InteractivesUpdated.on(this.refreshMapViewer.bind(this));
  56. this.props.account.game.map.PlayedCharacterMoving.on(this.playedCharacterMoving.bind(this));
  57. this.props.account.game.fight.FightJoined.on(this.refreshMapViewer.bind(this));
  58. this.props.account.game.fight.PossiblePositionsReceived.on(this.refreshMapViewer.bind(this));
  59. this.props.account.game.fight.FightStarted.on(this.refreshMapViewer.bind(this));
  60. this.props.account.game.fight.FightersUpdated.on(this.refreshMapViewer.bind(this));
  61. this.props.account.game.fight.PlayedFighterMoving.on(this.playedCharacterMoving.bind(this));
  62. }
  63.  
  64. public componentWillUnmount() {
  65. this.props.account.game.map.MapChanged.off(this.refreshMapViewer.bind(this));
  66. this.props.account.game.map.EntitiesUpdated.off(this.refreshMapViewer.bind(this));
  67. this.props.account.game.map.InteractivesUpdated.off(this.refreshMapViewer.bind(this));
  68. this.props.account.game.map.PlayedCharacterMoving.off(this.playedCharacterMoving.bind(this));
  69. this.props.account.game.fight.FightJoined.off(this.refreshMapViewer.bind(this));
  70. this.props.account.game.fight.PossiblePositionsReceived.off(this.refreshMapViewer.bind(this));
  71. this.props.account.game.fight.FightStarted.off(this.refreshMapViewer.bind(this));
  72. this.props.account.game.fight.FightersUpdated.off(this.refreshMapViewer.bind(this));
  73. this.props.account.game.fight.PlayedFighterMoving.off(this.playedCharacterMoving.bind(this));
  74. }
  75.  
  76. public render() {
  77. return (
  78. <Container>
  79. <Row>
  80. <Col>
  81. <canvas id="mapStatus"></canvas>
  82. </Col>
  83. </Row>
  84. </Container>
  85. );
  86. }
  87.  
  88. private refreshMapViewer() {
  89. this.buildMap();
  90. }
  91.  
  92. private async playedCharacterMoving(cells: number[]) {
  93. this.setState({ path: cells });
  94. this.buildMap();
  95.  
  96. if (!this.props.account.config.enableSpeedHack) {
  97. await PathDuration.calculate(cells);
  98. }
  99.  
  100. if (this.state.path.length > 0) {
  101. this.setState({ path: [] });
  102. this.buildMap();
  103. }
  104. }
  105.  
  106. private GetCellBrush(cell: number): Color {
  107. // In case the cell is currently selected
  108. if (cell === this.state.selectedCellId) {
  109. return this.selectedCellBrush;
  110. }
  111.  
  112. // In case the cell is a possible placement
  113. if (this.props.account.isFighting && this.props.account.game.fight.positionsForChallengers.Contains(cell) === true) {
  114. return new Color(255, 0, 0);
  115. }
  116.  
  117. if (this.props.account.isFighting && this.props.account.game.fight.positionsForDefenders.Contains(cell) === true) {
  118. return new Color(0, 0, 255);
  119. }
  120.  
  121. let brush = this.losCellBrush;
  122.  
  123. if (this.props.account.game.map.data.cells[cell].isObstacle()) {
  124. brush = this.obstacleCellBrush;
  125. } else if (this.props.account.game.map.data.cells[cell].isWalkable(this.props.account.isFighting)) {
  126. brush = this.walkableCellBrush;
  127. }
  128.  
  129. return brush;
  130. }
  131.  
  132. private DrawTileContent(drawingContext: CanvasRenderingContext2D, cellId: number) {
  133. if (this.props.account.isFighting) {
  134. if (this.props.account.game.fight.playedFighter &&
  135. this.props.account.game.fight.playedFighter.cellId === cellId) {
  136. this.cells.ElementAt(cellId).DrawPie(drawingContext, this.ourPlayerBrush);
  137. } else if (this.props.account.game.fight.allies.find((a) => a.cellId === cellId) !== undefined) {
  138. this.cells.ElementAt(cellId).DrawPie(drawingContext, this.playersBrush);
  139. } else if (this.props.account.game.fight.enemies.find((e) => e.cellId === cellId) !== undefined) {
  140. this.cells.ElementAt(cellId).DrawPie(drawingContext, this.monstersGroupsBrush);
  141. }
  142. } else {
  143. if (this.props.account.game.fight.playedFighter &&
  144. this.props.account.game.map.playedCharacter.cellId === cellId) {
  145. this.cells.ElementAt(cellId).DrawPie(drawingContext, this.ourPlayerBrush);
  146. } else if (this.props.account.game.map.monstersGroups.find((mg) => mg.cellId === cellId) !== undefined) {
  147. this.cells.ElementAt(cellId).DrawPie(drawingContext, this.monstersGroupsBrush);
  148. } else if (this.props.account.game.map.players.find((p) => p.cellId === cellId) !== undefined) {
  149. this.cells.ElementAt(cellId).DrawPie(drawingContext, this.playersBrush);
  150. } else if (this.props.account.game.map.doors.find((d) => d.cellId === cellId) !== undefined) {
  151. this.cells.ElementAt(cellId).DrawRectangle(drawingContext, this.doorsBrush);
  152. } else if (this.props.account.game.map.statedElements.find((se) => se.cellId === cellId) !== undefined ||
  153. this.props.account.game.map.zaap && this.props.account.game.map.zaap.cellId === cellId ||
  154. this.props.account.game.map.zaapi && this.props.account.game.map.zaapi.cellId === cellId) {
  155. this.cells.ElementAt(cellId).DrawRectangle(drawingContext, this.interactivesBrush);
  156. } else if (this.props.account.game.map.npcs.find((n) => n.cellId === cellId) !== undefined) {
  157. this.cells.ElementAt(cellId).DrawPie(drawingContext, this.npcsBrush);
  158. }
  159. }
  160. }
  161.  
  162. private initCells() {
  163. this.cells = new List<MapViewerCell>();
  164. let cell = 0;
  165. for (let i = 0; i < DTConstants.MAP_HEIGHT; i++) {
  166. for (let j = 0; j < DTConstants.MAP_WIDTH * 2; j++) {
  167. const coords = this.cellCoords(cell);
  168. const x = coords.x;
  169. const y = coords.y;
  170. const startPtX = x * (DTConstants.tileWidth) + (y % 2 === 1 ? (DTConstants.tileWidth) / 2 : 0);
  171. const startPtY = y * (DTConstants.tileHeight) / 2;
  172. this.cells.Add(new MapViewerCell([
  173. new Point(startPtX + (DTConstants.tileWidth) / 2, startPtY),
  174. new Point(startPtX + (DTConstants.tileWidth), startPtY + (DTConstants.tileHeight) / 2),
  175. new Point(startPtX + (DTConstants.tileWidth) / 2, startPtY + (DTConstants.tileHeight)),
  176. new Point(startPtX, startPtY + (DTConstants.tileHeight) / 2),
  177. ]));
  178. cell++;
  179. }
  180. }
  181. }
  182.  
  183. private cellCoords(cellId: number) {
  184. return {
  185. x: cellId % DTConstants.MAP_WIDTH, // X
  186. y: Math.floor(cellId / DTConstants.MAP_WIDTH), // Y
  187. };
  188. }
  189.  
  190. private buildMap() {
  191. const c = document.getElementById("mapStatus") as HTMLCanvasElement;
  192. const ctx = c.getContext("2d");
  193. c.width = DTConstants.tileWidth * (DTConstants.MAP_WIDTH + 1);
  194. c.height = DTConstants.tileHeight * (DTConstants.MAP_HEIGHT + 1);
  195.  
  196. c.addEventListener("click", this.onMouseClick.bind(this), false);
  197.  
  198. for (let i = 0; i < this.cells.Count(); i++) {
  199. const brush = this.GetCellBrush(i);
  200.  
  201. if (brush === this.obstacleCellBrush && !this.state.showCellIds) {
  202. this.cells.ElementAt(i).DrawObstacle(ctx, brush);
  203. } else {
  204. this.cells.ElementAt(i).Draw(ctx, brush);
  205.  
  206. if (this.state.path.includes(i) === true) {
  207. this.cells.ElementAt(i).DrawCross(ctx);
  208. }
  209. }
  210.  
  211. if (this.state.showCellIds) {
  212. const cell = this.cells.ElementAt(i);
  213. ctx.font = "10px tahoma";
  214. ctx.fillStyle = brush === this.losCellBrush || brush === this.obstacleCellBrush ? "#fff" : "#000";
  215. ctx.fillText(`${i}`, cell.mid.x - 10, cell.mid.y + 5);
  216. }
  217.  
  218. // Draw the sun image if this cell has it
  219. if (this.props.account.game.map.teleportableCells.includes(i)) {
  220. this.cells.ElementAt(i).DrawImage(ctx, this.sunImage);
  221. } else if (this.props.account.game.map.phenixs.find((p) => p.cellId === i) != null) {
  222. this.cells.ElementAt(i).DrawImage(ctx, this.phenixImage);
  223. } else if (this.props.account.game.map.lockedStorages.find((ls) => ls.cellId === i) != null) {
  224. this.cells.ElementAt(i).DrawImage(ctx, this.lockedStorageImage);
  225. }
  226.  
  227. this.DrawTileContent(ctx, i);
  228. }
  229. }
  230.  
  231. private onMouseClick(event) {
  232. // No need to check if the map is not valid or the bot is not inactif
  233. if (this.props.account.isBusy) {
  234. return;
  235. }
  236.  
  237. const pos = new Point(event.offsetX, event.offsetY);
  238.  
  239. for (let i = 0; i < this.cells.Count(); i++) {
  240. if (this.cells.ElementAt(i).IsPointInside(pos)) {
  241. if (this.props.account.game.map.data.cells[i].isWalkable(false)) {
  242. this.setState({ selectedCellId: i });
  243. this.buildMap();
  244.  
  245. const task = async () => {
  246. await sleep(200);
  247. if (this.state.selectedCellId !== -1) {
  248. this.setState({ selectedCellId: -1 });
  249. this.buildMap();
  250. }
  251. };
  252.  
  253. task();
  254.  
  255. this.HandleWalkableCellClicked(i);
  256. }
  257.  
  258. break;
  259. }
  260. }
  261. }
  262.  
  263. private HandleWalkableCellClicked(cell: number) {
  264. // Check if we can change the map from this cell
  265. if (this.props.account.game.managers.movements.canChangeMap(cell, MapChangeDirections.Left)) {
  266. this.props.account.game.managers.movements.changeMapWithCellId(MapChangeDirections.Left, cell);
  267. } else if (this.props.account.game.managers.movements.canChangeMap(cell, MapChangeDirections.Right)) {
  268. this.props.account.game.managers.movements.changeMapWithCellId(MapChangeDirections.Right, cell);
  269. } else if (this.props.account.game.managers.movements.canChangeMap(cell, MapChangeDirections.Top)) {
  270. this.props.account.game.managers.movements.changeMapWithCellId(MapChangeDirections.Top, cell);
  271. } else if (this.props.account.game.managers.movements.canChangeMap(cell, MapChangeDirections.Bottom)) {
  272. this.props.account.game.managers.movements.changeMapWithCellId(MapChangeDirections.Bottom, cell);
  273. } else {
  274. // Otherwise just move to the cell
  275. this.props.account.game.managers.movements.moveToCell(cell);
  276. }
  277. }
  278. }
Add Comment
Please, Sign In to add comment