/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1");
/****
* Classes
****/
var BatteryIndicator = Container.expand(function () {
var self = Container.call(this);
var batteryOutline = self.attachAsset('batteryIndicator', {
anchorX: 0,
anchorY: 0,
tint: 0xAAAAAA
});
var batteryLevel = self.attachAsset('batteryIndicator', {
anchorX: 0,
anchorY: 0,
y: 100,
tint: 0x00FF00
});
batteryLevel.scale.y = -1; // Flip to grow from bottom
self.updateLevel = function (level) {
var percentage = level / 100;
batteryLevel.scale.y = -percentage;
// Change color based on battery level
if (level > 70) {
batteryLevel.tint = 0x00FF00; // Green
} else if (level > 30) {
batteryLevel.tint = 0xFFFF00; // Yellow
} else {
batteryLevel.tint = 0xFF0000; // Red
}
};
return self;
});
var CameraView = Container.expand(function () {
var self = Container.call(this);
var viewShape = self.attachAsset('cameraView', {
anchorX: 0.5,
anchorY: 0.5,
alpha: 0.8
});
var cameraText = new Text2('CAM 1A', {
size: 60,
fill: 0x00FF00
});
cameraText.anchor.set(0.5, 0.2);
self.addChild(cameraText);
var recordText = new Text2('REC', {
size: 30,
fill: 0x00FF00
});
recordText.anchor.set(0, 0);
recordText.x = -viewShape.width / 2 + 20;
recordText.y = -viewShape.height / 2 + 20;
self.addChild(recordText);
var freddySprite = self.attachAsset('freddy', {
anchorX: 0.5,
anchorY: 0.5,
alpha: 0
});
var staticEffect = 0;
self.activeCamera = null;
self.setActiveCamera = function (camera) {
self.activeCamera = camera;
cameraText.setText('CAM ' + camera.id);
self.updateFreddyVisibility();
};
self.updateFreddyVisibility = function () {
if (self.activeCamera && self.activeCamera.checkForFreddy()) {
freddySprite.alpha = 1;
} else {
freddySprite.alpha = 0;
}
};
self.update = function () {
// Blinking REC text
if (Math.floor(LK.ticks / 30) % 2 === 0) {
recordText.alpha = 1;
} else {
recordText.alpha = 0;
}
// Static effect
staticEffect = Math.random() * 0.2;
viewShape.alpha = 0.8 - staticEffect;
// Update freddy visibility if camera changes
if (self.activeCamera) {
self.updateFreddyVisibility();
}
};
return self;
});
var Clock = Container.expand(function () {
var self = Container.call(this);
var clockShape = self.attachAsset('clock', {
anchorX: 0.5,
anchorY: 0.5
});
var timeText = new Text2('12 AM', {
size: 50,
fill: 0x90EE90
});
timeText.anchor.set(0.5, 0.5);
self.addChild(timeText);
self.currentHour = 12;
self.isAM = true;
self.minuteCounter = 0;
self.update = function () {
self.minuteCounter++;
// Every 2100 ticks (35 seconds) = 1 hour in game time
if (self.minuteCounter >= 2100) {
self.minuteCounter = 0;
self.advanceHour();
}
};
self.advanceHour = function () {
self.currentHour++;
if (self.currentHour > 12) {
self.currentHour = 1;
}
if (self.currentHour === 2 || self.currentHour === 4) {
// Trigger Freddy at 2 AM and 4 AM
freddy.setLocation('office'); // Move Freddy to the player
}
if (self.currentHour === 6 && self.isAM) {
// 6 AM - player survived the night!
gameState.winGame();
}
timeText.setText(self.currentHour + (self.isAM ? ' AM' : ' PM'));
};
return self;
});
var Flashlight = Container.expand(function () {
var self = Container.call(this);
var beam = self.attachAsset('flashlightBeam', {
anchorX: 0.5,
anchorY: 0.5,
alpha: 0.3
});
self.isOn = false;
self.batteryLevel = 100;
self.drainRate = 0.05;
self.turnOn = function () {
if (self.batteryLevel > 0) {
self.isOn = true;
beam.alpha = 0.3;
}
};
self.turnOff = function () {
self.isOn = false;
beam.alpha = 0;
};
self.update = function () {
if (self.isOn && self.batteryLevel > 0) {
self.batteryLevel -= self.drainRate;
if (self.batteryLevel <= 0) {
self.batteryLevel = 0;
self.turnOff();
LK.getSound('battery_low').play();
}
}
};
self.getBatteryLevel = function () {
return self.batteryLevel;
};
return self;
});
var Freddy = Container.expand(function () {
var self = Container.call(this);
var freddyShape = self.attachAsset('freddy', {
anchorX: 0.5,
anchorY: 0.5
});
self.isActive = false;
self.currentLocation = 'stage';
self.aggression = 1;
self.moveTimer = 0;
self.moveThreshold = 600; // 10 seconds
self.canMove = true;
self.alpha = 0;
self.activate = function () {
self.isActive = true;
return self;
};
self.setLocation = function (location) {
self.currentLocation = location;
return self;
};
self.getLocation = function () {
return self.currentLocation;
};
self.setAggression = function (level) {
self.aggression = level;
self.moveThreshold = 1200 - level * 120; // Decreases time between moves as aggression rises
return self;
};
self.update = function () {
if (!self.isActive) {
return;
}
self.moveTimer++;
if (self.moveTimer >= self.moveThreshold && self.canMove) {
self.moveTimer = 0;
self.canMove = false;
// Attempt to move based on aggression level
if (Math.random() * 10 < self.aggression) {
self.move();
}
// Reset move capability after delay
LK.setTimeout(function () {
self.canMove = true;
}, 2000);
}
};
self.move = function () {
var locations = {
'stage': ['diningArea'],
'diningArea': ['westHall', 'eastHall'],
'westHall': ['westHallCorner', 'diningArea'],
'eastHall': ['eastHallCorner', 'diningArea'],
'westHallCorner': ['office', 'westHall'],
'eastHallCorner': ['office', 'eastHall'],
'office': ['jumpscare']
};
var possibleMoves = locations[self.currentLocation];
if (!possibleMoves) {
return;
}
var nextLocation = possibleMoves[Math.floor(Math.random() * possibleMoves.length)];
self.currentLocation = nextLocation;
if (nextLocation === 'jumpscare') {
gameState.triggerJumpscare();
}
LK.getSound('footsteps').play();
// Update camera states
for (var i = 0; i < cameras.length; i++) {
cameras[i].setFreddyPresent(cameras[i].id === self.currentLocation);
}
};
self.show = function () {
self.alpha = 1;
};
self.hide = function () {
self.alpha = 0;
};
return self;
});
var SecurityCamera = Container.expand(function () {
var self = Container.call(this);
var cameraShape = self.attachAsset('camera', {
anchorX: 0.5,
anchorY: 0.5
});
self.id = '';
self.isActive = false;
self.freddyPresent = false;
self.activate = function () {
self.isActive = true;
LK.getSound('camera').play();
return self;
};
self.deactivate = function () {
self.isActive = false;
return self;
};
self.setId = function (id) {
self.id = id;
return self;
};
self.checkForFreddy = function () {
return self.freddyPresent;
};
self.setFreddyPresent = function (isPresent) {
self.freddyPresent = isPresent;
return self;
};
return self;
});
var UIButton = Container.expand(function () {
var self = Container.call(this);
var buttonShape = self.attachAsset('ui_button', {
anchorX: 0.5,
anchorY: 0.5
});
var buttonText = new Text2('Button', {
size: 40,
fill: 0x90EE90
});
buttonText.anchor.set(0.5, 0.5);
self.addChild(buttonText);
self.action = null;
self.setText = function (text) {
buttonText.setText(text);
return self;
};
self.setAction = function (callback) {
self.action = callback;
return self;
};
self.down = function (x, y, obj) {
buttonShape.tint = 0x888888;
};
self.up = function (x, y, obj) {
buttonShape.tint = 0xFFFFFF;
if (self.action) {
self.action();
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000
});
/****
* Game Code
****/
// Game state management
var gameState = {
currentState: 'menu',
// 'menu', 'game', 'camera', 'gameover', 'win'
isHiding: false,
setGameState: function setGameState(state) {
this.currentState = state;
// Handle state transitions
if (state === 'menu') {
hideAllScreens();
showMenuScreen();
} else if (state === 'game') {
hideAllScreens();
showGameScreen();
flashlight.turnOn();
} else if (state === 'camera') {
hideAllScreens();
showCameraScreen();
flashlight.turnOff();
} else if (state === 'gameover') {
hideAllScreens();
showGameOverScreen();
} else if (state === 'win') {
hideAllScreens();
showWinScreen();
}
},
toggleCamera: function toggleCamera() {
if (this.currentState === 'game') {
this.setGameState('camera');
} else if (this.currentState === 'camera') {
this.setGameState('game');
}
},
toggleHiding: function toggleHiding() {
this.isHiding = !this.isHiding;
if (this.isHiding) {
player.alpha = 0.3;
} else {
player.alpha = 1;
}
},
triggerJumpscare: function triggerJumpscare() {
if (this.isHiding) {
// Player avoided jumpscare by hiding
this.isHiding = false;
player.alpha = 1;
freddy.setLocation('eastHall'); // Send Freddy back
} else {
// Game over - jumpscare!
LK.getSound('jumpscare').play();
flashlight.turnOff();
LK.effects.flashScreen(0xFF0000, 500);
LK.setTimeout(function () {
gameState.setGameState('gameover');
}, 1000);
}
},
winGame: function winGame() {
this.setGameState('win');
LK.setScore(100);
}
};
// Create game elements
var securityRoom = LK.getAsset('securityRoom', {
anchorX: 0,
anchorY: 0
});
var hallway = LK.getAsset('hallway', {
anchorX: 0,
anchorY: 0
});
var player = new Container();
var playerShape = player.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
var flashlight = new Flashlight();
var batteryIndicator = new BatteryIndicator();
var leftDoor = LK.getAsset('door', {
anchorX: 0.5,
anchorY: 0.5
});
var rightDoor = LK.getAsset('door', {
anchorX: 0.5,
anchorY: 0.5
});
var hidingSpot = LK.getAsset('hidingSpot', {
anchorX: 0.5,
anchorY: 0.5
});
var freddy = new Freddy();
var cameras = [];
var cameraView = new CameraView();
var clock = new Clock();
// UI elements
var cameraButton = new UIButton().setText('Camera');
var flashlightButton = new UIButton().setText('Flashlight');
var hideButton = new UIButton().setText('Hide');
var startButton = new UIButton().setText('Start Game');
var restartButton = new UIButton().setText('Try Again');
// Create containers for different screens
var menuScreen = new Container();
var gameScreen = new Container();
var cameraScreen = new Container();
var gameOverScreen = new Container();
var winScreen = new Container();
// Menu screen setup
function setupMenuScreen() {
var titleText = new Text2('Night Shift at Freddy\'s', {
size: 120,
fill: 0x00FF00
});
titleText.anchor.set(0.5, 0.5);
titleText.x = 2048 / 2;
titleText.y = 600;
var instructionsText = new Text2('Survive until 6 AM!\n\nUse your flashlight wisely\nWatch cameras for Freddy\nHide when in danger', {
size: 70,
fill: 0x00FF00
});
instructionsText.anchor.set(0.5, 0.5);
instructionsText.x = 2048 / 2;
instructionsText.y = 1200;
startButton.x = 2048 / 2;
startButton.y = 1800;
startButton.setAction(function () {
gameState.setGameState('game');
LK.playMusic('background_music', {
volume: 0.3
});
freddy.activate();
});
menuScreen.addChild(titleText);
menuScreen.addChild(instructionsText);
menuScreen.addChild(startButton);
}
// Game screen setup
function setupGameScreen() {
securityRoom.x = 0;
securityRoom.y = 0;
player.x = 2048 / 2;
player.y = 2732 / 2 + 400;
flashlight.x = player.x;
flashlight.y = player.y;
batteryIndicator.x = 100;
batteryIndicator.y = 200;
leftDoor.x = 400;
leftDoor.y = 2732 / 2;
leftDoor.interactive = true;
leftDoor.on('pointerdown', function () {
freddy.setLocation('office');
gameState.triggerJumpscare();
});
rightDoor.x = 2048 - 400;
rightDoor.y = 2732 / 2;
rightDoor.interactive = true;
rightDoor.on('pointerdown', function () {
freddy.setLocation('office');
gameState.triggerJumpscare();
});
hidingSpot.x = 2048 / 2;
hidingSpot.y = 2732 - 300;
cameraButton.x = 2048 - 150;
cameraButton.y = 200;
cameraButton.setAction(function () {
gameState.toggleCamera();
});
flashlightButton.x = 2048 - 150;
flashlightButton.y = 350;
flashlightButton.setAction(function () {
if (flashlight.isOn) {
flashlight.turnOff();
} else {
flashlight.turnOn();
}
});
hideButton.x = 2048 - 150;
hideButton.y = 500;
hideButton.setAction(function () {
gameState.toggleHiding();
});
clock.x = 2048 / 2;
clock.y = 100;
gameScreen.addChild(securityRoom);
gameScreen.addChild(leftDoor);
gameScreen.addChild(rightDoor);
gameScreen.addChild(hidingSpot);
gameScreen.addChild(player);
gameScreen.addChild(flashlight);
gameScreen.addChild(cameraButton);
gameScreen.addChild(flashlightButton);
gameScreen.addChild(hideButton);
gameScreen.addChild(batteryIndicator);
gameScreen.addChild(clock);
}
// Camera screen setup
function setupCameraScreen() {
// Create camera objects
var locationIds = ['stage', 'diningArea', 'westHall', 'eastHall', 'westHallCorner', 'eastHallCorner'];
var locationNames = ['1A', '1B', '2A', '2B', '3A', '3B'];
for (var i = 0; i < locationIds.length; i++) {
var cam = new SecurityCamera();
cam.setId(locationIds[i]);
cameras.push(cam);
// Create camera selection buttons
var camButton = new UIButton().setText(locationNames[i]);
camButton.x = 200 + i % 3 * 220;
camButton.y = 2732 - 300 + Math.floor(i / 3) * 120;
// Use immediately invoked function to capture current camera
(function (camera) {
camButton.setAction(function () {
cameraView.setActiveCamera(camera);
});
})(cam);
cameraScreen.addChild(camButton);
}
cameraView.x = 2048 / 2;
cameraView.y = 2732 / 2 - 300;
var backButton = new UIButton().setText('Back');
backButton.x = 2048 - 150;
backButton.y = 200;
backButton.setAction(function () {
gameState.toggleCamera();
});
cameraScreen.addChild(cameraView);
cameraScreen.addChild(backButton);
// Set initial camera
cameraView.setActiveCamera(cameras[0]);
}
// Game over screen setup
function setupGameOverScreen() {
var gameOverText = new Text2('GAME OVER', {
size: 150,
fill: 0x00FF00
});
gameOverText.anchor.set(0.5, 0.5);
gameOverText.x = 2048 / 2;
gameOverText.y = 1200;
restartButton.x = 2048 / 2;
restartButton.y = 1800;
restartButton.setAction(function () {
LK.showGameOver(); // Use LK's game over mechanism to reset the game
});
gameOverScreen.addChild(gameOverText);
gameOverScreen.addChild(restartButton);
}
// Win screen setup
function setupWinScreen() {
var winText = new Text2('YOU SURVIVED!', {
size: 150,
fill: 0x00FF00
});
winText.anchor.set(0.5, 0.5);
winText.x = 2048 / 2;
winText.y = 1200;
var survivalText = new Text2('Congratulations on making it to 6 AM!', {
size: 70,
fill: 0x00FF00
});
survivalText.anchor.set(0.5, 0.5);
survivalText.x = 2048 / 2;
survivalText.y = 1400;
var playAgainButton = new UIButton().setText('Play Again');
playAgainButton.x = 2048 / 2;
playAgainButton.y = 1800;
playAgainButton.setAction(function () {
LK.showYouWin(); // Use LK's win mechanism to reset the game
});
winScreen.addChild(winText);
winScreen.addChild(survivalText);
winScreen.addChild(playAgainButton);
}
// Screen visibility functions
function hideAllScreens() {
menuScreen.visible = false;
gameScreen.visible = false;
cameraScreen.visible = false;
gameOverScreen.visible = false;
winScreen.visible = false;
}
function showMenuScreen() {
menuScreen.visible = true;
}
function showGameScreen() {
gameScreen.visible = true;
}
function showCameraScreen() {
cameraScreen.visible = true;
}
function showGameOverScreen() {
gameOverScreen.visible = true;
}
function showWinScreen() {
winScreen.visible = true;
}
// Setup screens
setupMenuScreen();
setupGameScreen();
setupCameraScreen();
setupGameOverScreen();
setupWinScreen();
// Add screens to game
game.addChild(menuScreen);
game.addChild(gameScreen);
game.addChild(cameraScreen);
game.addChild(gameOverScreen);
game.addChild(winScreen);
// Initialize game state
gameState.setGameState('menu');
// Game update loop
game.update = function () {
// Update flashlight position
if (gameState.currentState === 'game') {
flashlight.update();
batteryIndicator.updateLevel(flashlight.getBatteryLevel());
clock.update();
}
// Update freddy
freddy.update();
// Update camera view
if (gameState.currentState === 'camera') {
cameraView.update();
}
// Debug info
if (LK.ticks % 600 === 0) {
console.log("Freddy location: " + freddy.getLocation());
console.log("Battery: " + flashlight.getBatteryLevel().toFixed(1) + "%");
}
};
// Game input handling
game.down = function (x, y, obj) {
// Handled by UI buttons
};
game.up = function (x, y, obj) {
// Handled by UI buttons
};
// Play ambient sounds
LK.getSound('ambience').play(); /****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1");
/****
* Classes
****/
var BatteryIndicator = Container.expand(function () {
var self = Container.call(this);
var batteryOutline = self.attachAsset('batteryIndicator', {
anchorX: 0,
anchorY: 0,
tint: 0xAAAAAA
});
var batteryLevel = self.attachAsset('batteryIndicator', {
anchorX: 0,
anchorY: 0,
y: 100,
tint: 0x00FF00
});
batteryLevel.scale.y = -1; // Flip to grow from bottom
self.updateLevel = function (level) {
var percentage = level / 100;
batteryLevel.scale.y = -percentage;
// Change color based on battery level
if (level > 70) {
batteryLevel.tint = 0x00FF00; // Green
} else if (level > 30) {
batteryLevel.tint = 0xFFFF00; // Yellow
} else {
batteryLevel.tint = 0xFF0000; // Red
}
};
return self;
});
var CameraView = Container.expand(function () {
var self = Container.call(this);
var viewShape = self.attachAsset('cameraView', {
anchorX: 0.5,
anchorY: 0.5,
alpha: 0.8
});
var cameraText = new Text2('CAM 1A', {
size: 60,
fill: 0x00FF00
});
cameraText.anchor.set(0.5, 0.2);
self.addChild(cameraText);
var recordText = new Text2('REC', {
size: 30,
fill: 0x00FF00
});
recordText.anchor.set(0, 0);
recordText.x = -viewShape.width / 2 + 20;
recordText.y = -viewShape.height / 2 + 20;
self.addChild(recordText);
var freddySprite = self.attachAsset('freddy', {
anchorX: 0.5,
anchorY: 0.5,
alpha: 0
});
var staticEffect = 0;
self.activeCamera = null;
self.setActiveCamera = function (camera) {
self.activeCamera = camera;
cameraText.setText('CAM ' + camera.id);
self.updateFreddyVisibility();
};
self.updateFreddyVisibility = function () {
if (self.activeCamera && self.activeCamera.checkForFreddy()) {
freddySprite.alpha = 1;
} else {
freddySprite.alpha = 0;
}
};
self.update = function () {
// Blinking REC text
if (Math.floor(LK.ticks / 30) % 2 === 0) {
recordText.alpha = 1;
} else {
recordText.alpha = 0;
}
// Static effect
staticEffect = Math.random() * 0.2;
viewShape.alpha = 0.8 - staticEffect;
// Update freddy visibility if camera changes
if (self.activeCamera) {
self.updateFreddyVisibility();
}
};
return self;
});
var Clock = Container.expand(function () {
var self = Container.call(this);
var clockShape = self.attachAsset('clock', {
anchorX: 0.5,
anchorY: 0.5
});
var timeText = new Text2('12 AM', {
size: 50,
fill: 0x90EE90
});
timeText.anchor.set(0.5, 0.5);
self.addChild(timeText);
self.currentHour = 12;
self.isAM = true;
self.minuteCounter = 0;
self.update = function () {
self.minuteCounter++;
// Every 2100 ticks (35 seconds) = 1 hour in game time
if (self.minuteCounter >= 2100) {
self.minuteCounter = 0;
self.advanceHour();
}
};
self.advanceHour = function () {
self.currentHour++;
if (self.currentHour > 12) {
self.currentHour = 1;
}
if (self.currentHour === 2 || self.currentHour === 4) {
// Trigger Freddy at 2 AM and 4 AM
freddy.setLocation('office'); // Move Freddy to the player
}
if (self.currentHour === 6 && self.isAM) {
// 6 AM - player survived the night!
gameState.winGame();
}
timeText.setText(self.currentHour + (self.isAM ? ' AM' : ' PM'));
};
return self;
});
var Flashlight = Container.expand(function () {
var self = Container.call(this);
var beam = self.attachAsset('flashlightBeam', {
anchorX: 0.5,
anchorY: 0.5,
alpha: 0.3
});
self.isOn = false;
self.batteryLevel = 100;
self.drainRate = 0.05;
self.turnOn = function () {
if (self.batteryLevel > 0) {
self.isOn = true;
beam.alpha = 0.3;
}
};
self.turnOff = function () {
self.isOn = false;
beam.alpha = 0;
};
self.update = function () {
if (self.isOn && self.batteryLevel > 0) {
self.batteryLevel -= self.drainRate;
if (self.batteryLevel <= 0) {
self.batteryLevel = 0;
self.turnOff();
LK.getSound('battery_low').play();
}
}
};
self.getBatteryLevel = function () {
return self.batteryLevel;
};
return self;
});
var Freddy = Container.expand(function () {
var self = Container.call(this);
var freddyShape = self.attachAsset('freddy', {
anchorX: 0.5,
anchorY: 0.5
});
self.isActive = false;
self.currentLocation = 'stage';
self.aggression = 1;
self.moveTimer = 0;
self.moveThreshold = 600; // 10 seconds
self.canMove = true;
self.alpha = 0;
self.activate = function () {
self.isActive = true;
return self;
};
self.setLocation = function (location) {
self.currentLocation = location;
return self;
};
self.getLocation = function () {
return self.currentLocation;
};
self.setAggression = function (level) {
self.aggression = level;
self.moveThreshold = 1200 - level * 120; // Decreases time between moves as aggression rises
return self;
};
self.update = function () {
if (!self.isActive) {
return;
}
self.moveTimer++;
if (self.moveTimer >= self.moveThreshold && self.canMove) {
self.moveTimer = 0;
self.canMove = false;
// Attempt to move based on aggression level
if (Math.random() * 10 < self.aggression) {
self.move();
}
// Reset move capability after delay
LK.setTimeout(function () {
self.canMove = true;
}, 2000);
}
};
self.move = function () {
var locations = {
'stage': ['diningArea'],
'diningArea': ['westHall', 'eastHall'],
'westHall': ['westHallCorner', 'diningArea'],
'eastHall': ['eastHallCorner', 'diningArea'],
'westHallCorner': ['office', 'westHall'],
'eastHallCorner': ['office', 'eastHall'],
'office': ['jumpscare']
};
var possibleMoves = locations[self.currentLocation];
if (!possibleMoves) {
return;
}
var nextLocation = possibleMoves[Math.floor(Math.random() * possibleMoves.length)];
self.currentLocation = nextLocation;
if (nextLocation === 'jumpscare') {
gameState.triggerJumpscare();
}
LK.getSound('footsteps').play();
// Update camera states
for (var i = 0; i < cameras.length; i++) {
cameras[i].setFreddyPresent(cameras[i].id === self.currentLocation);
}
};
self.show = function () {
self.alpha = 1;
};
self.hide = function () {
self.alpha = 0;
};
return self;
});
var SecurityCamera = Container.expand(function () {
var self = Container.call(this);
var cameraShape = self.attachAsset('camera', {
anchorX: 0.5,
anchorY: 0.5
});
self.id = '';
self.isActive = false;
self.freddyPresent = false;
self.activate = function () {
self.isActive = true;
LK.getSound('camera').play();
return self;
};
self.deactivate = function () {
self.isActive = false;
return self;
};
self.setId = function (id) {
self.id = id;
return self;
};
self.checkForFreddy = function () {
return self.freddyPresent;
};
self.setFreddyPresent = function (isPresent) {
self.freddyPresent = isPresent;
return self;
};
return self;
});
var UIButton = Container.expand(function () {
var self = Container.call(this);
var buttonShape = self.attachAsset('ui_button', {
anchorX: 0.5,
anchorY: 0.5
});
var buttonText = new Text2('Button', {
size: 40,
fill: 0x90EE90
});
buttonText.anchor.set(0.5, 0.5);
self.addChild(buttonText);
self.action = null;
self.setText = function (text) {
buttonText.setText(text);
return self;
};
self.setAction = function (callback) {
self.action = callback;
return self;
};
self.down = function (x, y, obj) {
buttonShape.tint = 0x888888;
};
self.up = function (x, y, obj) {
buttonShape.tint = 0xFFFFFF;
if (self.action) {
self.action();
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000
});
/****
* Game Code
****/
// Game state management
var gameState = {
currentState: 'menu',
// 'menu', 'game', 'camera', 'gameover', 'win'
isHiding: false,
setGameState: function setGameState(state) {
this.currentState = state;
// Handle state transitions
if (state === 'menu') {
hideAllScreens();
showMenuScreen();
} else if (state === 'game') {
hideAllScreens();
showGameScreen();
flashlight.turnOn();
} else if (state === 'camera') {
hideAllScreens();
showCameraScreen();
flashlight.turnOff();
} else if (state === 'gameover') {
hideAllScreens();
showGameOverScreen();
} else if (state === 'win') {
hideAllScreens();
showWinScreen();
}
},
toggleCamera: function toggleCamera() {
if (this.currentState === 'game') {
this.setGameState('camera');
} else if (this.currentState === 'camera') {
this.setGameState('game');
}
},
toggleHiding: function toggleHiding() {
this.isHiding = !this.isHiding;
if (this.isHiding) {
player.alpha = 0.3;
} else {
player.alpha = 1;
}
},
triggerJumpscare: function triggerJumpscare() {
if (this.isHiding) {
// Player avoided jumpscare by hiding
this.isHiding = false;
player.alpha = 1;
freddy.setLocation('eastHall'); // Send Freddy back
} else {
// Game over - jumpscare!
LK.getSound('jumpscare').play();
flashlight.turnOff();
LK.effects.flashScreen(0xFF0000, 500);
LK.setTimeout(function () {
gameState.setGameState('gameover');
}, 1000);
}
},
winGame: function winGame() {
this.setGameState('win');
LK.setScore(100);
}
};
// Create game elements
var securityRoom = LK.getAsset('securityRoom', {
anchorX: 0,
anchorY: 0
});
var hallway = LK.getAsset('hallway', {
anchorX: 0,
anchorY: 0
});
var player = new Container();
var playerShape = player.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
var flashlight = new Flashlight();
var batteryIndicator = new BatteryIndicator();
var leftDoor = LK.getAsset('door', {
anchorX: 0.5,
anchorY: 0.5
});
var rightDoor = LK.getAsset('door', {
anchorX: 0.5,
anchorY: 0.5
});
var hidingSpot = LK.getAsset('hidingSpot', {
anchorX: 0.5,
anchorY: 0.5
});
var freddy = new Freddy();
var cameras = [];
var cameraView = new CameraView();
var clock = new Clock();
// UI elements
var cameraButton = new UIButton().setText('Camera');
var flashlightButton = new UIButton().setText('Flashlight');
var hideButton = new UIButton().setText('Hide');
var startButton = new UIButton().setText('Start Game');
var restartButton = new UIButton().setText('Try Again');
// Create containers for different screens
var menuScreen = new Container();
var gameScreen = new Container();
var cameraScreen = new Container();
var gameOverScreen = new Container();
var winScreen = new Container();
// Menu screen setup
function setupMenuScreen() {
var titleText = new Text2('Night Shift at Freddy\'s', {
size: 120,
fill: 0x00FF00
});
titleText.anchor.set(0.5, 0.5);
titleText.x = 2048 / 2;
titleText.y = 600;
var instructionsText = new Text2('Survive until 6 AM!\n\nUse your flashlight wisely\nWatch cameras for Freddy\nHide when in danger', {
size: 70,
fill: 0x00FF00
});
instructionsText.anchor.set(0.5, 0.5);
instructionsText.x = 2048 / 2;
instructionsText.y = 1200;
startButton.x = 2048 / 2;
startButton.y = 1800;
startButton.setAction(function () {
gameState.setGameState('game');
LK.playMusic('background_music', {
volume: 0.3
});
freddy.activate();
});
menuScreen.addChild(titleText);
menuScreen.addChild(instructionsText);
menuScreen.addChild(startButton);
}
// Game screen setup
function setupGameScreen() {
securityRoom.x = 0;
securityRoom.y = 0;
player.x = 2048 / 2;
player.y = 2732 / 2 + 400;
flashlight.x = player.x;
flashlight.y = player.y;
batteryIndicator.x = 100;
batteryIndicator.y = 200;
leftDoor.x = 400;
leftDoor.y = 2732 / 2;
leftDoor.interactive = true;
leftDoor.on('pointerdown', function () {
freddy.setLocation('office');
gameState.triggerJumpscare();
});
rightDoor.x = 2048 - 400;
rightDoor.y = 2732 / 2;
rightDoor.interactive = true;
rightDoor.on('pointerdown', function () {
freddy.setLocation('office');
gameState.triggerJumpscare();
});
hidingSpot.x = 2048 / 2;
hidingSpot.y = 2732 - 300;
cameraButton.x = 2048 - 150;
cameraButton.y = 200;
cameraButton.setAction(function () {
gameState.toggleCamera();
});
flashlightButton.x = 2048 - 150;
flashlightButton.y = 350;
flashlightButton.setAction(function () {
if (flashlight.isOn) {
flashlight.turnOff();
} else {
flashlight.turnOn();
}
});
hideButton.x = 2048 - 150;
hideButton.y = 500;
hideButton.setAction(function () {
gameState.toggleHiding();
});
clock.x = 2048 / 2;
clock.y = 100;
gameScreen.addChild(securityRoom);
gameScreen.addChild(leftDoor);
gameScreen.addChild(rightDoor);
gameScreen.addChild(hidingSpot);
gameScreen.addChild(player);
gameScreen.addChild(flashlight);
gameScreen.addChild(cameraButton);
gameScreen.addChild(flashlightButton);
gameScreen.addChild(hideButton);
gameScreen.addChild(batteryIndicator);
gameScreen.addChild(clock);
}
// Camera screen setup
function setupCameraScreen() {
// Create camera objects
var locationIds = ['stage', 'diningArea', 'westHall', 'eastHall', 'westHallCorner', 'eastHallCorner'];
var locationNames = ['1A', '1B', '2A', '2B', '3A', '3B'];
for (var i = 0; i < locationIds.length; i++) {
var cam = new SecurityCamera();
cam.setId(locationIds[i]);
cameras.push(cam);
// Create camera selection buttons
var camButton = new UIButton().setText(locationNames[i]);
camButton.x = 200 + i % 3 * 220;
camButton.y = 2732 - 300 + Math.floor(i / 3) * 120;
// Use immediately invoked function to capture current camera
(function (camera) {
camButton.setAction(function () {
cameraView.setActiveCamera(camera);
});
})(cam);
cameraScreen.addChild(camButton);
}
cameraView.x = 2048 / 2;
cameraView.y = 2732 / 2 - 300;
var backButton = new UIButton().setText('Back');
backButton.x = 2048 - 150;
backButton.y = 200;
backButton.setAction(function () {
gameState.toggleCamera();
});
cameraScreen.addChild(cameraView);
cameraScreen.addChild(backButton);
// Set initial camera
cameraView.setActiveCamera(cameras[0]);
}
// Game over screen setup
function setupGameOverScreen() {
var gameOverText = new Text2('GAME OVER', {
size: 150,
fill: 0x00FF00
});
gameOverText.anchor.set(0.5, 0.5);
gameOverText.x = 2048 / 2;
gameOverText.y = 1200;
restartButton.x = 2048 / 2;
restartButton.y = 1800;
restartButton.setAction(function () {
LK.showGameOver(); // Use LK's game over mechanism to reset the game
});
gameOverScreen.addChild(gameOverText);
gameOverScreen.addChild(restartButton);
}
// Win screen setup
function setupWinScreen() {
var winText = new Text2('YOU SURVIVED!', {
size: 150,
fill: 0x00FF00
});
winText.anchor.set(0.5, 0.5);
winText.x = 2048 / 2;
winText.y = 1200;
var survivalText = new Text2('Congratulations on making it to 6 AM!', {
size: 70,
fill: 0x00FF00
});
survivalText.anchor.set(0.5, 0.5);
survivalText.x = 2048 / 2;
survivalText.y = 1400;
var playAgainButton = new UIButton().setText('Play Again');
playAgainButton.x = 2048 / 2;
playAgainButton.y = 1800;
playAgainButton.setAction(function () {
LK.showYouWin(); // Use LK's win mechanism to reset the game
});
winScreen.addChild(winText);
winScreen.addChild(survivalText);
winScreen.addChild(playAgainButton);
}
// Screen visibility functions
function hideAllScreens() {
menuScreen.visible = false;
gameScreen.visible = false;
cameraScreen.visible = false;
gameOverScreen.visible = false;
winScreen.visible = false;
}
function showMenuScreen() {
menuScreen.visible = true;
}
function showGameScreen() {
gameScreen.visible = true;
}
function showCameraScreen() {
cameraScreen.visible = true;
}
function showGameOverScreen() {
gameOverScreen.visible = true;
}
function showWinScreen() {
winScreen.visible = true;
}
// Setup screens
setupMenuScreen();
setupGameScreen();
setupCameraScreen();
setupGameOverScreen();
setupWinScreen();
// Add screens to game
game.addChild(menuScreen);
game.addChild(gameScreen);
game.addChild(cameraScreen);
game.addChild(gameOverScreen);
game.addChild(winScreen);
// Initialize game state
gameState.setGameState('menu');
// Game update loop
game.update = function () {
// Update flashlight position
if (gameState.currentState === 'game') {
flashlight.update();
batteryIndicator.updateLevel(flashlight.getBatteryLevel());
clock.update();
}
// Update freddy
freddy.update();
// Update camera view
if (gameState.currentState === 'camera') {
cameraView.update();
}
// Debug info
if (LK.ticks % 600 === 0) {
console.log("Freddy location: " + freddy.getLocation());
console.log("Battery: " + flashlight.getBatteryLevel().toFixed(1) + "%");
}
};
// Game input handling
game.down = function (x, y, obj) {
// Handled by UI buttons
};
game.up = function (x, y, obj) {
// Handled by UI buttons
};
// Play ambient sounds
LK.getSound('ambience').play();
create night guard with freddy fazbear logo. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
create night guard. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
create a clock. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
create a door with window. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
create a red batery. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
create a hallway. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
craete a flashlight light. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
create a brown floor. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
create camera. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
create a cameraView. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows