Skip to content

Commit

Permalink
Merge pull request #795 from JustAGenericUsername/main
Browse files Browse the repository at this point in the history
scenexe mod 0.2
  • Loading branch information
slweeb authored Sep 14, 2024
2 parents 08c1e5a + 11f4cb1 commit 85785d1
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 22 deletions.
2 changes: 1 addition & 1 deletion mod-list.html
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ <h2>Sandboxels Mod List</h2>
<tr><td>randomness_but_tick.js</td><td>Random experimental elements using the tick function feature</td><td>Alice</td></tr>
<tr><td>randomness_but_tool.js</td><td>Random experimental elements using the tool function feature</td><td>Alice</td></tr>
<tr><td>randomness.js</td><td>Random experimental elements</td><td>Alice</td></tr>
<tr><td>scenexe.js</td><td>Work-in-progress mod, renders and simulates a bunch of polygons</td><td>nousernamefound</td></tr>
<tr><td>scenexe.js</td><td>Work-in-progress mod, allows you to move through a simulated field of polygons</td><td>nousernamefound</td></tr>
<tr><td>structure_test_2.js</td><td>Another test for implementing structures into Sandboxels (requires the previous test)</td><td>Alice</td></tr>
<tr><td>structure_test.js</td><td>A test for implementing structures into Sandboxels</td><td>Alice</td></tr>
<tr><td>test.js</td><td>A test mod that adds mayo :)</td><td><a href="https://R74n.com" class="R74nLink">R74n</a></td></tr>
Expand Down
169 changes: 148 additions & 21 deletions mods/scenexe.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,46 @@ polygonColors = function(sides){
return {r: 0, g: 0, b: 0}
}
}
isKeyDown = {
w: false,
a: false,
s: false,
d: false
}
// when wasd keydown, set isKeyDown to true
document.addEventListener("keydown", (event) => {
if (event.key === "w") {
isKeyDown.w = true;
}
if (event.key === "a") {
isKeyDown.a = true;
}
if (event.key === "s") {
isKeyDown.s = true;
}
if (event.key === "d") {
isKeyDown.d = true
}
})
// when wasd keyup, set isKeyDown to false
document.addEventListener("keyup", (event) => {
if (event.key === "w") {
isKeyDown.w = false;
}
if (event.key === "a") {
isKeyDown.a = false;
}
if (event.key === "s") {
isKeyDown.s = false;
}
if (event.key === "d") {
isKeyDown.d = false
}
})
orbitalSpeed = function(sides){
return 1/(1.00672*Math.pow(0.344151, sides) + 0.000002)
}
shapeNames = ["Triangle", "Square", "Pentagon", "Hexagon", "Heptagon", "Octagon", "Nonagon", "Decagon", "Hendecagon", "Dodecagon", "Triskaidecagon", "Tetrakaidecagon", "Pentakaidecagon", "Hexakaidecagon", "Heptakaidecagon", "Octakaidecagon", "Enneakaidecagon", "Icosagon", "literally just a circle"]
function polygonCount(random){
const thresholds = [0, 0.3, 0.68, 0.77, 0.82, 0.86, 0.88, 0.89, 0.895, 0.8975, 0.898, 0.8982, 1];
const values = [3, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 3];
Expand All @@ -30,7 +70,7 @@ function polygonCount(random){
}
}
polygonList = []
zoomLevel = .2
zoomLevel = 1
polygonSize = function(sides){
return 18 * Math.pow(1.47, sides - 3)
}
Expand All @@ -45,6 +85,7 @@ for (var i = 0; i <= 400; i++){
vy: 0,
random: Math.random(),
rotation: Math.random() * 2 * Math.PI,
collisionMass: 3+(polygonSize(sides)**2)/6
})
}
function drawPolygon(ctx, polygon){
Expand Down Expand Up @@ -80,65 +121,151 @@ function drawPolygon(ctx, polygon){
ctx.lineTo(x+20*vx, y+20*vy)
ctx.strokeStyle = "rgb(255, 0, 0)"
ctx.stroke()
// draw some text to represent mass
ctx.lineWidth = 2
ctx.fillStyle = "rgb(255, 255, 255)"
ctx.strokeStyle = "rgb(0, 0, 0)"
ctx.font = `bold 30px Helvetica`
ctx.fillText(Math.round(polygon.collisionMass), x, y)
ctx.strokeText(Math.round(polygon.collisionMass), x, y)
*/
}
function drawPlayer(ctx, player){
ctx.fillStyle = "rgb(63, 153, 255)"
ctx.strokeStyle = "rgb(33, 123, 225)"
ctx.lineWidth = 5*zoomLevel
ctx.beginPath()
ctx.arc(player.x, player.y, player.radius, 0, 2*Math.PI)
ctx.fill()
ctx.stroke()
}
function zoom(ctx, poly, multiplier){
polyreturn = {...poly}
polyreturn.x = polyreturn.x * multiplier + ctx.canvas.width/2
polyreturn.y = polyreturn.y * multiplier + ctx.canvas.height/2
polyreturn.radius = polyreturn.radius * multiplier
return polyreturn
}
function applyCameraPos(ctx, object, x, y){
objectreturn = {...object}
objectreturn.x = objectreturn.x + x
objectreturn.y = objectreturn.y + y
return objectreturn
}
function cameraZoom(ctx, object, multiplier, camera){
objectreturn = {...object}
let x = camera[0]
let y = camera[1]
objectreturn.x = objectreturn.x * multiplier - x + ctx.canvas.width/2
objectreturn.y = objectreturn.y * multiplier - y + ctx.canvas.height/2
objectreturn.radius = objectreturn.radius * multiplier
return objectreturn
}
camera = [0, 0]
scenexeplayer = {
x: 0,
y: 0,
vx: 0,
vy: 0,
rotation: 0,
radius: 40,
collisionMass: 520
}
renderPostPixel(function(ctx){
ctx.fillStyle = "rgb(205, 205, 205)"
ctx.fillRect(0, 0, canvas.width, canvas.height)
ctx.strokeStyle = "rgb(200, 200, 200)"
ctx.lineWidth = 5*zoomLevel
for (var i = 0; i < canvas.width; i += canvas.width/30*zoomLevel){
for (var i = 0; i < canvas.width; i += 20*zoomLevel){
ctx.beginPath()
ctx.moveTo(i, canvas.height)
ctx.lineTo(i, 0)
//ctx.moveTo(i, canvas.height)
//ctx.lineTo(i, 0)
// add offset depending on mod of camera
ctx.moveTo(i-camera[0]%20*zoomLevel, canvas.height)
ctx.lineTo(i-camera[0]%20*zoomLevel, 0)
ctx.stroke()
}
for (var j = 0; j < canvas.height; j += canvas.width/30*zoomLevel){
for (var j = 0; j < canvas.height; j += 20*zoomLevel){
ctx.beginPath()
ctx.moveTo(0, j)
ctx.lineTo(canvas.width, j)
//ctx.moveTo(0, j)
//ctx.lineTo(canvas.width, j)
// add offset depending on mod of camera
ctx.moveTo(0, j-camera[1]%20*zoomLevel)
ctx.lineTo(canvas.width, j-camera[1]%20*zoomLevel)
ctx.stroke()
}
//drawPolygon(ctx, Math.round((pixelTicks/20)%13)+3, polygonSize(Math.round((pixelTicks/20)%13)+3), canvas.width/2, canvas.height/2, 0)
for (var poly = 0; poly < polygonList.length; poly++){
drawPolygon(ctx, zoom(ctx, polygonList[poly], zoomLevel))
drawPolygon(ctx, cameraZoom(ctx, polygonList[poly], zoomLevel, camera))
}
// polygon collision checking
for (var poly = 0; poly < polygonList.length; poly++){
for (var poly2 = 0; poly2 < polygonList.length; poly2++){
drawPlayer(ctx, cameraZoom(ctx, scenexeplayer, zoomLevel, camera))
// collidable collision checking
collideList = []
collideList = collideList.concat(polygonList)
collideList.push(scenexeplayer)
for (var poly = 0; poly < collideList.length; poly++){
for (var poly2 = 0; poly2 < collideList.length; poly2++){
if (poly != poly2){
polygon1 = polygonList[poly]
polygon2 = polygonList[poly2]
polygon1 = collideList[poly]
polygon2 = collideList[poly2]
distance = Math.sqrt((polygon1.x - polygon2.x)**2 + (polygon1.y - polygon2.y)**2)
if (distance < polygonSize(polygon1.sides) + polygonSize(polygon2.sides)){
if (distance < polygon1.radius + polygon2.radius){
//calculate angle of collision
angle = Math.atan2(polygon2.y - polygon1.y, polygon2.x - polygon1.x)
// update velocity
polygon1.vx -= (Math.cos(angle) * (polygonSize(polygon1.sides) + polygonSize(polygon2.sides) - distance)/2)/(3+polygonSize(polygon1.sides)/9)
polygon1.vy -= (Math.sin(angle) * (polygonSize(polygon1.sides) + polygonSize(polygon2.sides) - distance)/2)/(3+polygonSize(polygon1.sides)/9)
polygon1.vx -= (Math.cos(angle) * (polygon1.radius + polygon2.radius - distance)/2)/(polygon1.collisionMass/polygon2.collisionMass**(1/2.5))
polygon1.vy -= (Math.sin(angle) * (polygon1.radius + polygon2.radius - distance)/2)/(polygon1.collisionMass/polygon2.collisionMass**(1/2.5))
}
}
}
}
// move polygons
for (var poly = 0; poly < polygonList.length; poly++){
let polygon = polygonList[poly]
for (var poly = 0; poly < collideList.length; poly++){
let polygon = collideList[poly]
if (polygon.vx || polygon.vy){
polygon.x += polygon.vx
polygon.y += polygon.vy
polygon.vx *= 0.8
polygon.vy *= 0.8
}
polygon.rotation += 1.00672*Math.pow(0.344151, polygon.sides) + 0.000002
}
// orbit polygons
for (var poly = 0; poly < polygonList.length; poly++){
let polygon = polygonList[poly]
polygon.rotation += 1/orbitalSpeed(polygon.sides)
// add some velocity towards wherever its facing
polygon.vx += Math.sin(polygon.rotation)*(1.00672*Math.pow(0.344151, polygon.sides) + 0.000002)*8
polygon.vy += Math.cos(polygon.rotation)*(1.00672*Math.pow(0.344151, polygon.sides) + 0.000002)*8
polygon.vx += Math.sin(polygon.rotation)/orbitalSpeed(polygon.sides)*8
polygon.vy += Math.cos(polygon.rotation)/orbitalSpeed(polygon.sides)*8
}
camera[0] += (scenexeplayer.x - camera[0])/10
camera[1] += (scenexeplayer.y - camera[1])/10
// add velocity to player depending on which keys are down
if (isKeyDown.w){
if (isKeyDown.a + isKeyDown.d == 1){
scenexeplayer.vy -= 1/Math.sqrt(2)
} else{
scenexeplayer.vy -= 1
}
}
if (isKeyDown.s){
if (isKeyDown.a + isKeyDown.d == 1){
scenexeplayer.vy += 1/Math.sqrt(2)
} else{
scenexeplayer.vy += 1
}
}
if (isKeyDown.a){
if (isKeyDown.w + isKeyDown.s == 1){
scenexeplayer.vx -= 1/Math.sqrt(2)
} else{
scenexeplayer.vx -= 1
}
}
if (isKeyDown.d){
if (isKeyDown.w + isKeyDown.s == 1){
scenexeplayer.vx += 1/Math.sqrt(2)
} else{
scenexeplayer.vx += 1
}
}
})

0 comments on commit 85785d1

Please sign in to comment.