Advertisement
Guest User

Untitled

a guest
Feb 21st, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.79 KB | None | 0 0
  1. import Renderer from "../../Renderer/Renderer";
  2. import Generation from "../../GA/Generation";
  3. import Brain from "../../GA/Brain";
  4. import * as PIXI from "pixi.js";
  5. import { Directions } from "./Directions";
  6.  
  7. export default class Character{
  8. sprite: PIXI.Sprite;
  9. velocityX: number = 0;
  10. velocityY: number = 0;
  11. brain: Brain;
  12. alive: boolean = true;
  13. input: number[];
  14. output: number[];
  15. fitness: number;
  16.  
  17. constructor(brain: Brain) {
  18. this.brain = brain;
  19. this.sprite = new PIXI.Sprite.fromImage("images/character.png");
  20. this.setDefaults();
  21. Renderer.app.stage.addChild(this.sprite);
  22.  
  23. this.setKeys();
  24.  
  25. Renderer.app.ticker.add(() => {
  26. this.update();
  27. });
  28. }
  29.  
  30. private setDefaults() {
  31. this.sprite.width = 50;
  32. this.sprite.height = 50;
  33. this.sprite.y = Renderer.app.renderer.height - this.sprite.height - 20;
  34. this.sprite.x = Math.floor(Math.random() * Renderer.app.renderer.width / 2) + Renderer.app.renderer.width / 4
  35. }
  36.  
  37. public update() {
  38. if (!this.alive) return;
  39. this.brainWork();
  40. for (let obstacle of Generation.obstacles) {
  41. if (this.sprite.x < obstacle.sprite.x + obstacle.sprite.width &&
  42. this.sprite.x + this.sprite.width > obstacle.sprite.x &&
  43. this.sprite.y < obstacle.sprite.y + obstacle.sprite.height &&
  44. this.sprite.y + this.sprite.height > obstacle.sprite.y) {
  45. this.kill();
  46. }
  47. }
  48. this.sprite.x += this.velocityX;
  49. this.sprite.y += this.velocityY;
  50. this.velocityX += this.velocityX > 0 ? - 0.5 : this.velocityX == 0 ? 0 : 0.5;
  51. this.velocityY += this.velocityY > 0 ? - 0.5 : this.velocityY == 0 ? 0 : 0.5;
  52. }
  53.  
  54. private brainWork() {
  55. let input = [
  56. this.sprite.x / Renderer.app.renderer.width,
  57. this.sprite.y / Renderer.app.renderer.height
  58. ];
  59. this.output = this.brain.network.activate(input);
  60. let [moveUp, moveRight, moveDown, moveLeft] = this.output;
  61.  
  62. if (moveUp > 0.75) this.move(Directions.Up);
  63. if (moveLeft > 0.5) this.move(Directions.Left);
  64. if (moveRight > 0.5) this.move(Directions.Right);
  65. if (moveDown > 0.5) this.move(Directions.Down);
  66.  
  67. this.fitness = Math.sqrt(Math.pow(this.sprite.x - Generation.target.sprite.x, 2) - Math.pow(this.sprite.y - Generation.target.sprite.y, 2));
  68. console.log(this.fitness);
  69. }
  70.  
  71. private setKeys() {
  72. let keys = new Array();
  73.  
  74. document.onkeydown = e => {
  75. keys[e.keyCode] = true;
  76. };
  77. document.onkeyup = e => {
  78. keys[e.keyCode] = false;
  79. };
  80.  
  81. Renderer.app.ticker.add(() => {
  82. for (let code in keys) {
  83. if (keys[code]) {
  84. let c = parseInt(code);
  85. if (c == 87) this.move(Directions.Up)
  86. if (c == 68) this.move(Directions.Right)
  87. if (c == 83) this.move(Directions.Down)
  88. if (c == 65) this.move(Directions.Left)
  89. }
  90. };
  91. });
  92.  
  93. }
  94.  
  95. public move(direction: Directions) {
  96. if (direction === 1) {
  97. this.velocityY -= this.velocityY > -5 ? 1 : 0;
  98. }
  99. else if (direction === 2) {
  100. this.velocityX += this.velocityX < 5 ? 1 : 0;
  101. }
  102. else if (direction === 3) {
  103. this.velocityY += this.velocityY < 5 ? 1 : 0;
  104. }
  105. else if (direction === 4) {
  106. this.velocityX -= this.velocityX > -5 ? 1 : 0;
  107. }
  108. }
  109.  
  110. public kill() {
  111. this.alive = true;
  112. Renderer.app.stage.removeChild(this.sprite);
  113. }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement