User prompt
Please fix the bug: 'TypeError: Cannot read properties of undefined (reading 'rotation')' in or related to this line: 'cockpitDisplay.cockpitBase.rotation += -current3DSpaceAngles.deltaH - cockpitDisplay.cockpitBase.rotation >= 0.02 ? 0.1 : 0;' Line Number: 371
Code edit (2 edits merged)
Please save this source code
User prompt
apply the exponential shrink to width and height
User prompt
ok but invert it : fast at start, slow at end (doppler effect)
User prompt
try exponential instead of log
User prompt
add min and max to speed (4 and 20)
User prompt
try a logarithmic approch to speed decrease
Code edit (2 edits merged)
Please save this source code
User prompt
still too slow
User prompt
speed is too slow at the begining
Code edit (2 edits merged)
Please save this source code
User prompt
in ` self.distanceToCenter = Math.pow(Math.abs(self.x - 1024), 2); var shrinkRatio = 0.1 + Math.pow(self.distanceToCenter, 1.2) * 5 / Math.pow(1024, 1.2); self.width = Math.min(5, self.width * Math.pow(shrinkRatio, 1.1)); self.height = Math.min(100, self.height * Math.pow(shrinkRatio, 1.1)); self.speed = Math.max(4, Math.min(20, self.speed * Math.pow(self.distanceToCenter / 1024, 2) * 1)); ` make speed go progressively from 20 to 4 depending on distanceToCenter
Code edit (6 edits merged)
Please save this source code
User prompt
make all this part more progressive: ``` self.distanceToCenter = Math.abs(self.x - 1024); var shrinkRatio = 0.1 + self.distanceToCenter * 5 / 1024; self.width = Math.min(5, self.width * shrinkRatio); self.height = Math.min(100, self.height * shrinkRatio); self.speed = Math.max(4, Math.min(20, self.speed * Math.pow(self.distanceToCenter / 1024, 3) * 8)); ```
Code edit (1 edits merged)
Please save this source code
User prompt
enhance `self.speed = Math.max(4, Math.min(20, self.speed * Math.pow(self.distanceToCenter / 1024, 2) * 4))` to be more progressive
Code edit (20 edits merged)
Please save this source code
User prompt
enhance `self.speed = Math.max(4, Math.min(20, self.speed * self.distanceToCenter * 4 / 1024));` to be more progressive
Code edit (1 edits merged)
Please save this source code
Code edit (6 edits merged)
Please save this source code
User prompt
bullets are created when user fires so currently they appear above the spaceship and the cockpit. make the appear behind
User prompt
Please fix the bug: 'ReferenceError: distanceToCenter is not defined' in or related to this line: 'var widthReduction = Math.max(0, (1 - distanceToCenter / 1024) * self.width);' Line Number: 35
Code edit (1 edits merged)
Please save this source code
User prompt
Migrate to the latest version of LK
Code edit (16 edits merged)
Please save this source code
/**** * Classes ****/ // Bullet class for the spaceship. var Bullet = Container.expand(function (isLeft, isTop) { var self = Container.call(this); self.isTop = isTop; var bulletGraphics = self.attachAsset('3D_bullet', { anchorX: 0.5, anchorY: 0.5 }); self.isLeft = isLeft; self.speed = 10; self._move_migrated = function () { self.x += self.isLeft ? self.speed : -self.speed; self.y += self.isTop ? self.speed * 0.6 : -self.speed * 0.6; // Calculate distance to center and adjust width accordingly var distanceToCenter = Math.abs(self.x - 1024); var widthReduction = Math.max(0, (1 - distanceToCenter / 1024) * self.width); self.width = Math.max(5, self.width - widthReduction); // Ensure width does not go below 5 }; }); // Cockpit representation class for the spaceship var CockpitDisplay = Container.expand(function () { var self = Container.call(this); // Create a simple 2D representation of the ship's cockpit self.cockpitBase = self.attachAsset('flywheel', { anchorX: 0.5, anchorY: 0.5, // Anchor at the bottom center to simulate cockpit view scaleX: 1, // Scale down to fit within the screen appropriately scaleY: 1.5, y: 2732 - 400 // Position near the bottom of the screen }); // Additional cockpit elements can be added here }); // Cube class for creating and managing a 3D cube shape var Cube = Container.expand(function () { var self = Container.call(this); self.lines = []; // Define the vertices of a cube var vertices = [{ x: -1, y: -1, z: -1 }, // 0: left, top, back { x: 1, y: -1, z: -1 }, // 1: right, top, back { x: 1, y: 1, z: -1 }, // 2: right, bottom, back { x: -1, y: 1, z: -1 }, // 3: left, bottom, back { x: -1, y: -1, z: 1 }, // 4: left, top, front { x: 1, y: -1, z: 1 }, // 5: right, top, front { x: 1, y: 1, z: 1 }, // 6: right, bottom, front { x: -1, y: 1, z: 1 } // 7: left, bottom, front ]; // Scale factor for the cube size var scale = 100; // Center position of the cube var center = { x: 1024, y: 1366, z: 0 }; // Function to draw edges between vertices self.drawEdges = function () { // List of vertex indices defining the cube edges var edges = [[0, 1], [1, 2], [2, 3], [3, 0], // back face [4, 5], [5, 6], [6, 7], [7, 4], // front face [0, 4], [1, 5], [2, 6], [3, 7] // side faces ]; edges.forEach(function (edge) { var startVertex = vertices[edge[0]]; var endVertex = vertices[edge[1]]; // Convert 3D coordinates to 2D using simple projection (ignoring z for simplicity) var startX = center.x + startVertex.x * scale; var startY = center.y + startVertex.y * scale; var endX = center.x + endVertex.x * scale; var endY = center.y + endVertex.y * scale; // Draw line between projected vertices var line = drawLine(startX, startY, endX, endY); self.addChild(line); self.lines.push(line); }); }; // Initialize rotation angle self.rotationAngle = 0; // Update method to rotate the cube self.update = function () { self.rotationAngle += 0.01; // Increment the rotation angle // Rotate each line around the center of the cube self.lines.forEach(function (line) { var angleRadians = self.rotationAngle; // Calculate new start and end points for each line after rotation var newStartX = Math.cos(angleRadians) * (line.startX - center.x) - Math.sin(angleRadians) * (line.startY - center.y) + center.x; var newStartY = Math.sin(angleRadians) * (line.startX - center.x) + Math.cos(angleRadians) * (line.startY - center.y) + center.y; var newEndX = Math.cos(angleRadians) * (line.endX - center.x) - Math.sin(angleRadians) * (line.endY - center.y) + center.x; var newEndY = Math.sin(angleRadians) * (line.endX - center.x) + Math.cos(angleRadians) * (line.endY - center.y) + center.y; // Update line position line.x = newStartX; line.y = newStartY; // Update line width and rotation based on new position var distance = Math.sqrt(Math.pow(newEndX - newStartX, 2) + Math.pow(newEndY - newStartY, 2)); line.width = distance; var angle = Math.atan2(newEndY - newStartY, newEndX - newStartX); line.rotation = angle; }); }; self.drawEdges(); }); /***********************************************************************************/ /********************************** FIGHTER CLASS ************************************/ /***********************************************************************************/ var Fighter = Container.expand(function () { var self = Container.call(this); self.buildShape = function () { // Redefine fighterBody for a more complex 3D shape var fighterBody = new Shape([{ x: 0, y: -75 }, // Nose tip { x: 20, y: -50 }, // Right upper wing { x: 100, y: 0 }, // Right wing tip { x: 20, y: 50 }, // Right lower wing { x: 0, y: 25 }, // Tail bottom { x: -20, y: 50 }, // Left lower wing { x: -100, y: 0 }, // Left wing tip { x: -20, y: -50 } // Left upper wing ]); this.addChild(fighterBody); // Add cockpit var fighterCockpit = new Shape([{ x: 0, y: -60 }, { x: 10, y: -45 }, { x: -10, y: -45 }]); this.addChild(fighterCockpit); // Add reactors var leftReactor = new Shape([{ x: -80, y: 10 }, { x: -90, y: 30 }, { x: -70, y: 30 }]); this.addChild(leftReactor); var rightReactor = new Shape([{ x: 80, y: 10 }, { x: 90, y: 30 }, { x: 70, y: 30 }]); this.addChild(rightReactor); // Add cannons var leftCannon = new Shape([{ x: -100, y: 0 }, { x: -110, y: -10 }, { x: -110, y: 10 }]); this.addChild(leftCannon); var rightCannon = new Shape([{ x: 100, y: 0 }, { x: 110, y: -10 }, { x: 110, y: 10 }]); this.addChild(rightCannon); }; self.buildShape(); }); /***********************************************************************************/ /********************************** SHAPE CLASS ************************************/ /***********************************************************************************/ var Shape = Container.expand(function (coordinates) { var self = Container.call(this); self.polygon = drawPolygon(coordinates); // Function to create a polygon from a list of coordinates self.speed = 0.0; // Speed of shape movement self.direction = 0; // Movement direction in radians self.update = function () { // Update position based on speed and direction self.x += Math.cos(self.direction) * self.speed; self.y += Math.sin(self.direction) * self.speed; }; self.attachLines = function () { // Iterate through each line in the polygon and attach it to the shape self.polygon.forEach(function (line) { self.addChild(line); }); }; self.attachLines(); }); // Enemy class removed as per task requirement. // Assets will be automatically generated based on usage in the code. // Player's spaceship class var Spaceship = Container.expand(function () { var self = Container.call(this); var spaceshipGraphics = self.attachAsset('3D_spaceship', { anchorX: 0.5, anchorY: 0.5 }); self.coords = { x: 0, y: 0, z: 0 }; self.speed = 0.01; self.shoot = function () { // Spawn bullets from side canons var leftBullet = new Bullet(true); leftBullet.x = 0; // Adjust for left canon position leftBullet.y = self.y + 512; // Start the bullet just above the spaceship leftBullet.rotation = Math.PI * 0.25; game.addChild(leftBullet); bullets.push(leftBullet); var rightBullet = new Bullet(false); rightBullet.x = game.width; // Adjust for right canon position rightBullet.y = self.y + 512; // Start the bullet just above the spaceship rightBullet.rotation = -Math.PI * 0.25; game.addChild(rightBullet); bullets.push(rightBullet); // Adding two more cannons var extraLeftBullet = new Bullet(true, true); extraLeftBullet.x = leftBullet.x + 100; // Slightly to the right of the left canon extraLeftBullet.y = self.y - 512 - 128; // Start the bullet just above the spaceship extraLeftBullet.rotation = Math.PI * 1.75; game.addChild(extraLeftBullet); bullets.push(extraLeftBullet); var extraRightBullet = new Bullet(false, true); extraRightBullet.x = rightBullet.x - 100; // Slightly to the left of the right canon extraRightBullet.y = self.y - 512 - 128; // Start the bullet just above the spaceship extraRightBullet.rotation = -Math.PI * 1.75; game.addChild(extraRightBullet); bullets.push(extraRightBullet); }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ // Utility function to draw a polygon using drawLine function drawPolygon(coordinates) { var lines = []; for (var i = 0; i < coordinates.length; i++) { var startPoint = coordinates[i]; var endPoint = coordinates[(i + 1) % coordinates.length]; // Loop back to the first point var line = drawLine(startPoint.x, startPoint.y, endPoint.x, endPoint.y); lines.push(line); } return lines; } // Utility function to draw lines between two points function drawLine(x1, y1, x2, y2) { var line = LK.getAsset('line', { anchorX: 0.0, anchorY: 0.0, x: x1, y: y1 }); line.startX = x1; line.startY = y1; line.endY = x2; line.endY = y2; // Calculate the distance between the two points var distance = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2)); // Set the width of the line to the distance between the points line.width = distance; // Calculate the angle between the two points var angle = Math.atan2(y2 - y1, x2 - x1); // Correct angle calculation for all quadrants line.rotation = angle; return line; } // Debug text to display current 3D space angles var debugText = new Text2('Angles: 0, 0', { size: 50, fill: "#ffffff" }); debugText.x = 10; // Position at top left debugText.y = 10; LK.gui.topLeft.addChild(debugText); // Global object to hold the current horizontal and vertical angle in the 3D space var current3DSpaceAngles = { horizontalAngle: 0, verticalAngle: 0, deltaH: 0, deltaV: 0 }; // Removed drag movement system // Function to animate the star field for a dynamic background effect function starFieldImageAnimation() { // TODO : Move starField background to go with current movement } function starFieldAnimation() { stars.forEach(function (star) { // Calculate direction vector from center to star var directionX = star.x - 1024; // 1024 is half the width of the screen var directionY = star.y - 1366; // 1366 is half the height of the screen // Normalize direction var length = Math.sqrt(directionX * directionX + directionY * directionY); if (length === 0) { // Prevent division by zero length = 1; } directionX /= length; directionY /= length; // Add offset based on current3DSpaceAngles directionX += current3DSpaceAngles.deltaH * 10; //.horizontalAngle; directionY += current3DSpaceAngles.deltaV * 10; //.verticalAngle; // Move star away from center // Increase star size as it moves away to simulate faster movement var sizeIncrease = Math.min(Math.abs(directionX * 2), Math.abs(directionY * 2)); star.width = Math.min(20, star.width + sizeIncrease * 0.05); // Limit width to 20 star.height = star.width; star.x += directionX * starsSpeed; // Increase speed to 10 for faster star movement star.y += directionY * starsSpeed; // Reset star position if it moves off screen if (star.x < 0 || star.x > 2048 || star.y < 0 || star.y > 2732) { star.x = Math.random() * 2048; star.y = Math.random() * 2732; star.width = Math.random() * 10; // Limit width to 20 star.height = star.width; // Reset height to initial value } }); starField.x -= 0.1; starField.y -= 0.1; } // Add starField as an independent asset var starField = LK.getAsset('starField', { anchorX: 0.5, anchorY: 0.5, scaleX: 0.25, scaleY: 0.25, x: 0.5 * 2048 / 2, y: 0.5 * 2732 / 2, alpha: 0.5 }); var starField2 = LK.getAsset('starField2', { anchorX: 0.5, anchorY: 0.5, scaleX: 0.5, scaleY: 0.5, x: 2048 / 2, y: 2732 / 2, alpha: 0.5 }); game.addChild(starField); game.addChild(starField2); starField.visible = true; // Initially hide the second starField starField2.visible = false; // Initially hide the second starField //starField2.width = 2048; // Reset width to initial value //starField2.height = 2732; // Reset height to initial value var nbStars = 80; var starsSpeed = 10; var stars = []; for (var i = 0; i < nbStars; i++) { var star = LK.getAsset('star', { anchorX: 0.5, anchorY: 0.5, alpha: 0.5, x: Math.random() * 2048, // Random position across the screen width y: Math.random() * 2732 // Random position across the screen height }); stars.push(star); game.addChild(star); } // Fixed for (var i = 0; i < nbStars * 2; i++) { var star = LK.getAsset('star', { anchorX: 0.5, anchorY: 0.5, alpha: Math.random(), x: Math.random() * 2048, // Random position across the screen width y: Math.random() * 2732 // Random position across the screen height }); game.addChild(star); } // White color for laser-like bullets // Light Slate Gray for the spaceship var spaceship = game.addChild(new Spaceship()); spaceship.x = 2048 / 2; // Center spaceship horizontally spaceship.y = 2732 - spaceship.height / 2; // Position spaceship near the bottom of the screen // Add the cockpit display to the game and rotate with ship X rotation var cockpitDisplay = game.addChild(new CockpitDisplay()); cockpitDisplay.x = 2048 / 2; // Center cockpit display horizontally var bullets = []; // Player's bullets var enemies = []; var rotationSpeed = 0.25; var shape1; // CONTINUER ICI : MAKE BLOCKS & CODE SEPARATORS // CONTINUER ICI : MAKE BLOCKS & CODE SEPARATORS // CONTINUER ICI : MAKE BLOCKS & CODE SEPARATORS // CONTINUER ICI : MAKE BLOCKS & CODE SEPARATORS // CONTINUER ICI : MAKE BLOCKS & CODE SEPARATORS // Touch event to move and shoot game.on('down', function (x, y, obj) { var touchPosition = game.toLocal(obj.global); console.log("touchPosition", touchPosition); if (touchPosition.x < game.width * 0.33) { // Pressing on the left side of the screen current3DSpaceAngles.deltaH += rotationSpeed; // Adjust horizontal angle to the left } else if (touchPosition.x > game.width * 0.66) { // Pressing on the right side of the screen current3DSpaceAngles.deltaH -= rotationSpeed; // Adjust horizontal angle to the right } else { console.log("centered"); current3DSpaceAngles.deltaH = 0; } if (touchPosition.y < game.height * 0.33) { // Pressing on the upper half of the screen current3DSpaceAngles.deltaV += rotationSpeed; // Adjust vertical angle upwards } else if (touchPosition.y > game.height * 0.66) { // Pressing on the lower half of the screen current3DSpaceAngles.deltaV -= rotationSpeed; // Adjust vertical angle downwards } else { current3DSpaceAngles.deltaV = 0; } if (!current3DSpaceAngles.deltaH && !current3DSpaceAngles.deltaV) { spaceship.shoot(); } }); // Stop movement when user releases touch game.on('up', function () { current3DSpaceAngles.deltaH = 0; current3DSpaceAngles.deltaV = 0; }); // Enemy spawning logic removed as per task requirement. // Game tick LK.on('tick', function () { shape1.update(); starFieldAnimation(); // Call the animation function within the game tick // CONTINUER ICI : MOVE IN FUNCTIONS // CONTINUER ICI : MOVE IN FUNCTIONS // CONTINUER ICI : MOVE IN FUNCTIONS // CONTINUER ICI : MOVE IN FUNCTIONS // Update spaceship coordinates and angles based on current3DSpaceAngles spaceship.coords.x += Math.sin(current3DSpaceAngles.horizontalAngle) * spaceship.speed; // CONTINUER ICI : FIX angleH = 180 MAIS Y NE PASSE PAS EN NEGATIF !!! // CONTINUER ICI : FIX angleH = 180 MAIS Y NE PASSE PAS EN NEGATIF !!! // CONTINUER ICI : FIX angleH = 180 MAIS Y NE PASSE PAS EN NEGATIF !!! // CONTINUER ICI : FIX angleH = 180 MAIS Y NE PASSE PAS EN NEGATIF !!! spaceship.coords.y += Math.cos(current3DSpaceAngles.verticalAngle) * spaceship.speed; spaceship.coords.z += Math.sin(current3DSpaceAngles.verticalAngle) * Math.cos(current3DSpaceAngles.horizontalAngle) * spaceship.speed; // Move bullets for (var i = bullets.length - 1; i >= 0; i--) { bullets[i]._move_migrated(); // Check if bullet reaches the center of the screen and destroy it //var distanceToCenter = Math.max(0, Math.min(1024, bullets[i].x - 1024)); var distanceToCenter = bullets[i].isLeft ? 1024 - bullets[i].x : bullets[i].x - 1024; if (distanceToCenter <= 10) { // Assuming a small threshold around the center for destruction bullets[i].destroy(); bullets.splice(i, 1); } } // Update angles at a lower rate using modulo on ticks if (LK.ticks % 15 == 0) { current3DSpaceAngles.horizontalAngle += current3DSpaceAngles.deltaH; current3DSpaceAngles.verticalAngle += current3DSpaceAngles.deltaV; } // Move bullets for (var i = bullets.length - 1; i >= 0; i--) { bullets[i]._move_migrated(); if (bullets[i].y < 0) { bullets[i].destroy(); bullets.splice(i, 1); } } // Rotate cockpitDisplay based on current3DSpaceAngles.horizontalAngle cockpitDisplay.cockpitBase.rotation += -current3DSpaceAngles.deltaH - cockpitDisplay.cockpitBase.rotation >= 0.02 ? 0.1 : 0; cockpitDisplay.cockpitBase.rotation += -current3DSpaceAngles.deltaH - cockpitDisplay.cockpitBase.rotation <= -0.02 ? -0.1 : 0; cockpitDisplay.cockpitBase.rotation = Math.min(Math.PI * 0.3, Math.max(-Math.PI * 0.3, cockpitDisplay.cockpitBase.rotation)); cockpitDisplay.cockpitBase.scale.y = 1.5 + current3DSpaceAngles.deltaV * 0.5; // Illustrate vertical rotation by scaling the wheel var tempTxt = 'Coords: X=' + Math.round(spaceship.coords.x) + ', Y=' + Math.round(spaceship.coords.y) + ', Z=' + Math.round(spaceship.coords.z); tempTxt += '\nAngles: H=' + Math.round(current3DSpaceAngles.horizontalAngle * (180 / Math.PI)) + '°, V=' + Math.round(current3DSpaceAngles.verticalAngle * (180 / Math.PI)) + '°'; debugText.setText(tempTxt); }); var gameInitialize = function gameInitialize() { // init shape1 = new Shape([{ x: 100, y: 100 }, { x: 500, y: 100 }, { x: 500, y: 500 }, { x: 100, y: 500 }]); // Add the line to the game and return the line object //game.addChild(shape1); var triangle1 = new Shape([{ x: 150, y: 150 }, { x: 550, y: 150 }, { x: 550, y: 550 }]); // Add the line to the game and return the line object //game.addChild(triangle1); var fighter1 = new Fighter(); fighter1.x = 1024; fighter1.y = 1024; // Add the line to the game and return the line object //game.addChild(fighter1); var cube1 = new Cube(); cube1.x = 100; cube1.y = 100; // Add the line to the game and return the line object game.addChild(cube1); }; gameInitialize();
/****
* Classes
****/
// Bullet class for the spaceship.
var Bullet = Container.expand(function (isLeft, isTop) {
var self = Container.call(this);
self.isTop = isTop;
var bulletGraphics = self.attachAsset('3D_bullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.isLeft = isLeft;
self.speed = 10;
self._move_migrated = function () {
self.x += self.isLeft ? self.speed : -self.speed;
self.y += self.isTop ? self.speed * 0.6 : -self.speed * 0.6;
// Calculate distance to center and adjust width accordingly
var distanceToCenter = Math.abs(self.x - 1024);
var widthReduction = Math.max(0, (1 - distanceToCenter / 1024) * self.width);
self.width = Math.max(5, self.width - widthReduction); // Ensure width does not go below 5
};
});
// Cockpit representation class for the spaceship
var CockpitDisplay = Container.expand(function () {
var self = Container.call(this);
// Create a simple 2D representation of the ship's cockpit
self.cockpitBase = self.attachAsset('flywheel', {
anchorX: 0.5,
anchorY: 0.5,
// Anchor at the bottom center to simulate cockpit view
scaleX: 1,
// Scale down to fit within the screen appropriately
scaleY: 1.5,
y: 2732 - 400 // Position near the bottom of the screen
});
// Additional cockpit elements can be added here
});
// Cube class for creating and managing a 3D cube shape
var Cube = Container.expand(function () {
var self = Container.call(this);
self.lines = [];
// Define the vertices of a cube
var vertices = [{
x: -1,
y: -1,
z: -1
},
// 0: left, top, back
{
x: 1,
y: -1,
z: -1
},
// 1: right, top, back
{
x: 1,
y: 1,
z: -1
},
// 2: right, bottom, back
{
x: -1,
y: 1,
z: -1
},
// 3: left, bottom, back
{
x: -1,
y: -1,
z: 1
},
// 4: left, top, front
{
x: 1,
y: -1,
z: 1
},
// 5: right, top, front
{
x: 1,
y: 1,
z: 1
},
// 6: right, bottom, front
{
x: -1,
y: 1,
z: 1
} // 7: left, bottom, front
];
// Scale factor for the cube size
var scale = 100;
// Center position of the cube
var center = {
x: 1024,
y: 1366,
z: 0
};
// Function to draw edges between vertices
self.drawEdges = function () {
// List of vertex indices defining the cube edges
var edges = [[0, 1], [1, 2], [2, 3], [3, 0],
// back face
[4, 5], [5, 6], [6, 7], [7, 4],
// front face
[0, 4], [1, 5], [2, 6], [3, 7] // side faces
];
edges.forEach(function (edge) {
var startVertex = vertices[edge[0]];
var endVertex = vertices[edge[1]];
// Convert 3D coordinates to 2D using simple projection (ignoring z for simplicity)
var startX = center.x + startVertex.x * scale;
var startY = center.y + startVertex.y * scale;
var endX = center.x + endVertex.x * scale;
var endY = center.y + endVertex.y * scale;
// Draw line between projected vertices
var line = drawLine(startX, startY, endX, endY);
self.addChild(line);
self.lines.push(line);
});
};
// Initialize rotation angle
self.rotationAngle = 0;
// Update method to rotate the cube
self.update = function () {
self.rotationAngle += 0.01; // Increment the rotation angle
// Rotate each line around the center of the cube
self.lines.forEach(function (line) {
var angleRadians = self.rotationAngle;
// Calculate new start and end points for each line after rotation
var newStartX = Math.cos(angleRadians) * (line.startX - center.x) - Math.sin(angleRadians) * (line.startY - center.y) + center.x;
var newStartY = Math.sin(angleRadians) * (line.startX - center.x) + Math.cos(angleRadians) * (line.startY - center.y) + center.y;
var newEndX = Math.cos(angleRadians) * (line.endX - center.x) - Math.sin(angleRadians) * (line.endY - center.y) + center.x;
var newEndY = Math.sin(angleRadians) * (line.endX - center.x) + Math.cos(angleRadians) * (line.endY - center.y) + center.y;
// Update line position
line.x = newStartX;
line.y = newStartY;
// Update line width and rotation based on new position
var distance = Math.sqrt(Math.pow(newEndX - newStartX, 2) + Math.pow(newEndY - newStartY, 2));
line.width = distance;
var angle = Math.atan2(newEndY - newStartY, newEndX - newStartX);
line.rotation = angle;
});
};
self.drawEdges();
});
/***********************************************************************************/
/********************************** FIGHTER CLASS ************************************/
/***********************************************************************************/
var Fighter = Container.expand(function () {
var self = Container.call(this);
self.buildShape = function () {
// Redefine fighterBody for a more complex 3D shape
var fighterBody = new Shape([{
x: 0,
y: -75
},
// Nose tip
{
x: 20,
y: -50
},
// Right upper wing
{
x: 100,
y: 0
},
// Right wing tip
{
x: 20,
y: 50
},
// Right lower wing
{
x: 0,
y: 25
},
// Tail bottom
{
x: -20,
y: 50
},
// Left lower wing
{
x: -100,
y: 0
},
// Left wing tip
{
x: -20,
y: -50
} // Left upper wing
]);
this.addChild(fighterBody);
// Add cockpit
var fighterCockpit = new Shape([{
x: 0,
y: -60
}, {
x: 10,
y: -45
}, {
x: -10,
y: -45
}]);
this.addChild(fighterCockpit);
// Add reactors
var leftReactor = new Shape([{
x: -80,
y: 10
}, {
x: -90,
y: 30
}, {
x: -70,
y: 30
}]);
this.addChild(leftReactor);
var rightReactor = new Shape([{
x: 80,
y: 10
}, {
x: 90,
y: 30
}, {
x: 70,
y: 30
}]);
this.addChild(rightReactor);
// Add cannons
var leftCannon = new Shape([{
x: -100,
y: 0
}, {
x: -110,
y: -10
}, {
x: -110,
y: 10
}]);
this.addChild(leftCannon);
var rightCannon = new Shape([{
x: 100,
y: 0
}, {
x: 110,
y: -10
}, {
x: 110,
y: 10
}]);
this.addChild(rightCannon);
};
self.buildShape();
});
/***********************************************************************************/
/********************************** SHAPE CLASS ************************************/
/***********************************************************************************/
var Shape = Container.expand(function (coordinates) {
var self = Container.call(this);
self.polygon = drawPolygon(coordinates); // Function to create a polygon from a list of coordinates
self.speed = 0.0; // Speed of shape movement
self.direction = 0; // Movement direction in radians
self.update = function () {
// Update position based on speed and direction
self.x += Math.cos(self.direction) * self.speed;
self.y += Math.sin(self.direction) * self.speed;
};
self.attachLines = function () {
// Iterate through each line in the polygon and attach it to the shape
self.polygon.forEach(function (line) {
self.addChild(line);
});
};
self.attachLines();
});
// Enemy class removed as per task requirement.
// Assets will be automatically generated based on usage in the code.
// Player's spaceship class
var Spaceship = Container.expand(function () {
var self = Container.call(this);
var spaceshipGraphics = self.attachAsset('3D_spaceship', {
anchorX: 0.5,
anchorY: 0.5
});
self.coords = {
x: 0,
y: 0,
z: 0
};
self.speed = 0.01;
self.shoot = function () {
// Spawn bullets from side canons
var leftBullet = new Bullet(true);
leftBullet.x = 0; // Adjust for left canon position
leftBullet.y = self.y + 512; // Start the bullet just above the spaceship
leftBullet.rotation = Math.PI * 0.25;
game.addChild(leftBullet);
bullets.push(leftBullet);
var rightBullet = new Bullet(false);
rightBullet.x = game.width; // Adjust for right canon position
rightBullet.y = self.y + 512; // Start the bullet just above the spaceship
rightBullet.rotation = -Math.PI * 0.25;
game.addChild(rightBullet);
bullets.push(rightBullet);
// Adding two more cannons
var extraLeftBullet = new Bullet(true, true);
extraLeftBullet.x = leftBullet.x + 100; // Slightly to the right of the left canon
extraLeftBullet.y = self.y - 512 - 128; // Start the bullet just above the spaceship
extraLeftBullet.rotation = Math.PI * 1.75;
game.addChild(extraLeftBullet);
bullets.push(extraLeftBullet);
var extraRightBullet = new Bullet(false, true);
extraRightBullet.x = rightBullet.x - 100; // Slightly to the left of the right canon
extraRightBullet.y = self.y - 512 - 128; // Start the bullet just above the spaceship
extraRightBullet.rotation = -Math.PI * 1.75;
game.addChild(extraRightBullet);
bullets.push(extraRightBullet);
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Utility function to draw a polygon using drawLine
function drawPolygon(coordinates) {
var lines = [];
for (var i = 0; i < coordinates.length; i++) {
var startPoint = coordinates[i];
var endPoint = coordinates[(i + 1) % coordinates.length]; // Loop back to the first point
var line = drawLine(startPoint.x, startPoint.y, endPoint.x, endPoint.y);
lines.push(line);
}
return lines;
}
// Utility function to draw lines between two points
function drawLine(x1, y1, x2, y2) {
var line = LK.getAsset('line', {
anchorX: 0.0,
anchorY: 0.0,
x: x1,
y: y1
});
line.startX = x1;
line.startY = y1;
line.endY = x2;
line.endY = y2;
// Calculate the distance between the two points
var distance = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
// Set the width of the line to the distance between the points
line.width = distance;
// Calculate the angle between the two points
var angle = Math.atan2(y2 - y1, x2 - x1);
// Correct angle calculation for all quadrants
line.rotation = angle;
return line;
}
// Debug text to display current 3D space angles
var debugText = new Text2('Angles: 0, 0', {
size: 50,
fill: "#ffffff"
});
debugText.x = 10; // Position at top left
debugText.y = 10;
LK.gui.topLeft.addChild(debugText);
// Global object to hold the current horizontal and vertical angle in the 3D space
var current3DSpaceAngles = {
horizontalAngle: 0,
verticalAngle: 0,
deltaH: 0,
deltaV: 0
};
// Removed drag movement system
// Function to animate the star field for a dynamic background effect
function starFieldImageAnimation() {
// TODO : Move starField background to go with current movement
}
function starFieldAnimation() {
stars.forEach(function (star) {
// Calculate direction vector from center to star
var directionX = star.x - 1024; // 1024 is half the width of the screen
var directionY = star.y - 1366; // 1366 is half the height of the screen
// Normalize direction
var length = Math.sqrt(directionX * directionX + directionY * directionY);
if (length === 0) {
// Prevent division by zero
length = 1;
}
directionX /= length;
directionY /= length;
// Add offset based on current3DSpaceAngles
directionX += current3DSpaceAngles.deltaH * 10; //.horizontalAngle;
directionY += current3DSpaceAngles.deltaV * 10; //.verticalAngle;
// Move star away from center
// Increase star size as it moves away to simulate faster movement
var sizeIncrease = Math.min(Math.abs(directionX * 2), Math.abs(directionY * 2));
star.width = Math.min(20, star.width + sizeIncrease * 0.05); // Limit width to 20
star.height = star.width;
star.x += directionX * starsSpeed; // Increase speed to 10 for faster star movement
star.y += directionY * starsSpeed;
// Reset star position if it moves off screen
if (star.x < 0 || star.x > 2048 || star.y < 0 || star.y > 2732) {
star.x = Math.random() * 2048;
star.y = Math.random() * 2732;
star.width = Math.random() * 10; // Limit width to 20
star.height = star.width; // Reset height to initial value
}
});
starField.x -= 0.1;
starField.y -= 0.1;
}
// Add starField as an independent asset
var starField = LK.getAsset('starField', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 0.25,
scaleY: 0.25,
x: 0.5 * 2048 / 2,
y: 0.5 * 2732 / 2,
alpha: 0.5
});
var starField2 = LK.getAsset('starField2', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 0.5,
scaleY: 0.5,
x: 2048 / 2,
y: 2732 / 2,
alpha: 0.5
});
game.addChild(starField);
game.addChild(starField2);
starField.visible = true; // Initially hide the second starField
starField2.visible = false; // Initially hide the second starField
//starField2.width = 2048; // Reset width to initial value
//starField2.height = 2732; // Reset height to initial value
var nbStars = 80;
var starsSpeed = 10;
var stars = [];
for (var i = 0; i < nbStars; i++) {
var star = LK.getAsset('star', {
anchorX: 0.5,
anchorY: 0.5,
alpha: 0.5,
x: Math.random() * 2048,
// Random position across the screen width
y: Math.random() * 2732 // Random position across the screen height
});
stars.push(star);
game.addChild(star);
}
// Fixed
for (var i = 0; i < nbStars * 2; i++) {
var star = LK.getAsset('star', {
anchorX: 0.5,
anchorY: 0.5,
alpha: Math.random(),
x: Math.random() * 2048,
// Random position across the screen width
y: Math.random() * 2732 // Random position across the screen height
});
game.addChild(star);
}
// White color for laser-like bullets
// Light Slate Gray for the spaceship
var spaceship = game.addChild(new Spaceship());
spaceship.x = 2048 / 2; // Center spaceship horizontally
spaceship.y = 2732 - spaceship.height / 2; // Position spaceship near the bottom of the screen
// Add the cockpit display to the game and rotate with ship X rotation
var cockpitDisplay = game.addChild(new CockpitDisplay());
cockpitDisplay.x = 2048 / 2; // Center cockpit display horizontally
var bullets = []; // Player's bullets
var enemies = [];
var rotationSpeed = 0.25;
var shape1;
// CONTINUER ICI : MAKE BLOCKS & CODE SEPARATORS
// CONTINUER ICI : MAKE BLOCKS & CODE SEPARATORS
// CONTINUER ICI : MAKE BLOCKS & CODE SEPARATORS
// CONTINUER ICI : MAKE BLOCKS & CODE SEPARATORS
// CONTINUER ICI : MAKE BLOCKS & CODE SEPARATORS
// Touch event to move and shoot
game.on('down', function (x, y, obj) {
var touchPosition = game.toLocal(obj.global);
console.log("touchPosition", touchPosition);
if (touchPosition.x < game.width * 0.33) {
// Pressing on the left side of the screen
current3DSpaceAngles.deltaH += rotationSpeed; // Adjust horizontal angle to the left
} else if (touchPosition.x > game.width * 0.66) {
// Pressing on the right side of the screen
current3DSpaceAngles.deltaH -= rotationSpeed; // Adjust horizontal angle to the right
} else {
console.log("centered");
current3DSpaceAngles.deltaH = 0;
}
if (touchPosition.y < game.height * 0.33) {
// Pressing on the upper half of the screen
current3DSpaceAngles.deltaV += rotationSpeed; // Adjust vertical angle upwards
} else if (touchPosition.y > game.height * 0.66) {
// Pressing on the lower half of the screen
current3DSpaceAngles.deltaV -= rotationSpeed; // Adjust vertical angle downwards
} else {
current3DSpaceAngles.deltaV = 0;
}
if (!current3DSpaceAngles.deltaH && !current3DSpaceAngles.deltaV) {
spaceship.shoot();
}
});
// Stop movement when user releases touch
game.on('up', function () {
current3DSpaceAngles.deltaH = 0;
current3DSpaceAngles.deltaV = 0;
});
// Enemy spawning logic removed as per task requirement.
// Game tick
LK.on('tick', function () {
shape1.update();
starFieldAnimation(); // Call the animation function within the game tick
// CONTINUER ICI : MOVE IN FUNCTIONS
// CONTINUER ICI : MOVE IN FUNCTIONS
// CONTINUER ICI : MOVE IN FUNCTIONS
// CONTINUER ICI : MOVE IN FUNCTIONS
// Update spaceship coordinates and angles based on current3DSpaceAngles
spaceship.coords.x += Math.sin(current3DSpaceAngles.horizontalAngle) * spaceship.speed;
// CONTINUER ICI : FIX angleH = 180 MAIS Y NE PASSE PAS EN NEGATIF !!!
// CONTINUER ICI : FIX angleH = 180 MAIS Y NE PASSE PAS EN NEGATIF !!!
// CONTINUER ICI : FIX angleH = 180 MAIS Y NE PASSE PAS EN NEGATIF !!!
// CONTINUER ICI : FIX angleH = 180 MAIS Y NE PASSE PAS EN NEGATIF !!!
spaceship.coords.y += Math.cos(current3DSpaceAngles.verticalAngle) * spaceship.speed;
spaceship.coords.z += Math.sin(current3DSpaceAngles.verticalAngle) * Math.cos(current3DSpaceAngles.horizontalAngle) * spaceship.speed;
// Move bullets
for (var i = bullets.length - 1; i >= 0; i--) {
bullets[i]._move_migrated();
// Check if bullet reaches the center of the screen and destroy it
//var distanceToCenter = Math.max(0, Math.min(1024, bullets[i].x - 1024));
var distanceToCenter = bullets[i].isLeft ? 1024 - bullets[i].x : bullets[i].x - 1024;
if (distanceToCenter <= 10) {
// Assuming a small threshold around the center for destruction
bullets[i].destroy();
bullets.splice(i, 1);
}
}
// Update angles at a lower rate using modulo on ticks
if (LK.ticks % 15 == 0) {
current3DSpaceAngles.horizontalAngle += current3DSpaceAngles.deltaH;
current3DSpaceAngles.verticalAngle += current3DSpaceAngles.deltaV;
}
// Move bullets
for (var i = bullets.length - 1; i >= 0; i--) {
bullets[i]._move_migrated();
if (bullets[i].y < 0) {
bullets[i].destroy();
bullets.splice(i, 1);
}
}
// Rotate cockpitDisplay based on current3DSpaceAngles.horizontalAngle
cockpitDisplay.cockpitBase.rotation += -current3DSpaceAngles.deltaH - cockpitDisplay.cockpitBase.rotation >= 0.02 ? 0.1 : 0;
cockpitDisplay.cockpitBase.rotation += -current3DSpaceAngles.deltaH - cockpitDisplay.cockpitBase.rotation <= -0.02 ? -0.1 : 0;
cockpitDisplay.cockpitBase.rotation = Math.min(Math.PI * 0.3, Math.max(-Math.PI * 0.3, cockpitDisplay.cockpitBase.rotation));
cockpitDisplay.cockpitBase.scale.y = 1.5 + current3DSpaceAngles.deltaV * 0.5; // Illustrate vertical rotation by scaling the wheel
var tempTxt = 'Coords: X=' + Math.round(spaceship.coords.x) + ', Y=' + Math.round(spaceship.coords.y) + ', Z=' + Math.round(spaceship.coords.z);
tempTxt += '\nAngles: H=' + Math.round(current3DSpaceAngles.horizontalAngle * (180 / Math.PI)) + '°, V=' + Math.round(current3DSpaceAngles.verticalAngle * (180 / Math.PI)) + '°';
debugText.setText(tempTxt);
});
var gameInitialize = function gameInitialize() {
// init
shape1 = new Shape([{
x: 100,
y: 100
}, {
x: 500,
y: 100
}, {
x: 500,
y: 500
}, {
x: 100,
y: 500
}]);
// Add the line to the game and return the line object
//game.addChild(shape1);
var triangle1 = new Shape([{
x: 150,
y: 150
}, {
x: 550,
y: 150
}, {
x: 550,
y: 550
}]);
// Add the line to the game and return the line object
//game.addChild(triangle1);
var fighter1 = new Fighter();
fighter1.x = 1024;
fighter1.y = 1024;
// Add the line to the game and return the line object
//game.addChild(fighter1);
var cube1 = new Cube();
cube1.x = 100;
cube1.y = 100;
// Add the line to the game and return the line object
game.addChild(cube1);
};
gameInitialize();
starfield.
remove
elongated futuristic laser canon gun green. top view
explosion from top. zenith view
white triangle.
black background ethereal blue gas.
black background ethereal centered galaxy.
black background ethereal centered galaxy.
black background ethereal centered planet.
close up of a giant red star. black background
planet with rings. black background. full, with margin.
metalic oval border with bevel. Black. Electronic style. empty inside. no background
futuristic space fighter.. full front view
Space scene with full earth (europe and africa side). High definition
elegant white rose in a long transparent futuristic glass tube.
laserShot
Sound effect
bgMusic
Sound effect
explosion
Sound effect
laserShot2
Sound effect
detectionBeep1
Sound effect
fighterPassing
Sound effect
targetFoundBeep
Sound effect
damage
Sound effect
warning
Sound effect
startSound
Sound effect
acceleration
Sound effect
teamKill
Sound effect
finalExplosion
Sound effect