/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1");
/****
* Classes
****/
// Create a new class for the bulk asset
var Bulk = Container.expand(function () {
var self = Container.call(this);
// Attach the bulk asset to the Bulk class
var bulkGraphics = self.attachAsset('Bulk', {
anchorX: 0.5,
anchorY: 0.5
});
// Set the bulk's speed
self.speed = 2;
// Set the bulk's health
self.health = 2;
// Update the bulk's position every game tick
self.update = function () {
// Calculate the distance between the bulk and the slime
var dx = slime.x - self.x;
var dy = slime.y - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
// Normalize the direction vector (dx and dy)
dx /= distance;
dy /= distance;
// Move the bulk towards the slime at a constant speed
self.x += dx * self.speed;
self.y += dy * self.speed;
};
});
// Create a new class for the cube asset
var Cube = Container.expand(function () {
var self = Container.call(this);
// Attach the cube asset to the Cube class
var cubeGraphics = self.attachAsset('Cube', {
anchorX: 0.5,
anchorY: 0.5
});
// Set the cube's speed
self.speed = 3;
// Increase cube's speed by 25% when wave equals 5
self.update = function () {
if (waveText.wave === 5) {
self.speed += self.speed * 0.25;
waveText.wave++; // Increment wave to prevent speed increase in every tick
}
};
// Set the cube's health
self.health = 1;
// Set the cube's health
self.health = 1;
// Update the cube's position every game tick
self.update = function () {
// Calculate the distance between the cube and the slime
var dx = slime.x - self.x;
var dy = slime.y - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
// Normalize the direction vector (dx and dy)
dx /= distance;
dy /= distance;
// Move the cube towards the slime at a constant speed
self.x += dx * self.speed;
self.y += dy * self.speed;
};
});
// Create a new class for the shoot asset
var Shoot = Container.expand(function () {
var self = Container.call(this);
// Attach the shoot asset to the Shoot class
var shootGraphics = self.attachAsset('Shoot', {
anchorX: 0.5,
anchorY: 0.5
});
// Set the shoot's speed
self.speed = 5;
// Set the shoot's damage
self.damage = 1;
// Set the shoot's damage
self.damage = 1;
// Update the shoot's position every game tick
self.update = function () {
// Move the shoot to the right at a constant speed
self.x += self.speed;
};
});
// Create a new class for the speedcube asset
var Speedcube = Container.expand(function () {
var self = Container.call(this);
// Attach the speedcube asset to the Speedcube class
var speedcubeGraphics = self.attachAsset('Speedcube', {
anchorX: 0.5,
anchorY: 0.5
});
// Set the speedcube's health
self.health = 1;
// Set the speedcube's speed
self.speed = 5;
// Update the speedcube's position every game tick
self.update = function () {
// Calculate the distance between the speedcube and the slime
var dx = slime.x - self.x;
var dy = slime.y - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
// Normalize the direction vector (dx and dy)
dx /= distance;
dy /= distance;
// Move the speedcube towards the slime at a constant speed
self.x += dx * self.speed;
self.y += dy * self.speed;
};
});
// Create a new class for the rate text centered on the rate asset
var CenteredRateText = Text2.expand(function () {
var self = Text2.call(this, '0.9s 10$', {
size: 100,
fill: 0xFFFFFF
});
self.anchor.set(0.5, 0.5); // Center the text
self.update = function () {
if (firingRate === 0.9) {
self.setText('0.85s 10$');
} else if (firingRate === 0.85) {
self.setText('0.80s 30$');
} else {
self.setText('0.9s 10$');
}
};
});
// Create a new class for the centered speed text
var CenteredSpeedText = Text2.expand(function () {
var self = Text2.call(this, '+20% 10', {
size: 100,
// Same size as the rate text
fill: 0x000000 // Black color
});
self.anchor.set(0.5, 0.5); // Center the text
});
// Create a new class for the gold text
var GoldText = Text2.expand(function () {
var self = Text2.call(this, 'Gold: 0', {
size: 100,
fill: 0xFFFFFF
});
self.anchor.set(0, 0);
self.x = 50;
self.y = 150;
self.gold = storage.gold || 10; // Set initial gold to 10
self.update = function () {
self.setText('Gold: ' + self.gold);
};
});
// Create a new class for the rate text
var RateText = Text2.expand(function () {
var self = Text2.call(this, 'Rate: 0s', {
size: 100,
fill: 0xFFFFFF
});
self.anchor.set(0, 0);
self.x = 50;
self.y = 250;
self.update = function () {
self.setText('Rate: ' + firingRate + 's');
};
});
// Create a new class for the reset button
var ResetButton = Text2.expand(function () {
var self = Text2.call(this, 'Reset Progress', {
size: 100,
fill: 0xFF0000 // Red color
});
self.anchor.set(1, 0); // Anchor to top right
self.x = 2048 - 50; // Position to top right with 50px padding
self.y = 50;
self.interactive = true; // Make the button clickable
self.down = function () {
// Reset progress
storage.wave = 1;
storage.gold = 10;
storage.firingRate = 1.0; // Reset firing rate to default
waveText.wave = 1;
goldText.gold = 10;
firingRate = 1.0; // Reset firing rate variable
rateText.setText('Rate ' + firingRate + 's'); // Update rate text
// Refresh the game
LK.showGameOver(); // This will reset the game state
};
});
// Create a new class for the wave text
var WaveText = Text2.expand(function () {
var self = Text2.call(this, 'Wave: 0', {
size: 100,
fill: 0xFFFFFF
});
self.anchor.set(0, 0);
self.x = 50;
self.y = 50;
self.wave = storage.wave || 1; // Set initial wave to 1
self.update = function () {
self.setText('Wave: ' + self.wave);
};
});
/****
* Initialize Game
****/
// Spawn the cube asset from the cubestart asset every 60 game ticks
//<Assets used in the game will automatically appear here>
var game = new LK.Game({
backgroundColor: 0x008000 //Init game with green background
});
/****
* Game Code
****/
//<Assets used in the game will automatically appear here>
// Spawn the cube asset from the cubestart asset every 60 game ticks
// Add slime asset to the game screen, make it 2x bigger and move it to the left
var slime = game.addChild(LK.getAsset('Slime', {
anchorX: 0.5,
anchorY: 0.5,
x: 200,
y: 1350,
// Adjusted y position to move the slime down a little
scaleX: 2,
scaleY: 2
}));
// Add menu asset to the game screen
var menu = game.addChild(LK.getAsset('Menu', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024,
y: 2766,
scaleX: 25,
scaleY: 25
}));
// Add cubestart asset to the game screen
var cubestart = game.addChild(LK.getAsset('Cubesstart', {
anchorX: 0.5,
anchorY: 0.5,
x: 1800,
// Moved to the right by a lot
y: 1366,
scaleX: 2,
scaleY: 2
}));
// Add the speed asset to the game screen
var speedAsset = game.addChild(LK.getAsset('Speed', {
anchorX: 0.5,
anchorY: 0.5,
x: 350,
y: 2500,
scaleX: 2,
scaleY: 2
}));
// Add the centered speed text to the center of the speed asset
var centeredSpeedText = game.addChild(new CenteredSpeedText());
centeredSpeedText.x = speedAsset.x;
centeredSpeedText.y = speedAsset.y;
// Make the speed asset interactive
speedAsset.interactive = true;
speedAsset.down = function () {
if (goldText.gold >= 10) {
goldText.gold -= 10;
// Increase speed of all speed assets by 20%
for (var i = 0; i < game.children.length; i++) {
var obj = game.children[i];
if (obj instanceof Speedcube) {
obj.speed += obj.speed * 0.20;
}
}
storage.gold = goldText.gold;
}
};
// Add the rate asset to the game screen
var rateAsset = game.addChild(LK.getAsset('Rate', {
anchorX: 0.5,
anchorY: 0.5,
x: 350,
y: 1800,
scaleX: 2,
scaleY: 2
}));
// Add the centered rate text to the center of the rate asset
var centeredRateText = game.addChild(new CenteredRateText());
centeredRateText.x = rateAsset.x;
centeredRateText.y = rateAsset.y;
// Make the rate asset interactive
rateAsset.interactive = true;
rateAsset.down = function () {
if (goldText.gold >= 10) {
goldText.gold -= 10;
firingRate = 0.9;
rateText.setText('Rate: ' + firingRate + 's');
storage.gold = goldText.gold;
storage.firingRate = firingRate;
// Update the centered rate text for the next upgrade
centeredRateText.setText('0.85s 10$');
centeredRateText.update(); // Ensure the text is updated immediately
// Update the rateAsset interaction for the next upgrade
rateAsset.down = function () {
if (goldText.gold >= 10) {
goldText.gold -= 10;
firingRate = 0.85;
rateText.setText('Rate: ' + firingRate + 's');
storage.gold = goldText.gold;
storage.firingRate = firingRate;
// Update the centered rate text for the next upgrade
centeredRateText.setText('0.75s 30$');
centeredRateText.update(); // Ensure the text is updated immediately
// Update the rateAsset interaction for the next upgrade
rateAsset.down = function () {
if (goldText.gold >= 30) {
goldText.gold -= 30;
firingRate = 0.80;
rateText.setText('Rate: ' + firingRate + 's');
storage.gold = goldText.gold;
storage.firingRate = firingRate;
// Update the centered rate text for the next upgrade
centeredRateText.setText('Max Level');
centeredRateText.update(); // Ensure the text is updated immediately
}
};
}
};
}
};
// Add the wave text to the game screen
var waveText = game.addChild(new WaveText());
// Add the gold text to the game screen
var goldText = game.addChild(new GoldText());
// Initialize firing rate variable from storage if available
var firingRate = storage.firingRate || 1.0;
var rateText = game.addChild(new RateText());
// Add the reset button to the game screen
var resetButton = game.addChild(new ResetButton());
game.update = function () {
// Spawn the cube asset from the cubestart asset every 60 game ticks or 48 game ticks if wave is above 9
var spawnRate;
if (waveText.wave > 14) {
spawnRate = Math.floor(60 * 1.25); // 1.25 seconds
} else if (waveText.wave > 9) {
spawnRate = 48;
} else {
spawnRate = 60;
}
if (LK.ticks % spawnRate == 0) {
var newCube;
if (waveText.wave > 14) {
newCube = new Bulk();
} else if (waveText.wave > 14) {
newCube = new Bulk();
} else if (waveText.wave > 5 && waveText.wave <= 14) {
newCube = new Speedcube();
} else {
newCube = new Cube();
}
newCube.x = cubestart.x;
newCube.y = cubestart.y;
game.addChild(newCube);
}
// Update the wave text every 600 game ticks
if (LK.ticks % 600 == 0 && LK.ticks != 0) {
waveText.wave++;
goldText.gold += 10;
storage.wave = waveText.wave;
storage.gold = goldText.gold;
}
// Make the slime asset shoot out the shoot asset to the right based on firingRate
if (LK.ticks % Math.floor(60 * firingRate) == 0) {
var newShoot = new Shoot();
newShoot.x = slime.x;
newShoot.y = slime.y;
newShoot.update = function () {
// Move the shoot to the right at a constant speed
this.x += this.speed;
};
game.addChild(newShoot);
}
// Check for collisions between Cube and Shoot objects
for (var i = 0; i < game.children.length; i++) {
var obj1 = game.children[i];
if (obj1 instanceof Cube || obj1 instanceof Speedcube || obj1 instanceof Bulk) {
for (var j = 0; j < game.children.length; j++) {
var obj2 = game.children[j];
if (obj2 instanceof Shoot && obj1.intersects(obj2)) {
// Destroy both objects on collision
obj1.health -= obj2.damage;
if (obj1.health <= 0) {
obj1.destroy();
}
obj2.destroy();
break;
}
}
}
}
}; /****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1");
/****
* Classes
****/
// Create a new class for the bulk asset
var Bulk = Container.expand(function () {
var self = Container.call(this);
// Attach the bulk asset to the Bulk class
var bulkGraphics = self.attachAsset('Bulk', {
anchorX: 0.5,
anchorY: 0.5
});
// Set the bulk's speed
self.speed = 2;
// Set the bulk's health
self.health = 2;
// Update the bulk's position every game tick
self.update = function () {
// Calculate the distance between the bulk and the slime
var dx = slime.x - self.x;
var dy = slime.y - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
// Normalize the direction vector (dx and dy)
dx /= distance;
dy /= distance;
// Move the bulk towards the slime at a constant speed
self.x += dx * self.speed;
self.y += dy * self.speed;
};
});
// Create a new class for the cube asset
var Cube = Container.expand(function () {
var self = Container.call(this);
// Attach the cube asset to the Cube class
var cubeGraphics = self.attachAsset('Cube', {
anchorX: 0.5,
anchorY: 0.5
});
// Set the cube's speed
self.speed = 3;
// Increase cube's speed by 25% when wave equals 5
self.update = function () {
if (waveText.wave === 5) {
self.speed += self.speed * 0.25;
waveText.wave++; // Increment wave to prevent speed increase in every tick
}
};
// Set the cube's health
self.health = 1;
// Set the cube's health
self.health = 1;
// Update the cube's position every game tick
self.update = function () {
// Calculate the distance between the cube and the slime
var dx = slime.x - self.x;
var dy = slime.y - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
// Normalize the direction vector (dx and dy)
dx /= distance;
dy /= distance;
// Move the cube towards the slime at a constant speed
self.x += dx * self.speed;
self.y += dy * self.speed;
};
});
// Create a new class for the shoot asset
var Shoot = Container.expand(function () {
var self = Container.call(this);
// Attach the shoot asset to the Shoot class
var shootGraphics = self.attachAsset('Shoot', {
anchorX: 0.5,
anchorY: 0.5
});
// Set the shoot's speed
self.speed = 5;
// Set the shoot's damage
self.damage = 1;
// Set the shoot's damage
self.damage = 1;
// Update the shoot's position every game tick
self.update = function () {
// Move the shoot to the right at a constant speed
self.x += self.speed;
};
});
// Create a new class for the speedcube asset
var Speedcube = Container.expand(function () {
var self = Container.call(this);
// Attach the speedcube asset to the Speedcube class
var speedcubeGraphics = self.attachAsset('Speedcube', {
anchorX: 0.5,
anchorY: 0.5
});
// Set the speedcube's health
self.health = 1;
// Set the speedcube's speed
self.speed = 5;
// Update the speedcube's position every game tick
self.update = function () {
// Calculate the distance between the speedcube and the slime
var dx = slime.x - self.x;
var dy = slime.y - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
// Normalize the direction vector (dx and dy)
dx /= distance;
dy /= distance;
// Move the speedcube towards the slime at a constant speed
self.x += dx * self.speed;
self.y += dy * self.speed;
};
});
// Create a new class for the rate text centered on the rate asset
var CenteredRateText = Text2.expand(function () {
var self = Text2.call(this, '0.9s 10$', {
size: 100,
fill: 0xFFFFFF
});
self.anchor.set(0.5, 0.5); // Center the text
self.update = function () {
if (firingRate === 0.9) {
self.setText('0.85s 10$');
} else if (firingRate === 0.85) {
self.setText('0.80s 30$');
} else {
self.setText('0.9s 10$');
}
};
});
// Create a new class for the centered speed text
var CenteredSpeedText = Text2.expand(function () {
var self = Text2.call(this, '+20% 10', {
size: 100,
// Same size as the rate text
fill: 0x000000 // Black color
});
self.anchor.set(0.5, 0.5); // Center the text
});
// Create a new class for the gold text
var GoldText = Text2.expand(function () {
var self = Text2.call(this, 'Gold: 0', {
size: 100,
fill: 0xFFFFFF
});
self.anchor.set(0, 0);
self.x = 50;
self.y = 150;
self.gold = storage.gold || 10; // Set initial gold to 10
self.update = function () {
self.setText('Gold: ' + self.gold);
};
});
// Create a new class for the rate text
var RateText = Text2.expand(function () {
var self = Text2.call(this, 'Rate: 0s', {
size: 100,
fill: 0xFFFFFF
});
self.anchor.set(0, 0);
self.x = 50;
self.y = 250;
self.update = function () {
self.setText('Rate: ' + firingRate + 's');
};
});
// Create a new class for the reset button
var ResetButton = Text2.expand(function () {
var self = Text2.call(this, 'Reset Progress', {
size: 100,
fill: 0xFF0000 // Red color
});
self.anchor.set(1, 0); // Anchor to top right
self.x = 2048 - 50; // Position to top right with 50px padding
self.y = 50;
self.interactive = true; // Make the button clickable
self.down = function () {
// Reset progress
storage.wave = 1;
storage.gold = 10;
storage.firingRate = 1.0; // Reset firing rate to default
waveText.wave = 1;
goldText.gold = 10;
firingRate = 1.0; // Reset firing rate variable
rateText.setText('Rate ' + firingRate + 's'); // Update rate text
// Refresh the game
LK.showGameOver(); // This will reset the game state
};
});
// Create a new class for the wave text
var WaveText = Text2.expand(function () {
var self = Text2.call(this, 'Wave: 0', {
size: 100,
fill: 0xFFFFFF
});
self.anchor.set(0, 0);
self.x = 50;
self.y = 50;
self.wave = storage.wave || 1; // Set initial wave to 1
self.update = function () {
self.setText('Wave: ' + self.wave);
};
});
/****
* Initialize Game
****/
// Spawn the cube asset from the cubestart asset every 60 game ticks
//<Assets used in the game will automatically appear here>
var game = new LK.Game({
backgroundColor: 0x008000 //Init game with green background
});
/****
* Game Code
****/
//<Assets used in the game will automatically appear here>
// Spawn the cube asset from the cubestart asset every 60 game ticks
// Add slime asset to the game screen, make it 2x bigger and move it to the left
var slime = game.addChild(LK.getAsset('Slime', {
anchorX: 0.5,
anchorY: 0.5,
x: 200,
y: 1350,
// Adjusted y position to move the slime down a little
scaleX: 2,
scaleY: 2
}));
// Add menu asset to the game screen
var menu = game.addChild(LK.getAsset('Menu', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024,
y: 2766,
scaleX: 25,
scaleY: 25
}));
// Add cubestart asset to the game screen
var cubestart = game.addChild(LK.getAsset('Cubesstart', {
anchorX: 0.5,
anchorY: 0.5,
x: 1800,
// Moved to the right by a lot
y: 1366,
scaleX: 2,
scaleY: 2
}));
// Add the speed asset to the game screen
var speedAsset = game.addChild(LK.getAsset('Speed', {
anchorX: 0.5,
anchorY: 0.5,
x: 350,
y: 2500,
scaleX: 2,
scaleY: 2
}));
// Add the centered speed text to the center of the speed asset
var centeredSpeedText = game.addChild(new CenteredSpeedText());
centeredSpeedText.x = speedAsset.x;
centeredSpeedText.y = speedAsset.y;
// Make the speed asset interactive
speedAsset.interactive = true;
speedAsset.down = function () {
if (goldText.gold >= 10) {
goldText.gold -= 10;
// Increase speed of all speed assets by 20%
for (var i = 0; i < game.children.length; i++) {
var obj = game.children[i];
if (obj instanceof Speedcube) {
obj.speed += obj.speed * 0.20;
}
}
storage.gold = goldText.gold;
}
};
// Add the rate asset to the game screen
var rateAsset = game.addChild(LK.getAsset('Rate', {
anchorX: 0.5,
anchorY: 0.5,
x: 350,
y: 1800,
scaleX: 2,
scaleY: 2
}));
// Add the centered rate text to the center of the rate asset
var centeredRateText = game.addChild(new CenteredRateText());
centeredRateText.x = rateAsset.x;
centeredRateText.y = rateAsset.y;
// Make the rate asset interactive
rateAsset.interactive = true;
rateAsset.down = function () {
if (goldText.gold >= 10) {
goldText.gold -= 10;
firingRate = 0.9;
rateText.setText('Rate: ' + firingRate + 's');
storage.gold = goldText.gold;
storage.firingRate = firingRate;
// Update the centered rate text for the next upgrade
centeredRateText.setText('0.85s 10$');
centeredRateText.update(); // Ensure the text is updated immediately
// Update the rateAsset interaction for the next upgrade
rateAsset.down = function () {
if (goldText.gold >= 10) {
goldText.gold -= 10;
firingRate = 0.85;
rateText.setText('Rate: ' + firingRate + 's');
storage.gold = goldText.gold;
storage.firingRate = firingRate;
// Update the centered rate text for the next upgrade
centeredRateText.setText('0.75s 30$');
centeredRateText.update(); // Ensure the text is updated immediately
// Update the rateAsset interaction for the next upgrade
rateAsset.down = function () {
if (goldText.gold >= 30) {
goldText.gold -= 30;
firingRate = 0.80;
rateText.setText('Rate: ' + firingRate + 's');
storage.gold = goldText.gold;
storage.firingRate = firingRate;
// Update the centered rate text for the next upgrade
centeredRateText.setText('Max Level');
centeredRateText.update(); // Ensure the text is updated immediately
}
};
}
};
}
};
// Add the wave text to the game screen
var waveText = game.addChild(new WaveText());
// Add the gold text to the game screen
var goldText = game.addChild(new GoldText());
// Initialize firing rate variable from storage if available
var firingRate = storage.firingRate || 1.0;
var rateText = game.addChild(new RateText());
// Add the reset button to the game screen
var resetButton = game.addChild(new ResetButton());
game.update = function () {
// Spawn the cube asset from the cubestart asset every 60 game ticks or 48 game ticks if wave is above 9
var spawnRate;
if (waveText.wave > 14) {
spawnRate = Math.floor(60 * 1.25); // 1.25 seconds
} else if (waveText.wave > 9) {
spawnRate = 48;
} else {
spawnRate = 60;
}
if (LK.ticks % spawnRate == 0) {
var newCube;
if (waveText.wave > 14) {
newCube = new Bulk();
} else if (waveText.wave > 14) {
newCube = new Bulk();
} else if (waveText.wave > 5 && waveText.wave <= 14) {
newCube = new Speedcube();
} else {
newCube = new Cube();
}
newCube.x = cubestart.x;
newCube.y = cubestart.y;
game.addChild(newCube);
}
// Update the wave text every 600 game ticks
if (LK.ticks % 600 == 0 && LK.ticks != 0) {
waveText.wave++;
goldText.gold += 10;
storage.wave = waveText.wave;
storage.gold = goldText.gold;
}
// Make the slime asset shoot out the shoot asset to the right based on firingRate
if (LK.ticks % Math.floor(60 * firingRate) == 0) {
var newShoot = new Shoot();
newShoot.x = slime.x;
newShoot.y = slime.y;
newShoot.update = function () {
// Move the shoot to the right at a constant speed
this.x += this.speed;
};
game.addChild(newShoot);
}
// Check for collisions between Cube and Shoot objects
for (var i = 0; i < game.children.length; i++) {
var obj1 = game.children[i];
if (obj1 instanceof Cube || obj1 instanceof Speedcube || obj1 instanceof Bulk) {
for (var j = 0; j < game.children.length; j++) {
var obj2 = game.children[j];
if (obj2 instanceof Shoot && obj1.intersects(obj2)) {
// Destroy both objects on collision
obj1.health -= obj2.damage;
if (obj1.health <= 0) {
obj1.destroy();
}
obj2.destroy();
break;
}
}
}
}
};