sf-b2d / app.js
flopro's picture
Add 3 files
9e3a33b
// Use AlpineJS to create the game component
<div class="game" x-data="{ nomination: '' }" x-init="selectEnemy()">
<div class="prose">
<p>You are a ninja and have to fight enemies to get to a data facility to clone a creature so every species is not extinct anymore. Good luck!</p>
</div>
<div class="game-canvas" x-bind:style="getCanvasStyle()" width="800" height="600">
<canvas id="game-canvas"></canvas>
</div>
<div class="options">
<button class="btn-primary" x-on:click="addEnemy()">Add Enemy</button>
<button class="btn-primary" x-on:click="updateEnemy()">Update Enemy</button>
<button class="btn-primary" x-on:click="removeEnemy()">Remove Enemy</button>
<button class="btn-primary" x-on:click="selectEnemy()">Select Enemy</button>
<button class="btn-primary" x-on:click="cloneCreature()">Clone Creature</button>
</div>
</div>
// Initialize the game canvas with Three.js
const canvas = document.getElementById("game-canvas");
const renderer = new THREE.WebGLRenderer({ canvas });
// Set up the camera
const camera = new THREE.PerspectiveCamera(75, canvas.width / canvas.height, 0.1, 1000);
camera.position.z = 1000;
// Set up the controls
const controls = new THREE.OrbitControls(camera, canvas);
controls.target.set(0, 0, 0);
controls.update();
// Set up the scene
const scene = new THREE.Scene();
// Set up the render function
const render = function () {
requestAnimationFrame(render);
renderer.render(scene, camera);
};
requestAnimationFrame(render);
// Set up the enemies
const enemies = [];
// Add an enemy
function addEnemy() {
enemies.push(new THREE.Mesh(new THREE.SphereGeometry(10, 10, 32), new THREE.MeshBasicMaterial()));
enemies[enemies.length - 1].position.set(0, 0, 0);
enemies[enemies.length - 1].Friendly = false;
scene.add(enemies[enemies.length - 1]);
}
// Update an enemy's position
function updateEnemy() {
const enemySelection = enemies[selectEnemy()];
enemySelection.position.set(Math.random() * 1000 - 500, Math.random() * 1000 - 500, Math.random() * 1000 - 500);
}
// Remove an enemy
function removeEnemy() {
const enemySelection = enemies[selectEnemy()];
scene.remove(enemySelection);
enemies.splice(enemies.indexOf(enemySelection), 1);
}
// Select an enemy
function selectEnemy() {
x-on:change="enemies[selectEnemy()] !== '' ? enemySelection = enemies[selectEnemy()] : enemySelection = null"
return enemySelection;
}
// Clone a creature
function cloneCreature() {
if (selectEnemy() !== null) {
scene.add(enemies[selectEnemy()]);
}
}