Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Sprig App] RaycastingGame #2643

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
295 changes: 295 additions & 0 deletions games/RaycastingGame.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,295 @@
/*
First time? Check out the tutorial game:
https://sprig.hackclub.com/gallery/getting_started

@title: RaycastingGame
@author: Simone2011
@tags: []
@addedOn: 2024-11-25
*/
const player = "l"
const pen = "p"
const red = "r"

// Custom floor function without using Math
function customFloor(value) {
if (value < 0) {
return value - (value % 1); // For negative numbers, we subtract the fractional part
} else {
return value - (value % 1); // For positive, subtract the fractional part
}
}

// Custom cosine function using the Taylor series expansion
function customCos(angle) {
let result = 1; // Start with 1 (0th term of the series)
let term = 1; // To track the terms in the series
let n = 2; // Start from x^2 / 2!

// Approximate the cosine function using a truncated Taylor series
for (let i = 0; i < 5; i++) { // Limit the number of terms for better performance
term *= -angle * angle / (n * (n - 1)); // Calculate the next term in the series
result += term; // Add the term to the result
n += 2; // Move to the next term in the series
}

return result;
}

// Custom sine function using the Taylor series expansion
function customSin(angle) {
let result = angle; // Start with x (first term of the series)
let term = angle; // To track the terms in the series
let n = 3; // Start from x^3 / 3!

// Approximate the sine function using a truncated Taylor series
for (let i = 0; i < 5; i++) { // Limit the number of terms for better performance
term *= -angle * angle / (n * (n - 1)); // Calculate the next term in the series
result += term; // Add the term to the result
n += 2; // Move to the next term in the series
}

return result;
}

// Set the legends for the player and red sprites
setLegend(
[player, bitmap`
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000`],
[pen, bitmap`
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL`],
[red, bitmap`
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333`]
)

// Set the solids for the player and red sprites
setSolids(
[player, bitmap`
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000`],
[pen, bitmap`
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLL3.3L.LLLL
..LLLLL...LLLLLL
..LLLLL555LLLLLL
.LLLLLLLLLLLLLLL
.LLLLLLLLLLLLLLL
.LLLLLLLLLLLLLLL
.LLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL`],
[red, bitmap`
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333`]
)

// Level map
let level = 0
const levels = [
map`
rrrrrrrrrrrr....................................................................
r........r.r....................................................................
r..........rr...................................................................
r..........r....................................................................
r..........r....................................................................
r..........r....................................................................
r..........r....................................................................
r..........r....................................................................
r..........r....................................................................
rrrrrrrrrrrr....................................................................
rrrrrrrrrrrr....................................................................
rrrrrrrrrrrr....................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
......p.........................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................`
]

setMap(levels[level])

setPushables({
[pen]: [],
[player]: []
})

// Function to draw a red sprite at a specific tile
function drawTile(xTile, yTile) {
addSprite(xTile, yTile, red)
}

// Function to draw a vertical wall starting from a specific position (x) and with a specific height
function drawWall(x, height) {
const penPos = getFirst(pen) // Get the pen's current position

// Set pen starting position to the middle of the map (height / 2)
penPos.x = x
penPos.y = customFloor(80 / 2) // Position it at the center of the map (using customFloor)

// Calculate the start and end y positions based on the desired height
const startY = penPos.y - customFloor(height / 2) // Start drawing above the center
const endY = penPos.y + customFloor(height / 2) // End drawing below the center

// Draw the red tiles for the wall
for (let y = startY; y <= endY; y++) {
drawTile(penPos.x, y) // Draw each tile
}
}

function euclideanDistanceSquared(x1, y1, x2, y2) {
// Calculate the squared difference in the x and y coordinates
let dx = x2 - x1;
let dy = y2 - y1;

// Return the sum of the squared differences (no square root)
return (dx * dx) + (dy * dy);
}


// Player movement and actions
onInput("s", () => {
// Call the drawWall function to draw a vertical wall at x = 10 and height = 15
// Draws a vertical wall at x = 10 with a height of 15 tiles

drawWall(0,4)


})

afterInput(() => {
// Handle any additional actions after input here (if needed)
})