JSFiddle - React, Tailwind, and code Playground
by Bai Shiuan Huang
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/84/three.min.js"></script>
<script src="https://threejs.org/examples/js/controls/OrbitControls.js"></script>
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/stats.js/r16/Stats.min.js"></script>
<script src="https://jyunming-chen.github.io/tutsplus/js/KeyboardState.js"></script>
<div id = 'div1'>
TANK<br>
W (go foward) S (fall back)<br>
Q E (TRUN TURRET)<br>
A D (TRUN TANK)<br>
1 2 (TRUN BARREL)<br>
Space (SHOOT)
</div>
CSS
body {
overflow: hidden;
}
#div1{
position: absolute;
top: 10vh;
left: 10vw;
color: white;
}
JavaScript
var renderer, camera, controls, scene, BARREL, TURRET, TANK;
var cannon, light;
var thetaTURRET = 0;
var thetaTANK = 0;
var thetaBARREL = -Math.PI/2;
var keyboard = new KeyboardState();
var vector, gforce, target;
var associate, stopped = true;
var tmpball = new THREE.Mesh(new THREE.SphereGeometry(1.5, 20, 20), new THREE.MeshLambertMaterial({
color: 0xffff00
}));
class Cannonball{
constructor(){
this.mesh = new THREE.Mesh(new THREE.SphereGeometry(1.5, 20, 20), new THREE.MeshLambertMaterial({
color: 0xffff00
}));
this.mesh.visible = false;
associate.add(this.mesh);
this.vx = 0;
this.vy = 0;
}
start(vx, vy, posX, posY){
this.vx = vx;
this.vy = vy;
this.mesh.position.set(posX, posY, 0);
this.mesh.visible = true;
stopped = false;
}
update(dt){
this.vy += (-10 * dt);
this.mesh.position.x += this.vx;
this.mesh.position.y += this.vy;
if (this.mesh.position.y < -15) {
this.mesh.position.y = -15;
//this.mesh.visible = false;
stopped = true;
}
}
}
init();
animate();
function init() {
renderer = new THREE.WebGLRenderer({
antialias: true
});
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0x888888);
document.body.appendChild(renderer.domElement);
camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 1000);
camera.position.y = 200;
camera.position.z = 150;
controls = new THREE.OrbitControls(camera, renderer.domElement);
scene = new THREE.Scene();
stats = new Stats();
stats.domElement.style.position = 'absolute';
stats.domElement.style.bottom = '5px';
stats.domElement.style.left = '10px';
stats.domElement.style.zIndex = 100;
document.body.appendChild(stats.domElement);
var gridXZ = new THREE.GridHelper(200, 20, 'red', 'lightgreen');
var axes = new THREE.AxisHelper(10);
scene.add(axes);
scene.add(gridXZ);
...