rayjs/examples/2d_camera.js

114 lines
4.2 KiB
JavaScript
Raw Normal View History

2023-05-11 20:10:40 +00:00
import * as rlc from "raylib.core"
// Initialization
//--------------------------------------------------------------------------------------
const screenWidth = 800;
const screenHeight = 450;
const MAX_BUILDINGS = 100
rlc.initWindow(screenWidth, screenHeight, "raylib [core] example - 2d camera");
const player = new rlc.Rectangle(400, 280, 40, 40);
const buildings = new Array(MAX_BUILDINGS);
const buildColors = new Array(MAX_BUILDINGS);
let spacing = 0;
for (let i = 0; i < MAX_BUILDINGS; i++)
{
2023-05-13 12:49:05 +00:00
const height = rlc.getRandomValue(100, 800)
buildings[i] = new rlc.Rectangle(
-6000.0 + spacing,
screenHeight - 130.0 - height,
rlc.getRandomValue(50, 200),
height)
2023-05-11 20:10:40 +00:00
spacing += buildings[i].width;
buildColors[i] = new rlc.Color(rlc.getRandomValue(200, 240), rlc.getRandomValue(200, 240), rlc.getRandomValue(200, 250), 255);
}
2023-05-13 12:49:05 +00:00
const camera = new rlc.Camera2D(new rlc.Vector2(screenWidth/2.0, screenHeight/2.0),new rlc.Vector2(player.x + 20.0, player.y + 20.0), 0, 1)
2023-05-11 20:10:40 +00:00
2023-05-13 12:49:05 +00:00
rlc.setTargetFPS(60); // Set our game to run at 60 frames-per-second
2023-05-11 20:10:40 +00:00
//--------------------------------------------------------------------------------------
// Main game loop
2023-05-13 12:49:05 +00:00
while (!rlc.windowShouldClose()) // Detect window close button or ESC key
2023-05-11 20:10:40 +00:00
{
// Update
//----------------------------------------------------------------------------------
// Player movement
2023-05-13 12:49:05 +00:00
if (rlc.isKeyDown(rlc.KEY_RIGHT)) player.x += 2;
else if (rlc.isKeyDown(rlc.KEY_LEFT)) player.x -= 2;
2023-05-11 20:10:40 +00:00
// Camera target follows player
2023-05-13 12:49:05 +00:00
const cameraTarget = new rlc.Vector2(player.x + 20, player.y + 20);
camera.target = cameraTarget;
2023-05-11 20:10:40 +00:00
// Camera rotation controls
2023-05-13 12:49:05 +00:00
if (rlc.isKeyDown(rlc.KEY_A)) camera.rotation--;
else if (rlc.isKeyDown(rlc.KEY_S)) camera.rotation++;
2023-05-11 20:10:40 +00:00
// Limit camera rotation to 80 degrees (-40 to 40)
if (camera.rotation > 40) camera.rotation = 40;
else if (camera.rotation < -40) camera.rotation = -40;
// Camera zoom controls
2023-05-13 12:49:05 +00:00
camera.zoom += (rlc.getMouseWheelMove()*0.05);
2023-05-11 20:10:40 +00:00
2023-05-13 12:49:05 +00:00
if (camera.zoom > 3.0) camera.zoom = 3.0;
else if (camera.zoom < 0.1) camera.zoom = 0.1;
2023-05-11 20:10:40 +00:00
// Camera reset (zoom and rotation)
2023-05-13 12:49:05 +00:00
if (rlc.isKeyPressed(rlc.KEY_R))
2023-05-11 20:10:40 +00:00
{
2023-05-13 12:49:05 +00:00
camera.zoom = 1.0;
camera.rotation = 0.0;
2023-05-11 20:10:40 +00:00
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
2023-05-13 12:49:05 +00:00
rlc.beginDrawing();
2023-05-11 20:10:40 +00:00
2023-05-13 12:49:05 +00:00
rlc.clearBackground(rlc.RAYWHITE);
2023-05-11 20:10:40 +00:00
2023-05-13 12:49:05 +00:00
rlc.beginMode2D(camera);
2023-05-11 20:10:40 +00:00
2023-05-13 12:49:05 +00:00
rlc.drawRectangle(-6000, 320, 13000, 8000, rlc.DARKGRAY);
2023-05-11 20:10:40 +00:00
2023-05-13 12:49:05 +00:00
for (let i = 0; i < MAX_BUILDINGS; i++) rlc.drawRectangleRec(buildings[i], buildColors[i]);
2023-05-11 20:10:40 +00:00
2023-05-13 12:49:05 +00:00
rlc.drawRectangleRec(player, rlc.RED);
2023-05-11 20:10:40 +00:00
2023-05-13 12:49:05 +00:00
rlc.drawLine(cameraTarget.x, -screenHeight*10, cameraTarget.x, screenHeight*10, rlc.GREEN);
rlc.drawLine(-screenWidth*10, cameraTarget.y, screenWidth*10, cameraTarget.y, rlc.GREEN);
2023-05-11 20:10:40 +00:00
2023-05-13 12:49:05 +00:00
rlc.endMode2D();
2023-05-11 20:10:40 +00:00
2023-05-13 12:49:05 +00:00
rlc.drawText("SCREEN AREA", 640, 10, 20, rlc.RED);
2023-05-11 20:10:40 +00:00
2023-05-13 12:49:05 +00:00
rlc.drawRectangle(0, 0, screenWidth, 5, rlc.RED);
rlc.drawRectangle(0, 5, 5, screenHeight - 10, rlc.RED);
rlc.drawRectangle(screenWidth - 5, 5, 5, screenHeight - 10, rlc.RED);
rlc.drawRectangle(0, screenHeight - 5, screenWidth, 5, rlc.RED);
2023-05-11 20:10:40 +00:00
2023-05-13 12:49:05 +00:00
rlc.drawRectangle( 10, 10, 250, 113, rlc.fade(rlc.SKYBLUE, 0.5));
rlc.drawRectangleLines( 10, 10, 250, 113, rlc.BLUE);
2023-05-11 20:10:40 +00:00
2023-05-13 12:49:05 +00:00
rlc.drawText("Free 2d camera controls:", 20, 20, 10, rlc.BLACK);
rlc.drawText("- Right/Left to move Offset", 40, 40, 10, rlc.DARKGRAY);
rlc.drawText("- Mouse Wheel to Zoom in-out", 40, 60, 10, rlc.DARKGRAY);
rlc.drawText("- A / S to Rotate", 40, 80, 10, rlc.DARKGRAY);
rlc.drawText("- R to reset Zoom and Rotation", 40, 100, 10, rlc.DARKGRAY);
2023-05-11 20:10:40 +00:00
2023-05-13 12:49:05 +00:00
rlc.endDrawing();
2023-05-11 20:10:40 +00:00
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
2023-05-13 12:49:05 +00:00
rlc.closeWindow(); // Close window and OpenGL context
2023-05-11 20:10:40 +00:00
//--------------------------------------------------------------------------------------