/****
* Classes
****/
var Background = Container.expand(function () {
var self = Container.call(this);
self.attachAsset('background', {
width: 2048 * 1.5,
height: 2732 * 1.5,
anchorX: 0,
anchorY: 0
});
self.move = function () {
self.x -= game.speed;
if (self.x <= -2048) {
self.x = 0;
}
};
});
var Cactus = Container.expand(function () {
var self = Container.call(this);
var cactusGraphics = self.attachAsset('cactus', {
anchorX: 0.5,
anchorY: 1.0,
scaleX: 1 + Math.random() * 0.2,
scaleY: 1 + Math.random() * 0.2
});
self.move = function () {
if (LK.ticks % 600 == 0) {
game.speed *= 1.1;
}
self.x -= game.speed * 2;
if (LK.ticks % 60 == 0) {
self.scale.x *= -1;
}
};
});
var Cloud = Container.expand(function () {
var self = Container.call(this);
self.attachAsset('cloud', {
anchorX: 5.5,
anchorY: 0
});
self.x = 2048; // Position cloud outside of the playspace
self.move = function () {
self.x -= game.speed;
};
});
// Create a single instance of Cloud outside the game loop
var Collectible = Container.expand(function () {
var self = Container.call(this);
self.attachAsset('collectible', {
anchorX: 0.5,
anchorY: 0.5
});
self.move = function () {
self.x -= game.speed;
if (self.x < -100) {
self.destroy();
game.collectibles = [];
}
};
});
var Dinosaur = Container.expand(function () {
var self = Container.call(this);
var dinosaurGraphics = self.attachAsset('dinosaur', {
anchorX: 1.0,
anchorY: 1.0
});
self.isJumping = false;
self.jumpSpeed = 0;
self.gravity = 0.6;
self.jump = function () {
if (!self.isJumping && self.y >= game.floorLevel) {
self.isJumping = true;
self.jumpSpeed = -30;
}
};
self.update = function () {
if (self.isJumping) {
self.y += self.jumpSpeed;
self.jumpSpeed += self.gravity;
} else {
//self.y += 50 * Math.cos(LK.ticks % 5);
}
if (self.y > game.floorLevel) {
self.y = game.floorLevel;
self.isJumping = false;
}
if (LK.ticks % 18 == 0) {
self.scale.x *= self.scale.x > 1 ? 0.95 : 1.05;
self.scale.y *= self.scale.y > 1 ? 0.95 : 1.05;
//self.y += self.y > game.floorLevel ? 30 : -30;
//self.y += 100 * Math.cos(LK.ticks % 1000);
}
//self.y += 50 * Math.cos(LK.ticks % 1000);
};
});
var Floor = Container.expand(function () {
var self = Container.call(this);
self.attachAsset('Floor', {
anchorX: 0.5,
anchorY: 0
});
console.log("Floor instance created");
// Adjust the initial x-coordinate to be outside the playspace
self.x = 2048;
//self.y = 2048;
self.move = function () {
console.log("Floor move method called");
self.x -= game.speed;
// Reposition the floor instance when it moves out of view
if (self.x + self.width <= 0) {
self.x = 2048; // Assuming the width of the playspace is 2048
}
};
});
var Ground = Container.expand(function () {
var self = Container.call(this);
self.attachAsset('ground', {
anchorX: 0,
anchorY: -0.9
});
self.move = function () {
self.x -= game.speed * 2; // Increase ground speed
//if (self.x <= -100) {
if (self.x <= -1024) {
self.x = 0; // Adjusted to reset position sooner for seamless repetition
}
};
});
var NightBackground = Container.expand(function () {
var self = Container.call(this);
self.attachAsset('nightBackground', {
width: 2048 * 1.2,
height: 2732 * 1.2,
anchorX: 0.1,
anchorY: 0
});
/* self.move = function () {
self.x -= game.speed;
if (self.x <= -2048) {
self.x = 0;
}
};
*/
});
var Pterodactyl = Container.expand(function () {
var self = Container.call(this);
var pterodactylGraphics = self.attachAsset('pterodactyl', {
anchorX: 0.5,
anchorY: 0.5
});
self.direction = Math.random() < 0.5 ? 1 : -1;
self.move = function () {
if (LK.ticks % 600 == 0) {
game.speed *= 1.1;
}
self.x -= game.speed * 2;
//self.y += Math.sin(LK.ticks / 10) * 5;
self.rotation += Math.PI * 0.01 * self.direction;
};
});
var Star = Container.expand(function () {
var self = Container.call(this);
var starGraphics = self.attachAsset('particle', {
anchorX: 0.5,
anchorY: 0.5
});
self.vx = Math.random() * 80 - 40; // Horizontal velocity
self.vy = Math.random() * 80 - 40; // Vertical velocity
self.lifeSpan = 100; // Frames before disappearing
self.move = function () {
self.x += self.vx;
self.y += self.vy;
self.alpha -= 0.01;
self.lifeSpan--;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87ceeb
});
/****
* Game Code
****/
// Create a single instance of Cloud outside the game loop
game.collectibles = [];
game.particlesArray = [];
var floor = game.addChild(new Floor());
floor.x = 0;
//floor.y = 2732 - floor.height; // Align the floor with the bottom of the screen
floor.y = 900;
floor.visible = true;
game.setChildIndex(floor, game.children.length - 1);
var title = LK.gui.top.addChild(LK.getAsset('title', {
anchorX: 0,
anchorY: 0,
x: -250,
//-500,
y: 100
}));
//var background1 = game.addChildAt(new Background(), 0);
var nightBackground1 = game.addChildAt(new NightBackground(), 0);
//nightBackground1.visible = false;
//game.setChildIndex(background1, 0);
game.setChildIndex(nightBackground1, 0);
var scoreTxt = new Text2(LK.getScore().toString(), {
size: 150,
fill: '#ffffff'
});
scoreTxt.anchor.set(0.5, 0.5);
LK.gui.center.addChild(scoreTxt);
var ground1 = game.addChild(new Ground());
ground1.y = 2732 - 200; //ground1.height; // Align with the bottom of the playspace
var ground2 = game.addChild(new Ground());
ground2.x = ground1.width;
ground2.y = 2732 - 200; //ground2.height; // Align with the bottom of the playspace
game.speed = 5;
game.floorLevel = 2732 - 90; // Dinosaur stands on the floor
game.score = 0;
game.obstacles = [];
game.particlesArray = [];
var dinosaur = game.addChild(new Dinosaur());
dinosaur.x = 300;
dinosaur.y = game.floorLevel;
game.on('down', function () {
dinosaur.jump();
});
LK.on('tick', function () {
/*
if (LK.ticks % 2700 == 0) {
background1.visible = !background1.visible;
nightBackground1.visible = !nightBackground1.visible;
}
if (LK.ticks % 2700 == 1350) {
background1.visible = !background1.visible;
nightBackground1.visible = !nightBackground1.visible;
}
*/
dinosaur.update();
if (LK.ticks % 60 == 0) {
game.score += 1;
}
scoreTxt.setText(LK.getScore().toString());
LK.setScore(Math.floor(game.score)); // Update the score display
// Move the ground and background
floor.move();
ground1.move();
ground2.move();
// Handle obstacles
if (game.collectibles.length == 0 && LK.ticks % 420 == 0) {
// Add a new collectible every 7 to 14 seconds
var collectible = new Collectible();
collectible.y = game.floorLevel - Math.random() * 500 - 300; // Random height for collectible
collectible.x = 2048;
game.collectibles.push(collectible);
game.addChild(collectible);
}
if (game.score >= 200 ? LK.ticks % 60 == 0 : game.score >= 100 ? LK.ticks % 102 == 0 : LK.ticks % 120 == 0) {
// Add a new obstacle every 1.7 seconds
var obstacle;
if (Math.random() < 0.5) {
obstacle = new Cactus();
obstacle.y = game.floorLevel;
} else {
obstacle = new Pterodactyl();
obstacle.y = game.floorLevel - Math.random() * 300 - 300; // Random height for pterodactyl
}
obstacle.x = 2048;
game.obstacles.push(obstacle);
game.addChild(obstacle);
// Add a 10% chance to spawn two pterodactyls rather than one
if (Math.random() < 0.1) {
var secondObstacle = new Pterodactyl();
secondObstacle.y = game.floorLevel - Math.random() * 300 - 300; // Random height for second pterodactyl
secondObstacle.x = 2048;
game.obstacles.push(secondObstacle);
game.addChild(secondObstacle);
}
}
for (var i = game.collectibles.length - 1; i >= 0; i--) {
game.collectibles[i].move();
if (dinosaur.intersects(game.collectibles[i])) {
// Destroy the collectible and add +10 to the score when dinosaur collides with it
game.collectibles[i].destroy();
game.collectibles = [];
game.score += 10;
// Trigger star explosion
for (var j = 0; j < 20; j++) {
var star = new Star();
star.x = dinosaur.x;
star.y = dinosaur.y - 200;
game.particlesArray.push(star);
game.addChild(star);
}
}
if (game.collectibles[i] && game.collectibles[i].x < -100) {
// Remove off-screen collectibles
game.collectibles[i].destroy();
game.collectibles.splice(i, 1);
}
}
for (var i = game.particlesArray.length - 1; i >= 0; i--) {
game.particlesArray[i].move();
if (game.particlesArray[i].lifespan <= 0) {
game.particlesArray[i].destroy();
game.particlesArray[i].splice(i, 1);
}
}
for (var i = game.obstacles.length - 1; i >= 0; i--) {
game.obstacles[i].move();
if (dinosaur.intersects(game.obstacles[i])) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
if (game.obstacles[i].x < -100) {
// Remove off-screen obstacles
game.obstacles[i].destroy();
game.obstacles.splice(i, 1);
}
}
});