Code edit (1 edits merged)
Please save this source code
Code edit (1 edits merged)
Please save this source code
User prompt
take out the minigame of slicing the tuna, just leave the original
Code edit (1 edits merged)
Please save this source code
User prompt
i want make the move in the belt faster and i want to add a feature that when you clic on 5 defective products you must stop production and pass to a mini game when you should slice the products ↪💡 Consider importing and using the following plugins: @upit/tween.v1
Code edit (1 edits merged)
Please save this source code
User prompt
The Quality Inspector
Initial prompt
i want to make a game of a inspector in a Fabric of tuna cans, watching out a transporting band, taking out malproductions clicing on them and stoping the production when the coute is completed or when three in arow malproduction appears, getting points whenever you stop the production correctly avoiding making products in excess or when the failed products appear, the tittle "the quality inspector"
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var Character = Container.expand(function () {
var self = Container.call(this);
// Crear todos los estados desde el inicio, pero invisibles
var states = {
normal: self.attachAsset('charNormal', {
anchorX: 0.5,
anchorY: 1
}),
alert: self.attachAsset('charAlert', {
anchorX: 0.5,
anchorY: 1
}),
worried: self.attachAsset('charWorried', {
anchorX: 0.5,
anchorY: 1
}),
happy: self.attachAsset('charHappy', {
anchorX: 0.5,
anchorY: 1
})
};
// Inicialmente mostrar solo el estado normal
for (var key in states) {
states[key].visible = false;
}
states.normal.visible = true;
self.currentState = 'normal';
self.setState = function (state) {
if (state === self.currentState) {
return;
} // evita cambio innecesario
if (states[self.currentState]) {
states[self.currentState].visible = false;
}
if (states[state]) {
states[state].visible = true;
self.currentState = state;
}
};
return self;
});
var StopButton = Container.expand(function () {
var self = Container.call(this);
var buttonGraphics = self.attachAsset('stopButton', {
anchorX: 0.5,
anchorY: 0.5
});
self.down = function (x, y, obj) {
stopProduction();
};
return self;
});
var TunaCan = Container.expand(function (isDefective) {
var self = Container.call(this);
self.isDefective = isDefective || false;
self.isClicked = false;
self.speed = 10;
// Asignar sprite según tipo de lata
self.sprite = self.attachAsset(self.isDefective ? 'defectCan' : 'goodCan', {
anchorX: 0.5,
anchorY: 0.5
});
if (self.isDefective) {
self.sprite.alpha = 0.8;
}
self.update = function () {
self.x += self.speed;
};
self.down = function (x, y, obj) {
if (!self.isClicked) {
self.isClicked = true;
LK.getSound('click').play();
self.visible = false;
if (self.isDefective) {
defectsRemoved++;
updateDefectStreak();
updateDefectsRemovedUI();
if (onlyDefectiveMode) {
// Penaliza 1 punto por eliminar lata defectuosa en modo solo defectuosas
LK.setScore(LK.getScore() - 1);
} else {
// Puntaje normal por eliminar defectuosa
LK.setScore(LK.getScore() + 1);
}
} else {
// Penaliza 1 punto por clic en lata buena
LK.setScore(LK.getScore() - 1);
}
updateScoreUI();
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x34495e
});
/****
* Game Code
****/
// Agregar background al juego
var background = game.addChild(LK.getAsset('background', {
anchorX: 0.5,
anchorY: 0.5
}));
background.x = 1024; // centro del canvas
background.y = 768; // centro del canvas (ajustar según resolución)
// Game variables
var cans = [];
var currentQuota = 10;
var goodCansProduced = 0;
var defectsRemoved = 0;
var consecutiveDefects = 0;
var gameSpeed = 1;
var spawnTimer = 0;
var spawnRate = 60; // frames between spawns
var isProductionRunning = true;
var level = 1;
var totalDefectiveCount = 0; // cuenta todas las latas defectuosas generadas
var forceDefectiveMode = false; // si es true, solo se generan latas defectuosas
var onlyDefectiveMode = false; // NUEVA variable: indica si está activo el modo solo defectuosas
var defectiveCount = 0; // para contar cuántas latas defectuosas consecutivas aparecieron
/****
* UI Elements
****/
var quotaText = new Text2('Quota: 0/0', {
size: 60,
fill: 0xFFFFFF
});
quotaText.anchor.set(0.5, 0);
LK.gui.top.addChild(quotaText);
var levelText = new Text2('Level: 1', {
size: 50,
fill: 0xFFFFFF
});
levelText.anchor.set(0, 0);
levelText.x = 150;
levelText.y = 50;
LK.gui.topLeft.addChild(levelText);
var defectStreakText = new Text2('Consecutive Defects: 0', {
size: 40,
fill: 0xFF6B35
});
defectStreakText.anchor.set(0.5, 0);
defectStreakText.y = 80;
LK.gui.top.addChild(defectStreakText);
// NUEVO: contador de latas defectuosas eliminadas
var defectsRemovedText = new Text2('Defects removed: 0', {
size: 40,
fill: 0xFF6B35
});
defectsRemovedText.anchor.set(0.5, 0);
defectsRemovedText.y = 120; // debajo de defectStreakText
LK.gui.top.addChild(defectsRemovedText);
/****
* Funciones de actualización de UI
****/
function updateUI() {
quotaText.setText('Quota: ' + goodCansProduced + '/' + currentQuota);
levelText.setText('Level: ' + level);
defectStreakText.setText('Consecutive Defects: ' + consecutiveDefects);
}
// NUEVA: actualiza el contador de latas defectuosas eliminadas
function updateDefectsRemovedUI() {
defectsRemovedText.setText('Defects removed: ' + defectsRemoved);
}
// Contador de puntaje en pantalla
var scoreText = new Text2('Score: 0', {
size: 50,
fill: 0xFFFF00
});
scoreText.anchor.set(0.5, 0);
scoreText.x = 1024;
scoreText.y = 50;
LK.gui.top.addChild(scoreText);
// Función para actualizar puntaje en pantalla
function updateScoreUI() {
scoreText.setText('Score: ' + LK.getScore());
}
// Conveyor belt
var conveyorBelt = game.addChild(LK.getAsset('conveyorBelt', {
anchorX: 0.5,
anchorY: 0.5
}));
conveyorBelt.x = 1024;
conveyorBelt.y = 1400;
// Stop button
var stopButton = game.addChild(new StopButton());
stopButton.x = 1024;
stopButton.y = 1600;
// Stop button label
var stopButtonText = new Text2('STOP PRODUCTION', {
size: 40,
fill: 0xFFFFFF
});
stopButtonText.anchor.set(0.5, 0.5);
stopButtonText.x = stopButton.x;
stopButtonText.y = stopButton.y;
game.addChild(stopButtonText);
var character = game.addChild(new Character());
character.x = 1000; // ajusta según tu canvas
character.y = 1200;
function spawnCan() {
if (!isProductionRunning) {
return;
}
var isDefective;
// Si está activado el modo solo defectuosas, todas las latas son defectuosas
if (onlyDefectiveMode) {
isDefective = true;
character.setState('worried'); // personaje preocupado en modo solo defectuosas
} else {
isDefective = Math.random() < 0.31; // 30% de probabilidad de defecto
}
// Contamos cuántas defectuosas seguidas salieron
if (isDefective) {
defectiveCount++;
// personaje alerta si aparece una defectuosa
if (!onlyDefectiveMode) {
character.setState('alert');
}
} else {
defectiveCount = 0;
// personaje normal si no hay defectuosa y no está el modo solo defectuosas
if (!onlyDefectiveMode) {
character.setState('normal');
}
}
// Si aparecieron 4 defectuosas seguidas, hay 50% de chance de activar el modo solo defectuosas
if (defectiveCount >= 3 && Math.random() < 0.85) {
onlyDefectiveMode = true;
console.log("🔧 Modo solo defectuosas activado");
character.setState('worried'); // personaje preocupado
}
// Crear la lata
var can = new TunaCan(isDefective);
can.x = -100;
can.y = conveyorBelt.y;
// Velocidad base aumentada + depende del nivel
can.speed = 4 + gameSpeed * 0.6 + level * 0.1;
cans.push(can);
game.addChild(can);
}
function updateDefectStreak() {
consecutiveDefects = 0;
defectStreakText.setText('Consecutive Defects: ' + consecutiveDefects);
}
function checkConsecutiveDefects() {
var recentCans = cans.slice(-3);
if (recentCans.length >= 3) {
var allDefective = true;
for (var i = 0; i < 3; i++) {
if (!recentCans[i].isDefective || recentCans[i].isClicked) {
allDefective = false;
break;
}
}
if (allDefective) {
consecutiveDefects = 3;
defectStreakText.setText('Consecutive Defects: ' + consecutiveDefects);
// Auto-stop production
LK.setTimeout(function () {
if (isProductionRunning) {
stopProduction();
}
}, 100);
}
}
}
function stopProduction() {
if (!isProductionRunning) {
return;
}
isProductionRunning = false;
// Check if quota was met
var quotaMet = goodCansProduced >= currentQuota && goodCansProduced <= currentQuota + 2;
var defectControl = consecutiveDefects >= 3;
// Permite ganar si está activado el modo solo defectuosas
if (quotaMet || defectControl || onlyDefectiveMode) {
// Éxito
LK.getSound('success').play();
var points = quotaMet ? 100 + level * 10 : 50;
if (defectControl) {
points += 25;
}
LK.setScore(LK.getScore() + points);
// Next level: aumento progresivo
level++;
currentQuota += 5;
gameSpeed += 0.3; // aumenta velocidad de las latas
spawnRate = Math.max(25, spawnRate - (2 + level)); // disminuye el tiempo entre spawns
// Flash pantalla verde
LK.effects.flashScreen(0x27ae60, 500);
// Personaje contento
character.setState('happy');
LK.setTimeout(function () {
// Vuelve a normal después de 1.5 s
character.setState('normal');
}, 1500);
// Reset del nivel
LK.setTimeout(function () {
resetLevel();
totalDefectiveCount = 0;
forceDefectiveMode = false;
onlyDefectiveMode = false; // restablece el modo solo defectuosas
}, 1000);
} else {
// Fallo
LK.getSound('fail').play();
LK.effects.flashScreen(0xe74c3c, 1000);
LK.showGameOver();
}
}
function resetLevel() {
// Si el modo solo defectuosas estaba activo, lo desactivamos para el nuevo nivel
if (onlyDefectiveMode) {
console.log(" Modo solo defectuosas desactivado al pasar de nivel");
onlyDefectiveMode = false;
defectiveCount = 0; // Reiniciamos el contador de defectuosas seguidas
}
// 🧩 Limpiamos todas las latas del nivel anterior
for (var i = cans.length - 1; i >= 0; i--) {
cans[i].destroy();
}
cans = [];
// 🔁 Reiniciamos contadores de nivel
goodCansProduced = 0;
defectsRemoved = 0;
consecutiveDefects = 0;
isProductionRunning = true;
// 🖥️ Actualizamos la interfaz
updateUI();
}
// Initialize UI
updateUI();
game.update = function () {
if (!isProductionRunning) {
return;
}
// Spawn cans
spawnTimer++;
if (spawnTimer >= spawnRate) {
spawnTimer = 0;
spawnCan();
}
// Update cans and check for removal
for (var i = cans.length - 1; i >= 0; i--) {
var can = cans[i];
// Remove cans that are off screen
if (can.x > 2148) {
if (!can.isDefective && !can.isClicked) {
goodCansProduced++;
}
can.destroy();
cans.splice(i, 1);
continue;
}
// Check if unclicked defective cans passed through
if (can.x > 2048 && can.isDefective && !can.isClicked) {
checkConsecutiveDefects();
}
}
// Update UI
updateUI();
// Check win condition
if (goodCansProduced >= currentQuota + 5) {
// Over-production penalty
LK.getSound('fail').play();
LK.effects.flashScreen(0xe74c3c, 1000);
LK.showGameOver();
}
}; /****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var Character = Container.expand(function () {
var self = Container.call(this);
// Crear todos los estados desde el inicio, pero invisibles
var states = {
normal: self.attachAsset('charNormal', {
anchorX: 0.5,
anchorY: 1
}),
alert: self.attachAsset('charAlert', {
anchorX: 0.5,
anchorY: 1
}),
worried: self.attachAsset('charWorried', {
anchorX: 0.5,
anchorY: 1
}),
happy: self.attachAsset('charHappy', {
anchorX: 0.5,
anchorY: 1
})
};
// Inicialmente mostrar solo el estado normal
for (var key in states) {
states[key].visible = false;
}
states.normal.visible = true;
self.currentState = 'normal';
self.setState = function (state) {
if (state === self.currentState) {
return;
} // evita cambio innecesario
if (states[self.currentState]) {
states[self.currentState].visible = false;
}
if (states[state]) {
states[state].visible = true;
self.currentState = state;
}
};
return self;
});
var StopButton = Container.expand(function () {
var self = Container.call(this);
var buttonGraphics = self.attachAsset('stopButton', {
anchorX: 0.5,
anchorY: 0.5
});
self.down = function (x, y, obj) {
stopProduction();
};
return self;
});
var TunaCan = Container.expand(function (isDefective) {
var self = Container.call(this);
self.isDefective = isDefective || false;
self.isClicked = false;
self.speed = 10;
// Asignar sprite según tipo de lata
self.sprite = self.attachAsset(self.isDefective ? 'defectCan' : 'goodCan', {
anchorX: 0.5,
anchorY: 0.5
});
if (self.isDefective) {
self.sprite.alpha = 0.8;
}
self.update = function () {
self.x += self.speed;
};
self.down = function (x, y, obj) {
if (!self.isClicked) {
self.isClicked = true;
LK.getSound('click').play();
self.visible = false;
if (self.isDefective) {
defectsRemoved++;
updateDefectStreak();
updateDefectsRemovedUI();
if (onlyDefectiveMode) {
// Penaliza 1 punto por eliminar lata defectuosa en modo solo defectuosas
LK.setScore(LK.getScore() - 1);
} else {
// Puntaje normal por eliminar defectuosa
LK.setScore(LK.getScore() + 1);
}
} else {
// Penaliza 1 punto por clic en lata buena
LK.setScore(LK.getScore() - 1);
}
updateScoreUI();
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x34495e
});
/****
* Game Code
****/
// Agregar background al juego
var background = game.addChild(LK.getAsset('background', {
anchorX: 0.5,
anchorY: 0.5
}));
background.x = 1024; // centro del canvas
background.y = 768; // centro del canvas (ajustar según resolución)
// Game variables
var cans = [];
var currentQuota = 10;
var goodCansProduced = 0;
var defectsRemoved = 0;
var consecutiveDefects = 0;
var gameSpeed = 1;
var spawnTimer = 0;
var spawnRate = 60; // frames between spawns
var isProductionRunning = true;
var level = 1;
var totalDefectiveCount = 0; // cuenta todas las latas defectuosas generadas
var forceDefectiveMode = false; // si es true, solo se generan latas defectuosas
var onlyDefectiveMode = false; // NUEVA variable: indica si está activo el modo solo defectuosas
var defectiveCount = 0; // para contar cuántas latas defectuosas consecutivas aparecieron
/****
* UI Elements
****/
var quotaText = new Text2('Quota: 0/0', {
size: 60,
fill: 0xFFFFFF
});
quotaText.anchor.set(0.5, 0);
LK.gui.top.addChild(quotaText);
var levelText = new Text2('Level: 1', {
size: 50,
fill: 0xFFFFFF
});
levelText.anchor.set(0, 0);
levelText.x = 150;
levelText.y = 50;
LK.gui.topLeft.addChild(levelText);
var defectStreakText = new Text2('Consecutive Defects: 0', {
size: 40,
fill: 0xFF6B35
});
defectStreakText.anchor.set(0.5, 0);
defectStreakText.y = 80;
LK.gui.top.addChild(defectStreakText);
// NUEVO: contador de latas defectuosas eliminadas
var defectsRemovedText = new Text2('Defects removed: 0', {
size: 40,
fill: 0xFF6B35
});
defectsRemovedText.anchor.set(0.5, 0);
defectsRemovedText.y = 120; // debajo de defectStreakText
LK.gui.top.addChild(defectsRemovedText);
/****
* Funciones de actualización de UI
****/
function updateUI() {
quotaText.setText('Quota: ' + goodCansProduced + '/' + currentQuota);
levelText.setText('Level: ' + level);
defectStreakText.setText('Consecutive Defects: ' + consecutiveDefects);
}
// NUEVA: actualiza el contador de latas defectuosas eliminadas
function updateDefectsRemovedUI() {
defectsRemovedText.setText('Defects removed: ' + defectsRemoved);
}
// Contador de puntaje en pantalla
var scoreText = new Text2('Score: 0', {
size: 50,
fill: 0xFFFF00
});
scoreText.anchor.set(0.5, 0);
scoreText.x = 1024;
scoreText.y = 50;
LK.gui.top.addChild(scoreText);
// Función para actualizar puntaje en pantalla
function updateScoreUI() {
scoreText.setText('Score: ' + LK.getScore());
}
// Conveyor belt
var conveyorBelt = game.addChild(LK.getAsset('conveyorBelt', {
anchorX: 0.5,
anchorY: 0.5
}));
conveyorBelt.x = 1024;
conveyorBelt.y = 1400;
// Stop button
var stopButton = game.addChild(new StopButton());
stopButton.x = 1024;
stopButton.y = 1600;
// Stop button label
var stopButtonText = new Text2('STOP PRODUCTION', {
size: 40,
fill: 0xFFFFFF
});
stopButtonText.anchor.set(0.5, 0.5);
stopButtonText.x = stopButton.x;
stopButtonText.y = stopButton.y;
game.addChild(stopButtonText);
var character = game.addChild(new Character());
character.x = 1000; // ajusta según tu canvas
character.y = 1200;
function spawnCan() {
if (!isProductionRunning) {
return;
}
var isDefective;
// Si está activado el modo solo defectuosas, todas las latas son defectuosas
if (onlyDefectiveMode) {
isDefective = true;
character.setState('worried'); // personaje preocupado en modo solo defectuosas
} else {
isDefective = Math.random() < 0.31; // 30% de probabilidad de defecto
}
// Contamos cuántas defectuosas seguidas salieron
if (isDefective) {
defectiveCount++;
// personaje alerta si aparece una defectuosa
if (!onlyDefectiveMode) {
character.setState('alert');
}
} else {
defectiveCount = 0;
// personaje normal si no hay defectuosa y no está el modo solo defectuosas
if (!onlyDefectiveMode) {
character.setState('normal');
}
}
// Si aparecieron 4 defectuosas seguidas, hay 50% de chance de activar el modo solo defectuosas
if (defectiveCount >= 3 && Math.random() < 0.85) {
onlyDefectiveMode = true;
console.log("🔧 Modo solo defectuosas activado");
character.setState('worried'); // personaje preocupado
}
// Crear la lata
var can = new TunaCan(isDefective);
can.x = -100;
can.y = conveyorBelt.y;
// Velocidad base aumentada + depende del nivel
can.speed = 4 + gameSpeed * 0.6 + level * 0.1;
cans.push(can);
game.addChild(can);
}
function updateDefectStreak() {
consecutiveDefects = 0;
defectStreakText.setText('Consecutive Defects: ' + consecutiveDefects);
}
function checkConsecutiveDefects() {
var recentCans = cans.slice(-3);
if (recentCans.length >= 3) {
var allDefective = true;
for (var i = 0; i < 3; i++) {
if (!recentCans[i].isDefective || recentCans[i].isClicked) {
allDefective = false;
break;
}
}
if (allDefective) {
consecutiveDefects = 3;
defectStreakText.setText('Consecutive Defects: ' + consecutiveDefects);
// Auto-stop production
LK.setTimeout(function () {
if (isProductionRunning) {
stopProduction();
}
}, 100);
}
}
}
function stopProduction() {
if (!isProductionRunning) {
return;
}
isProductionRunning = false;
// Check if quota was met
var quotaMet = goodCansProduced >= currentQuota && goodCansProduced <= currentQuota + 2;
var defectControl = consecutiveDefects >= 3;
// Permite ganar si está activado el modo solo defectuosas
if (quotaMet || defectControl || onlyDefectiveMode) {
// Éxito
LK.getSound('success').play();
var points = quotaMet ? 100 + level * 10 : 50;
if (defectControl) {
points += 25;
}
LK.setScore(LK.getScore() + points);
// Next level: aumento progresivo
level++;
currentQuota += 5;
gameSpeed += 0.3; // aumenta velocidad de las latas
spawnRate = Math.max(25, spawnRate - (2 + level)); // disminuye el tiempo entre spawns
// Flash pantalla verde
LK.effects.flashScreen(0x27ae60, 500);
// Personaje contento
character.setState('happy');
LK.setTimeout(function () {
// Vuelve a normal después de 1.5 s
character.setState('normal');
}, 1500);
// Reset del nivel
LK.setTimeout(function () {
resetLevel();
totalDefectiveCount = 0;
forceDefectiveMode = false;
onlyDefectiveMode = false; // restablece el modo solo defectuosas
}, 1000);
} else {
// Fallo
LK.getSound('fail').play();
LK.effects.flashScreen(0xe74c3c, 1000);
LK.showGameOver();
}
}
function resetLevel() {
// Si el modo solo defectuosas estaba activo, lo desactivamos para el nuevo nivel
if (onlyDefectiveMode) {
console.log(" Modo solo defectuosas desactivado al pasar de nivel");
onlyDefectiveMode = false;
defectiveCount = 0; // Reiniciamos el contador de defectuosas seguidas
}
// 🧩 Limpiamos todas las latas del nivel anterior
for (var i = cans.length - 1; i >= 0; i--) {
cans[i].destroy();
}
cans = [];
// 🔁 Reiniciamos contadores de nivel
goodCansProduced = 0;
defectsRemoved = 0;
consecutiveDefects = 0;
isProductionRunning = true;
// 🖥️ Actualizamos la interfaz
updateUI();
}
// Initialize UI
updateUI();
game.update = function () {
if (!isProductionRunning) {
return;
}
// Spawn cans
spawnTimer++;
if (spawnTimer >= spawnRate) {
spawnTimer = 0;
spawnCan();
}
// Update cans and check for removal
for (var i = cans.length - 1; i >= 0; i--) {
var can = cans[i];
// Remove cans that are off screen
if (can.x > 2148) {
if (!can.isDefective && !can.isClicked) {
goodCansProduced++;
}
can.destroy();
cans.splice(i, 1);
continue;
}
// Check if unclicked defective cans passed through
if (can.x > 2048 && can.isDefective && !can.isClicked) {
checkConsecutiveDefects();
}
}
// Update UI
updateUI();
// Check win condition
if (goodCansProduced >= currentQuota + 5) {
// Over-production penalty
LK.getSound('fail').play();
LK.effects.flashScreen(0xe74c3c, 1000);
LK.showGameOver();
}
};