Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Stack Game for the Projects #867

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions Projects/Stack Game/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!DOCTYPE html>
<html lang="en" >
<head>
<meta charset="UTF-8">
<title>DragonUncaged_Stack_Game</title>
<link rel="stylesheet" href="./style.css">

</head>
<body>
<meta name="viewport" content="width=device-width,user-scalable=no">

<div id="container">
<div id="game"></div>
<div id="score">0</div>
<div id="instructions">Click (or press the spacebar) to place the block</div>
<div class="game-over">
<h2>Game Over</h2>
<p>You did great, you're the best.</p>
<p>Click or spacebar to start again</p>
</div>
<div class="game-ready">
<div id="start-button">Start</div>
<div></div>
</div>
</div>

<script src='https://cdnjs.cloudflare.com/ajax/libs/three.js/r83/three.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/gsap/latest/TweenMax.min.js'></script><script src="./script.js"></script>

</body>
</html>
6 changes: 6 additions & 0 deletions Projects/Stack Game/output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"html": "<!DOCTYPE html>\n<html lang=\"en\" >\n<head>\n <meta charset=\"UTF-8\">\n <title>DragonUncaged_Stack_Game</title>\n <link rel=\"stylesheet\" href=\"./style.css\">\n\n</head>\n<body>\n<meta name=\"viewport\" content=\"width=device-width,user-scalable=no\">\n\n<div id=\"container\">\n\t<div id=\"game\"></div>\n\t<div id=\"score\">0</div>\n\t<div id=\"instructions\">Click (or press the spacebar) to place the block</div>\n\t<div class=\"game-over\">\n\t\t<h2>Game Over</h2>\n\t\t<p>You did great, you're the best.</p>\n\t\t<p>Click or spacebar to start again</p>\n\t</div>\n\t<div class=\"game-ready\">\n\t\t<div id=\"start-button\">Start</div>\n\t\t<div></div>\n\t</div>\n</div>\n\n<script src='https://cdnjs.cloudflare.com/ajax/libs/three.js/r83/three.min.js'></script>\n<script src='https://cdnjs.cloudflare.com/ajax/libs/gsap/latest/TweenMax.min.js'></script><script src=\"./script.js\"></script>\n\n</body>\n</html>",
"css": "@import url(\"https://fonts.googleapis.com/css?family=Comfortaa\");\nhtml, body {\n margin: 0;\n overflow: hidden;\n height: 100%;\n width: 100%;\n position: relative;\n font-family: \"Comfortaa\", cursive;\n}\n\n#container {\n width: 100%;\n height: 100%;\n}\n#container #score {\n position: absolute;\n top: 20px;\n width: 100%;\n text-align: center;\n font-size: 10vh;\n transition: transform 0.5s ease;\n color: #333344;\n transform: translatey(-200px) scale(1);\n}\n#container #game {\n position: absolute;\n top: 0;\n right: 0;\n bottom: 0;\n left: 0;\n}\n#container .game-over {\n position: absolute;\n top: 0;\n left: 0;\n width: 100%;\n height: 85%;\n display: flex;\n flex-direction: column;\n align-items: center;\n justify-content: center;\n}\n#container .game-over * {\n transition: opacity 0.5s ease, transform 0.5s ease;\n opacity: 0;\n transform: translatey(-50px);\n color: #333344;\n}\n#container .game-over h2 {\n margin: 0;\n padding: 0;\n font-size: 40px;\n}\n#container .game-ready {\n position: absolute;\n top: 0;\n left: 0;\n width: 100%;\n height: 100%;\n display: flex;\n flex-direction: column;\n align-items: center;\n justify-content: space-around;\n}\n#container .game-ready #start-button {\n transition: opacity 0.5s ease, transform 0.5s ease;\n opacity: 0;\n transform: translatey(-50px);\n border: 3px solid #333344;\n padding: 10px 20px;\n background-color: transparent;\n color: #333344;\n font-size: 30px;\n}\n#container #instructions {\n position: absolute;\n width: 100%;\n top: 16vh;\n left: 0;\n text-align: center;\n transition: opacity 0.5s ease, transform 0.5s ease;\n opacity: 0;\n}\n#container #instructions.hide {\n opacity: 0 !important;\n}\n#container.playing #score, #container.resetting #score {\n transform: translatey(0px) scale(1);\n}\n#container.playing #instructions {\n opacity: 1;\n}\n#container.ready .game-ready #start-button {\n opacity: 1;\n transform: translatey(0);\n}\n#container.ended #score {\n transform: translatey(6vh) scale(1.5);\n}\n#container.ended .game-over * {\n opacity: 1;\n transform: translatey(0);\n}\n#container.ended .game-over p {\n transition-delay: 0.3s;\n}",
"js": "var gameStarted = false;\nvar gameEnded = false;\nvar gameOverEl = document.querySelector('.game-over');\nvar startButtonEl = document.querySelector('#start-button');\nvar instructionsEl = document.querySelector('#instructions');\nvar scoreEl = document.querySelector('#score');\nvar containerEl = document.querySelector('#container');\nvar gameEl = document.querySelector('#game');\nvar score = 0;\n\nfunction init() {\n score = 0;\n scoreEl.innerHTML = score;\n gameStarted = false;\n gameEnded = false;\n gameOverEl.style.display = 'none';\n instructionsEl.style.opacity = 1;\n startButtonEl.style.opacity = 1;\n containerEl.classList.remove('ended');\n}\n\nfunction startGame() {\n gameStarted = true;\n instructionsEl.style.opacity = 0;\n startButtonEl.style.opacity = 0;\n scoreEl.innerHTML = score;\n containerEl.classList.add('playing');\n}\n\nfunction endGame() {\n gameEnded = true;\n containerEl.classList.remove('playing');\n containerEl.classList.add('ended');\n gameOverEl.style.display = 'flex';\n}\n\ndocument.addEventListener('click', function() {\n if (!gameStarted) {\n startGame();\n } else if (gameEnded) {\n init();\n startGame();\n } else {\n score++;\n scoreEl.innerHTML = score;\n }\n});\n\ndocument.addEventListener('keydown', function(e) {\n if (e.code === 'Space') {\n if (!gameStarted) {\n startGame();\n } else if (gameEnded) {\n init();n startGame();\n } else {\n score++;\n scoreEl.innerHTML = score;\n }\n }\n});\n\ninit();"
}

