Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Renderer from "../../Renderer/Renderer";
- import Generation from "../../GA/Generation";
- import Brain from "../../GA/Brain";
- import * as PIXI from "pixi.js";
- import { Directions } from "./Directions";
- export default class Character{
- sprite: PIXI.Sprite;
- velocityX: number = 0;
- velocityY: number = 0;
- brain: Brain;
- alive: boolean = true;
- input: number[];
- output: number[];
- fitness: number;
- constructor(brain: Brain) {
- this.brain = brain;
- this.sprite = new PIXI.Sprite.fromImage("images/character.png");
- this.setDefaults();
- Renderer.app.stage.addChild(this.sprite);
- this.setKeys();
- Renderer.app.ticker.add(() => {
- this.update();
- });
- }
- private setDefaults() {
- this.sprite.width = 50;
- this.sprite.height = 50;
- this.sprite.y = Renderer.app.renderer.height - this.sprite.height - 20;
- this.sprite.x = Math.floor(Math.random() * Renderer.app.renderer.width / 2) + Renderer.app.renderer.width / 4
- }
- public update() {
- if (!this.alive) return;
- this.brainWork();
- for (let obstacle of Generation.obstacles) {
- if (this.sprite.x < obstacle.sprite.x + obstacle.sprite.width &&
- this.sprite.x + this.sprite.width > obstacle.sprite.x &&
- this.sprite.y < obstacle.sprite.y + obstacle.sprite.height &&
- this.sprite.y + this.sprite.height > obstacle.sprite.y) {
- this.kill();
- }
- }
- this.sprite.x += this.velocityX;
- this.sprite.y += this.velocityY;
- this.velocityX += this.velocityX > 0 ? - 0.5 : this.velocityX == 0 ? 0 : 0.5;
- this.velocityY += this.velocityY > 0 ? - 0.5 : this.velocityY == 0 ? 0 : 0.5;
- }
- private brainWork() {
- let input = [
- this.sprite.x / Renderer.app.renderer.width,
- this.sprite.y / Renderer.app.renderer.height
- ];
- this.output = this.brain.network.activate(input);
- let [moveUp, moveRight, moveDown, moveLeft] = this.output;
- if (moveUp > 0.75) this.move(Directions.Up);
- if (moveLeft > 0.5) this.move(Directions.Left);
- if (moveRight > 0.5) this.move(Directions.Right);
- if (moveDown > 0.5) this.move(Directions.Down);
- this.fitness = Math.sqrt(Math.pow(this.sprite.x - Generation.target.sprite.x, 2) - Math.pow(this.sprite.y - Generation.target.sprite.y, 2));
- console.log(this.fitness);
- }
- private setKeys() {
- let keys = new Array();
- document.onkeydown = e => {
- keys[e.keyCode] = true;
- };
- document.onkeyup = e => {
- keys[e.keyCode] = false;
- };
- Renderer.app.ticker.add(() => {
- for (let code in keys) {
- if (keys[code]) {
- let c = parseInt(code);
- if (c == 87) this.move(Directions.Up)
- if (c == 68) this.move(Directions.Right)
- if (c == 83) this.move(Directions.Down)
- if (c == 65) this.move(Directions.Left)
- }
- };
- });
- }
- public move(direction: Directions) {
- if (direction === 1) {
- this.velocityY -= this.velocityY > -5 ? 1 : 0;
- }
- else if (direction === 2) {
- this.velocityX += this.velocityX < 5 ? 1 : 0;
- }
- else if (direction === 3) {
- this.velocityY += this.velocityY < 5 ? 1 : 0;
- }
- else if (direction === 4) {
- this.velocityX -= this.velocityX > -5 ? 1 : 0;
- }
- }
- public kill() {
- this.alive = true;
- Renderer.app.stage.removeChild(this.sprite);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement