JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://rawgit.com/ReddogStone/behavior.js/master/behavior.js"></script>
<script src="https://rawgit.com/ReddogStone/behavior.js/master/behavior-system.js"></script>
<body>
<canvas id="screen" width="600" height="400"></canvas>
</body>
JavaScript
'use strict';
function getMousePos(canvas, event) {
let rect = canvas.getBoundingClientRect();
return { x: event.clientX - rect.left, y: event.clientY - rect.top };
}
const GROUND_COLOR = 'BurlyWood';
const BULLET_COLOR = 'gray';
const BULLET_BORDER_COLOR = 'black';
const BULLET_SIZE = 5;
const BULLET_BASE_SPEED = 1000;
const EXPLOSION_COLOR = 'red';
const EXPLOSION_BORDER_COLOR = 'darkred';
const EXPLOSION_SIZE = 50;
const TURRET_COLOR = '#7070FF';
const TURRET_BORDER_COLOR = '#000030';
const TURRET_AIM_COLOR = '#FF7070';
const TURRET_AIM_BORDER_COLOR = '#300000';
const TURRET_SIZE = 20;
const TURRET_POWER_INC_SPEED = 1;
const GRAVITY = 1000;
function renderTurretBody(context, pos) {
context.beginPath();
context.arc(turret.pos.x, turret.pos.y, TURRET_SIZE, Math.PI, 2 * Math.PI);
context.closePath();
context.fillStyle = TURRET_COLOR;
context.strokeStyle = TURRET_BORDER_COLOR;
context.fill();
context.stroke();
}
function renderTurretCannon(context, pos, aim) {
context.save();
context.translate(pos.x, pos.y);
context.rotate(aim.angle);
context.translate(0, -TURRET_SIZE * 0.15);
context.translate(TURRET_SIZE * 1.5, 0);
context.beginPath();
let length = 2 * TURRET_SIZE * aim.power;
let size = 0.5 * TURRET_SIZE * aim.power;
context.moveTo(0, 0);
context.lineTo(length, -size);
context.lineTo(length, size + TURRET_SIZE * 0.3);
context.lineTo(0, TURRET_SIZE * 0.3);
context.closePath();
context.fillStyle = TURRET_AIM_COLOR;
context.strokeStyle = TURRET_AIM_BORDER_COLOR;
context.fill();
context.translate(-TURRET_SIZE * 1.5, 0);
context.beginPath();
context.rect(0, 0, TURRET_SIZE * 1.5, TURRET_SIZE * 0.3);
context.fillStyle = TURRET_COLOR;
context.strokeStyle = TURRET_BORDER_COLOR;
context.fill();
context.stroke();
context.restore();
}
function renderTurret(context, turret) {
context.lineWidth = 2;
renderTurretCannon(context, turret.pos, turret.aim);
renderTurretBody(context, turret.pos);
}
function renderGround(context,...