298 changes: 298 additions & 0 deletions Projects/Stack Game/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,298 @@
"use strict";
console.clear();
class Stage {
constructor() {
// container
this.render = function () {
this.renderer.render(this.scene, this.camera);
};
this.add = function (elem) {
this.scene.add(elem);
};
this.remove = function (elem) {
this.scene.remove(elem);
};
this.container = document.getElementById('game');
// renderer
this.renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false
});
this.renderer.setSize(window.innerWidth, window.innerHeight);
this.renderer.setClearColor('#D0CBC7', 1);
this.container.appendChild(this.renderer.domElement);
// scene
this.scene = new THREE.Scene();
// camera
let aspect = window.innerWidth / window.innerHeight;
let d = 20;
this.camera = new THREE.OrthographicCamera(-d * aspect, d * aspect, d, -d, -100, 1000);
this.camera.position.x = 2;
this.camera.position.y = 2;
this.camera.position.z = 2;
this.camera.lookAt(new THREE.Vector3(0, 0, 0));
//light
this.light = new THREE.DirectionalLight(0xffffff, 0.5);
this.light.position.set(0, 499, 0);
this.scene.add(this.light);
this.softLight = new THREE.AmbientLight(0xffffff, 0.4);
this.scene.add(this.softLight);
window.addEventListener('resize', () => this.onResize());
this.onResize();
}
setCamera(y, speed = 0.3) {
TweenLite.to(this.camera.position, speed, { y: y + 4, ease: Power1.easeInOut });
TweenLite.to(this.camera.lookAt, speed, { y: y, ease: Power1.easeInOut });
}
onResize() {
let viewSize = 30;
this.renderer.setSize(window.innerWidth, window.innerHeight);
this.camera.left = window.innerWidth / -viewSize;
this.camera.right = window.innerWidth / viewSize;
this.camera.top = window.innerHeight / viewSize;
this.camera.bottom = window.innerHeight / -viewSize;
this.camera.updateProjectionMatrix();
}
}
class Block {
constructor(block) {
// set size and position
this.STATES = { ACTIVE: 'active', STOPPED: 'stopped', MISSED: 'missed' };
this.MOVE_AMOUNT = 12;
this.dimension = { width: 0, height: 0, depth: 0 };
this.position = { x: 0, y: 0, z: 0 };
this.targetBlock = block;
this.index = (this.targetBlock ? this.targetBlock.index : 0) + 1;
this.workingPlane = this.index % 2 ? 'x' : 'z';
this.workingDimension = this.index % 2 ? 'width' : 'depth';
// set the dimensions from the target block, or defaults.
this.dimension.width = this.targetBlock ? this.targetBlock.dimension.width : 10;
this.dimension.height = this.targetBlock ? this.targetBlock.dimension.height : 2;
this.dimension.depth = this.targetBlock ? this.targetBlock.dimension.depth : 10;
this.position.x = this.targetBlock ? this.targetBlock.position.x : 0;
this.position.y = this.dimension.height * this.index;
this.position.z = this.targetBlock ? this.targetBlock.position.z : 0;
this.colorOffset = this.targetBlock ? this.targetBlock.colorOffset : Math.round(Math.random() * 100);
// set color
if (!this.targetBlock) {
this.color = 0x333344;
}
else {
let offset = this.index + this.colorOffset;
var r = Math.sin(0.3 * offset) * 55 + 200;
var g = Math.sin(0.3 * offset + 2) * 55 + 200;
var b = Math.sin(0.3 * offset + 4) * 55 + 200;
this.color = new THREE.Color(r / 255, g / 255, b / 255);
}
// state
this.state = this.index > 1 ? this.STATES.ACTIVE : this.STATES.STOPPED;
// set direction
this.speed = -0.1 - (this.index * 0.005);
if (this.speed < -4)
this.speed = -4;
this.direction = this.speed;
// create block
let geometry = new THREE.BoxGeometry(this.dimension.width, this.dimension.height, this.dimension.depth);
geometry.applyMatrix(new THREE.Matrix4().makeTranslation(this.dimension.width / 2, this.dimension.height / 2, this.dimension.depth / 2));
this.material = new THREE.MeshToonMaterial({ color: this.color, shading: THREE.FlatShading });
this.mesh = new THREE.Mesh(geometry, this.material);
this.mesh.position.set(this.position.x, this.position.y + (this.state == this.STATES.ACTIVE ? 0 : 0), this.position.z);
if (this.state == this.STATES.ACTIVE) {
this.position[this.workingPlane] = Math.random() > 0.5 ? -this.MOVE_AMOUNT : this.MOVE_AMOUNT;
}
}
reverseDirection() {
this.direction = this.direction > 0 ? this.speed : Math.abs(this.speed);
}
place() {
this.state = this.STATES.STOPPED;
let overlap = this.targetBlock.dimension[this.workingDimension] - Math.abs(this.position[this.workingPlane] - this.targetBlock.position[this.workingPlane]);
let blocksToReturn = {
plane: this.workingPlane,
direction: this.direction
};
if (this.dimension[this.workingDimension] - overlap < 0.3) {
overlap = this.dimension[this.workingDimension];
blocksToReturn.bonus = true;
this.position.x = this.targetBlock.position.x;
this.position.z = this.targetBlock.position.z;
this.dimension.width = this.targetBlock.dimension.width;
this.dimension.depth = this.targetBlock.dimension.depth;
}
if (overlap > 0) {
let choppedDimensions = { width: this.dimension.width, height: this.dimension.height, depth: this.dimension.depth };
choppedDimensions[this.workingDimension] -= overlap;
this.dimension[this.workingDimension] = overlap;
let placedGeometry = new THREE.BoxGeometry(this.dimension.width, this.dimension.height, this.dimension.depth);
placedGeometry.applyMatrix(new THREE.Matrix4().makeTranslation(this.dimension.width / 2, this.dimension.height / 2, this.dimension.depth / 2));
let placedMesh = new THREE.Mesh(placedGeometry, this.material);
let choppedGeometry = new THREE.BoxGeometry(choppedDimensions.width, choppedDimensions.height, choppedDimensions.depth);
choppedGeometry.applyMatrix(new THREE.Matrix4().makeTranslation(choppedDimensions.width / 2, choppedDimensions.height / 2, choppedDimensions.depth / 2));
let choppedMesh = new THREE.Mesh(choppedGeometry, this.material);
let choppedPosition = {
x: this.position.x,
y: this.position.y,
z: this.position.z
};
if (this.position[this.workingPlane] < this.targetBlock.position[this.workingPlane]) {
this.position[this.workingPlane] = this.targetBlock.position[this.workingPlane];
}
else {
choppedPosition[this.workingPlane] += overlap;
}
placedMesh.position.set(this.position.x, this.position.y, this.position.z);
choppedMesh.position.set(choppedPosition.x, choppedPosition.y, choppedPosition.z);
blocksToReturn.placed = placedMesh;
if (!blocksToReturn.bonus)
blocksToReturn.chopped = choppedMesh;
}
else {
this.state = this.STATES.MISSED;
}
this.dimension[this.workingDimension] = overlap;
return blocksToReturn;
}
tick() {
if (this.state == this.STATES.ACTIVE) {
let value = this.position[this.workingPlane];
if (value > this.MOVE_AMOUNT || value < -this.MOVE_AMOUNT)
this.reverseDirection();
this.position[this.workingPlane] += this.direction;
this.mesh.position[this.workingPlane] = this.position[this.workingPlane];
}
}
}
class Game {
constructor() {
this.STATES = {
'LOADING': 'loading',
'PLAYING': 'playing',
'READY': 'ready',
'ENDED': 'ended',
'RESETTING': 'resetting'
};
this.blocks = [];
this.state = this.STATES.LOADING;
this.stage = new Stage();
this.mainContainer = document.getElementById('container');
this.scoreContainer = document.getElementById('score');
this.startButton = document.getElementById('start-button');
this.instructions = document.getElementById('instructions');
this.scoreContainer.innerHTML = '0';
this.newBlocks = new THREE.Group();
this.placedBlocks = new THREE.Group();
this.choppedBlocks = new THREE.Group();
this.stage.add(this.newBlocks);
this.stage.add(this.placedBlocks);
this.stage.add(this.choppedBlocks);
this.addBlock();
this.tick();
this.updateState(this.STATES.READY);
document.addEventListener('keydown', e => {
if (e.keyCode == 32)
this.onAction();
});
document.addEventListener('click', e => {
this.onAction();
});
document.addEventListener('touchstart', e => {
e.preventDefault();
// this.onAction();
// this triggers after click on android so you
// insta-lose, will figure it out later.
});
}
updateState(newState) {
for (let key in this.STATES)
this.mainContainer.classList.remove(this.STATES[key]);
this.mainContainer.classList.add(newState);
this.state = newState;
}
onAction() {
switch (this.state) {
case this.STATES.READY:
this.startGame();
break;
case this.STATES.PLAYING:
this.placeBlock();
break;
case this.STATES.ENDED:
this.restartGame();
break;
}
}
startGame() {
if (this.state != this.STATES.PLAYING) {
this.scoreContainer.innerHTML = '0';
this.updateState(this.STATES.PLAYING);
this.addBlock();
}
}
restartGame() {
this.updateState(this.STATES.RESETTING);
let oldBlocks = this.placedBlocks.children;
let removeSpeed = 0.2;
let delayAmount = 0.02;
for (let i = 0; i < oldBlocks.length; i++) {
TweenLite.to(oldBlocks[i].scale, removeSpeed, { x: 0, y: 0, z: 0, delay: (oldBlocks.length - i) * delayAmount, ease: Power1.easeIn, onComplete: () => this.placedBlocks.remove(oldBlocks[i]) });
TweenLite.to(oldBlocks[i].rotation, removeSpeed, { y: 0.5, delay: (oldBlocks.length - i) * delayAmount, ease: Power1.easeIn });
}
let cameraMoveSpeed = removeSpeed * 2 + (oldBlocks.length * delayAmount);
this.stage.setCamera(2, cameraMoveSpeed);
let countdown = { value: this.blocks.length - 1 };
TweenLite.to(countdown, cameraMoveSpeed, { value: 0, onUpdate: () => { this.scoreContainer.innerHTML = String(Math.round(countdown.value)); } });
this.blocks = this.blocks.slice(0, 1);
setTimeout(() => {
this.startGame();
}, cameraMoveSpeed * 1000);
}
placeBlock() {
let currentBlock = this.blocks[this.blocks.length - 1];
let newBlocks = currentBlock.place();
this.newBlocks.remove(currentBlock.mesh);
if (newBlocks.placed)
this.placedBlocks.add(newBlocks.placed);
if (newBlocks.chopped) {
this.choppedBlocks.add(newBlocks.chopped);
let positionParams = { y: '-=30', ease: Power1.easeIn, onComplete: () => this.choppedBlocks.remove(newBlocks.chopped) };
let rotateRandomness = 10;
let rotationParams = {
delay: 0.05,
x: newBlocks.plane == 'z' ? ((Math.random() * rotateRandomness) - (rotateRandomness / 2)) : 0.1,
z: newBlocks.plane == 'x' ? ((Math.random() * rotateRandomness) - (rotateRandomness / 2)) : 0.1,
y: Math.random() * 0.1,
};
if (newBlocks.chopped.position[newBlocks.plane] > newBlocks.placed.position[newBlocks.plane]) {
positionParams[newBlocks.plane] = '+=' + (40 * Math.abs(newBlocks.direction));
}
else {
positionParams[newBlocks.plane] = '-=' + (40 * Math.abs(newBlocks.direction));
}
TweenLite.to(newBlocks.chopped.position, 1, positionParams);
TweenLite.to(newBlocks.chopped.rotation, 1, rotationParams);
}
this.addBlock();
}
addBlock() {
let lastBlock = this.blocks[this.blocks.length - 1];
if (lastBlock && lastBlock.state == lastBlock.STATES.MISSED) {
return this.endGame();
}
this.scoreContainer.innerHTML = String(this.blocks.length - 1);
let newKidOnTheBlock = new Block(lastBlock);
this.newBlocks.add(newKidOnTheBlock.mesh);
this.blocks.push(newKidOnTheBlock);
this.stage.setCamera(this.blocks.length * 2);
if (this.blocks.length >= 5)
this.instructions.classList.add('hide');
}
endGame() {
this.updateState(this.STATES.ENDED);
}
tick() {
this.blocks[this.blocks.length - 1].tick();
this.stage.render();
requestAnimationFrame(() => { this.tick(); });
}
}
let game = new Game();
Loading