Avoid Poop Game
Last updated
Last updated
1) File
2) main.js
// load sprite
let poop = App.loadSpritesheet('poop.png', 48, 43, [0], 16);
// load sprite
let tomb = App.loadSpritesheet('tomb.png', 32, 48, {
left: [0], // defined base anim
right: [0], // defined base anim
up: [0], // defined base anim
down: [0], // defined base anim
});
const STATE_INIT = 3000;
const STATE_READY = 3001;
const STATE_PLAYING = 3002;
const STATE_JUDGE = 3004;
const STATE_END = 3005;
let _level = 1;
let _levelTimer = 15;
let _levelAddTimer = 0;
let _start = false;
let _timer = 90;
let _poops = [];
let _stateTimer = 0;
let _genTime = 0;
let _dropTime = 0;
let _live = 0;
let _players = App.players; // App.players : get total players
function startApp()
{
_start = true;
_stateTimer = 0;
_genTime = 0;
_dropTime = 0;
_timer = 90;
for(let i in _players) {
let p = _players[i];
// create and utilize option data using tags.
p.tag = {
alive : true,
};
}
}
function startState(state)
{
_state = state;
_stateTImer = 0;
switch(_state)
{
case STATE_INIT:
startApp();
break;
case STATE_READY:
break;
case STATE_PLAYING:
// Show Label
App.showCenterLabel("Game Start");
break;
case STATE_JUDGE:
for(let i in _poops) {
let b = _poops[i];
Map.putObject(b[0], b[1], null);
}
break;
case STATE_END:
_start = false;
for(let i in _players) {
let p = _players[i];
p.sprite = null;
p.moveSpeed = 80;
p.sendUpdated();
}
break;
}
}
function checkSuvivors() {
if(!_start)
return;
let alive = 0;
for(let i in _players) {
let p = _players[i];
if(!p.sprite) {
lastSurvivor = p;
++alive;
}
}
return alive;
}
App.onStart.Add(function() {
startState(STATE_INIT);
});
// when player join the space event
App.onJoinPlayer.Add(function(p) {
// create and utilize option data using tags.
if(_start)
{
p.tag = {
alive : false,
};
// change move speed
p.moveSpeed = 20;
// change sprite image
p.sprite = tomb;
// when player property changed, have to call this method
p.sendUpdated();
}
_players = App.players;
});
// when player leave the space event
App.onLeavePlayer.Add(function(p) {
p.title = null;
p.sprite = null;
p.moveSpeed = 80;
p.sendUpdated();
_players = App.players; // App.players : get total players
});
// when player touched objects event
App.onObjectTouched.Add(function(sender, x, y, tileID) {
if(!_start)
return;
if(!sender.tag.alive)
return;
sender.tag.alive = false;
sender.sprite = tomb;
sender.moveSpeed = 40;
sender.sendUpdated();
_live = checkSuvivors();
if(_live == 1 || _live == 0)
{
startState(STATE_JUDGE);
}
else
{
if(_stateTimer >= 1)
{
_stateTimer = 0;
_timer--;
if(_timer <= 0)
{
startState(STATE_JUDGE);
}
}
}
});
// when the game block is pressed event
App.onDestroy.Add(function() {
for(let i in _poops) {
let b = _poops[i];
Map.putObject(b[0], b[1], null);
}
});
// called every 20ms
// param1 : deltatime ( elapsedTime )
App.onUpdate.Add(function(dt) {
if(!_start)
return;
_stateTimer += dt;
switch(_state)
{
case STATE_INIT:
App.showCenterLabel(`Avoid falling poop.`);
if(_stateTimer >= 5)
{
startState(STATE_READY);
}
break;
case STATE_READY:
App.showCenterLabel(`The game will start soon.`);
if(_stateTimer >= 3)
{
startState(STATE_PLAYING);
}
break;
case STATE_PLAYING:
_genTime -= dt;
if(_genTime <= 0) {
_genTime = Math.random() * (0.5 - (_level * 0.05));
let b = [Math.floor(Map.width * Math.random()),-1];
_poops.push(b);
if(b[1] >= 0)
Map.putObject(b[0], b[1], poop, {
overlap: true,
});
}
_dropTime -= dt;
if(_dropTime <= 0) {
_dropTime = Math.random() * (0.5 - (_level * 0.08));
for(let i in _poops) {
let b = _poops[i];
Map.putObject(b[0], b[1], null);
b[1]++;
if(b[1] < Map.height) {
Map.putObject(b[0], b[1], poop, {
overlap: true,
});
}
}
for(let k = _poops.length - 1;k >= 0;--k) {
let b = _poops[k];
if(b[1] >= Map.height)
_poops.splice(k, 1);
}
}
_levelAddTimer += dt;
if(_levelAddTimer >= _levelTimer)
{
_level++;
_levelAddTimer = 0;
if(_level > 6)
{
_level = 6;
}
}
break;
case STATE_JUDGE:
if(_live == 1)
{
App.showCenterLabel(`${lastSurvivor.name} is last suvivor`);
}
else if(_live == 0)
{
App.showCenterLabel(`There are no survivors.`);
}
else
{
App.showCenterLabel(`Final survivors : ` + _live);
}
if(_stateTimer >= 5)
{
startState(STATE_END);
}
break;
case STATE_END:
break;
}
});