chore: clean recommit
This commit is contained in:
parent
6bb62d9932
commit
8739e3b347
134 changed files with 15442 additions and 10648 deletions
|
|
@ -2,12 +2,16 @@
|
|||
*
|
||||
* raylib [core] example - 2d camera platformer
|
||||
*
|
||||
* This example has been created using raylib 2.5 (www.raylib.com)
|
||||
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
|
||||
* Example complexity rating: [★★★☆] 3/4
|
||||
*
|
||||
* Example originally created with raylib 2.5, last time updated with raylib 3.0
|
||||
*
|
||||
* Example contributed by arvyy (@arvyy) and reviewed by Ramon Santamaria (@raysan5)
|
||||
*
|
||||
* Copyright (c) 2019 arvyy (@arvyy)
|
||||
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
|
||||
* BSD-like license that allows static linking with closed source software
|
||||
*
|
||||
* Copyright (c) 2019-2025 arvyy (@arvyy)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
|
|
@ -18,20 +22,27 @@ using static Raylib_cs.Raymath;
|
|||
|
||||
namespace Examples.Core;
|
||||
|
||||
public class Camera2dPlatformer
|
||||
public partial class Camera2dPlatformer : IExample
|
||||
{
|
||||
const int G = 400;
|
||||
const float PlayerJumpSpeed = 350.0f;
|
||||
const float PlayerHorSpeed = 200.0f;
|
||||
private const int screenWidth = 800;
|
||||
private const int screenHeight = 450;
|
||||
|
||||
struct Player
|
||||
private const int G = 400;
|
||||
private const float PlayerJumpSpeed = 350.0f;
|
||||
private const float PlayerHorSpeed = 200.0f;
|
||||
|
||||
public string Name => "Core / 2D Camera Platformer";
|
||||
|
||||
public string Title => "raylib [core] example - 2d camera platformer";
|
||||
|
||||
private struct Player
|
||||
{
|
||||
public Vector2 Position;
|
||||
public float Speed;
|
||||
public bool CanJump;
|
||||
}
|
||||
|
||||
struct EnvItem
|
||||
private struct EnvItem
|
||||
{
|
||||
public Rectangle Rect;
|
||||
public int Blocking;
|
||||
|
|
@ -45,7 +56,7 @@ public class Camera2dPlatformer
|
|||
}
|
||||
}
|
||||
|
||||
delegate void CameraUpdaterCallback(
|
||||
private delegate void CameraUpdaterCallback(
|
||||
ref Camera2D camera,
|
||||
ref Player player,
|
||||
EnvItem[] envItems,
|
||||
|
|
@ -54,21 +65,22 @@ public class Camera2dPlatformer
|
|||
int height
|
||||
);
|
||||
|
||||
public static int Main()
|
||||
private Player player;
|
||||
private EnvItem[] envItems;
|
||||
private Camera2D camera;
|
||||
private CameraUpdaterCallback[] cameraUpdaters;
|
||||
private int cameraOption;
|
||||
private int cameraUpdatersLength;
|
||||
private string[] cameraDescriptions;
|
||||
|
||||
public void Init()
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
const int screenWidth = 800;
|
||||
const int screenHeight = 450;
|
||||
|
||||
InitWindow(screenWidth, screenHeight, "raylib [core] example - 2d camera");
|
||||
|
||||
Player player = new();
|
||||
player = new();
|
||||
player.Position = new Vector2(400, 280);
|
||||
player.Speed = 0;
|
||||
player.CanJump = false;
|
||||
|
||||
EnvItem[] envItems = new EnvItem[]
|
||||
envItems = new EnvItem[]
|
||||
{
|
||||
new EnvItem(new Rectangle(0, 0, 1000, 400), 0, Color.LightGray),
|
||||
new EnvItem(new Rectangle(0, 400, 1000, 200), 1, Color.Gray),
|
||||
|
|
@ -77,14 +89,14 @@ public class Camera2dPlatformer
|
|||
new EnvItem(new Rectangle(650, 300, 100, 10), 1, Color.Gray)
|
||||
};
|
||||
|
||||
Camera2D camera = new();
|
||||
camera = new();
|
||||
camera.Target = player.Position;
|
||||
camera.Offset = new Vector2(screenWidth / 2, screenHeight / 2);
|
||||
camera.Offset = new Vector2(screenWidth / 2.0f, screenHeight / 2.0f);
|
||||
camera.Rotation = 0.0f;
|
||||
camera.Zoom = 1.0f;
|
||||
|
||||
// Store callbacks to the multiple update camera functions
|
||||
CameraUpdaterCallback[] cameraUpdaters = new CameraUpdaterCallback[]
|
||||
// Store pointers to the multiple update camera functions
|
||||
cameraUpdaters = new CameraUpdaterCallback[]
|
||||
{
|
||||
UpdateCameraCenter,
|
||||
UpdateCameraCenterInsideMap,
|
||||
|
|
@ -93,93 +105,89 @@ public class Camera2dPlatformer
|
|||
UpdateCameraPlayerBoundsPush
|
||||
};
|
||||
|
||||
int cameraOption = 0;
|
||||
int cameraUpdatersLength = cameraUpdaters.Length;
|
||||
cameraOption = 0;
|
||||
cameraUpdatersLength = cameraUpdaters.Length;
|
||||
|
||||
string[] cameraDescriptions = new string[]{
|
||||
cameraDescriptions = new string[]{
|
||||
"Follow player center",
|
||||
"Follow player center, but clamp to map edges",
|
||||
"Follow player center; smoothed",
|
||||
"Follow player center horizontally; update player center vertically after landing",
|
||||
"Player push camera on getting too close to screen edge"
|
||||
};
|
||||
|
||||
SetTargetFPS(60);
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose())
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
float deltaTime = GetFrameTime();
|
||||
|
||||
UpdatePlayer(ref player, envItems, deltaTime);
|
||||
|
||||
camera.Zoom += ((float)GetMouseWheelMove() * 0.05f);
|
||||
|
||||
if (camera.Zoom > 3.0f)
|
||||
{
|
||||
camera.Zoom = 3.0f;
|
||||
}
|
||||
else if (camera.Zoom < 0.25f)
|
||||
{
|
||||
camera.Zoom = 0.25f;
|
||||
}
|
||||
|
||||
if (IsKeyPressed(KeyboardKey.R))
|
||||
{
|
||||
camera.Zoom = 1.0f;
|
||||
player.Position = new Vector2(400, 280);
|
||||
}
|
||||
|
||||
if (IsKeyPressed(KeyboardKey.C))
|
||||
{
|
||||
cameraOption = (cameraOption + 1) % cameraUpdatersLength;
|
||||
}
|
||||
|
||||
// Call update camera function by its pointer
|
||||
cameraUpdaters[cameraOption](ref camera, ref player, envItems, deltaTime, screenWidth, screenHeight);
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
ClearBackground(Color.LightGray);
|
||||
|
||||
BeginMode2D(camera);
|
||||
|
||||
for (int i = 0; i < envItems.Length; i++)
|
||||
{
|
||||
DrawRectangleRec(envItems[i].Rect, envItems[i].Color);
|
||||
}
|
||||
|
||||
Rectangle playerRect = new(player.Position.X - 20, player.Position.Y - 40, 40, 40);
|
||||
DrawRectangleRec(playerRect, Color.Red);
|
||||
|
||||
EndMode2D();
|
||||
|
||||
DrawText("Controls:", 20, 20, 10, Color.Black);
|
||||
DrawText("- Right/Left to move", 40, 40, 10, Color.DarkGray);
|
||||
DrawText("- Space to jump", 40, 60, 10, Color.DarkGray);
|
||||
DrawText("- Mouse Wheel to Zoom in-out, R to reset zoom", 40, 80, 10, Color.DarkGray);
|
||||
DrawText("- C to change camera mode", 40, 100, 10, Color.DarkGray);
|
||||
DrawText("Current camera mode:", 20, 120, 10, Color.Black);
|
||||
DrawText(cameraDescriptions[cameraOption], 40, 140, 10, Color.DarkGray);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
CloseWindow();
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void UpdatePlayer(ref Player player, EnvItem[] envItems, float delta)
|
||||
public void Update()
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
var deltaTime = GetFrameTime();
|
||||
|
||||
UpdatePlayer(ref player, envItems, deltaTime);
|
||||
|
||||
camera.Zoom += ((float)GetMouseWheelMove() * 0.05f);
|
||||
|
||||
if (camera.Zoom > 3.0f)
|
||||
{
|
||||
camera.Zoom = 3.0f;
|
||||
}
|
||||
else if (camera.Zoom < 0.25f)
|
||||
{
|
||||
camera.Zoom = 0.25f;
|
||||
}
|
||||
|
||||
if (IsKeyPressed(KeyboardKey.R))
|
||||
{
|
||||
camera.Zoom = 1.0f;
|
||||
player.Position = new Vector2(400, 280);
|
||||
}
|
||||
|
||||
if (IsKeyPressed(KeyboardKey.C))
|
||||
{
|
||||
cameraOption = (cameraOption + 1) % cameraUpdatersLength;
|
||||
}
|
||||
|
||||
// Call update camera function by its pointer
|
||||
cameraUpdaters[cameraOption](ref camera, ref player, envItems, deltaTime, screenWidth, screenHeight);
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
ClearBackground(Color.LightGray);
|
||||
|
||||
BeginMode2D(camera);
|
||||
|
||||
for (var i = 0; i < envItems.Length; i++)
|
||||
{
|
||||
DrawRectangleRec(envItems[i].Rect, envItems[i].Color);
|
||||
}
|
||||
|
||||
Rectangle playerRect = new(player.Position.X - 20, player.Position.Y - 40, 40.0f, 40.0f);
|
||||
DrawRectangleRec(playerRect, Color.Red);
|
||||
|
||||
DrawCircleV(player.Position, 5.0f, Color.Gold);
|
||||
|
||||
EndMode2D();
|
||||
|
||||
DrawText("Controls:", 20, 20, 10, Color.Black);
|
||||
DrawText("- Right/Left to move", 40, 40, 10, Color.DarkGray);
|
||||
DrawText("- Space to jump", 40, 60, 10, Color.DarkGray);
|
||||
DrawText("- Mouse Wheel to Zoom in-out", 40, 80, 10, Color.DarkGray);
|
||||
DrawText("- R to reset position + zoom", 40, 100, 10, Color.DarkGray);
|
||||
DrawText("- C to change camera mode", 40, 120, 10, Color.DarkGray);
|
||||
DrawText("Current camera mode:", 20, 140, 10, Color.Black);
|
||||
DrawText(cameraDescriptions[cameraOption], 40, 160, 10, Color.DarkGray);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
public void Unload()
|
||||
{
|
||||
}
|
||||
|
||||
private static void UpdatePlayer(ref Player player, EnvItem[] envItems, float delta)
|
||||
{
|
||||
if (IsKeyDown(KeyboardKey.Left))
|
||||
{
|
||||
|
|
@ -197,11 +205,11 @@ public class Camera2dPlatformer
|
|||
player.CanJump = false;
|
||||
}
|
||||
|
||||
int hitObstacle = 0;
|
||||
for (int i = 0; i < envItems.Length; i++)
|
||||
var hitObstacle = 0;
|
||||
for (var i = 0; i < envItems.Length; i++)
|
||||
{
|
||||
EnvItem ei = envItems[i];
|
||||
Vector2 p = player.Position;
|
||||
var ei = envItems[i];
|
||||
var p = player.Position;
|
||||
if (ei.Blocking != 0 &&
|
||||
ei.Rect.X <= p.X &&
|
||||
ei.Rect.X + ei.Rect.Width >= p.X &&
|
||||
|
|
@ -211,6 +219,7 @@ public class Camera2dPlatformer
|
|||
hitObstacle = 1;
|
||||
player.Speed = 0.0f;
|
||||
player.Position.Y = ei.Rect.Y;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -226,7 +235,7 @@ public class Camera2dPlatformer
|
|||
}
|
||||
}
|
||||
|
||||
static void UpdateCameraCenter(
|
||||
private static void UpdateCameraCenter(
|
||||
ref Camera2D camera,
|
||||
ref Player player,
|
||||
EnvItem[] envItems,
|
||||
|
|
@ -235,11 +244,11 @@ public class Camera2dPlatformer
|
|||
int height
|
||||
)
|
||||
{
|
||||
camera.Offset = new Vector2(width / 2, height / 2);
|
||||
camera.Offset = new Vector2(width / 2.0f, height / 2.0f);
|
||||
camera.Target = player.Position;
|
||||
}
|
||||
|
||||
static void UpdateCameraCenterInsideMap(
|
||||
private static void UpdateCameraCenterInsideMap(
|
||||
ref Camera2D camera,
|
||||
ref Player player,
|
||||
EnvItem[] envItems,
|
||||
|
|
@ -248,43 +257,43 @@ public class Camera2dPlatformer
|
|||
int height)
|
||||
{
|
||||
camera.Target = player.Position;
|
||||
camera.Offset = new Vector2(width / 2, height / 2);
|
||||
camera.Offset = new Vector2(width / 2.0f, height / 2.0f);
|
||||
float minX = 1000, minY = 1000, maxX = -1000, maxY = -1000;
|
||||
|
||||
for (int i = 0; i < envItems.Length; i++)
|
||||
for (var i = 0; i < envItems.Length; i++)
|
||||
{
|
||||
EnvItem ei = envItems[i];
|
||||
var ei = envItems[i];
|
||||
minX = Math.Min(ei.Rect.X, minX);
|
||||
maxX = Math.Max(ei.Rect.X + ei.Rect.Width, maxX);
|
||||
minY = Math.Min(ei.Rect.Y, minY);
|
||||
maxY = Math.Max(ei.Rect.Y + ei.Rect.Height, maxY);
|
||||
}
|
||||
|
||||
Vector2 max = GetWorldToScreen2D(new Vector2(maxX, maxY), camera);
|
||||
Vector2 min = GetWorldToScreen2D(new Vector2(minX, minY), camera);
|
||||
var max = GetWorldToScreen2D(new Vector2(maxX, maxY), camera);
|
||||
var min = GetWorldToScreen2D(new Vector2(minX, minY), camera);
|
||||
|
||||
if (max.X < width)
|
||||
{
|
||||
camera.Offset.X = width - (max.X - width / 2);
|
||||
camera.Offset.X = width - (max.X - width / 2.0f);
|
||||
}
|
||||
|
||||
if (max.Y < height)
|
||||
{
|
||||
camera.Offset.Y = height - (max.Y - height / 2);
|
||||
camera.Offset.Y = height - (max.Y - height / 2.0f);
|
||||
}
|
||||
|
||||
if (min.X > 0)
|
||||
{
|
||||
camera.Offset.X = width / 2 - min.X;
|
||||
camera.Offset.X = width / 2.0f - min.X;
|
||||
}
|
||||
|
||||
if (min.Y > 0)
|
||||
{
|
||||
camera.Offset.Y = height / 2 - min.Y;
|
||||
camera.Offset.Y = height / 2.0f - min.Y;
|
||||
}
|
||||
}
|
||||
|
||||
static void UpdateCameraCenterSmoothFollow(
|
||||
private static void UpdateCameraCenterSmoothFollow(
|
||||
ref Camera2D camera,
|
||||
ref Player player,
|
||||
EnvItem[] envItems,
|
||||
|
|
@ -297,18 +306,18 @@ public class Camera2dPlatformer
|
|||
const float minEffectLength = 10;
|
||||
const float fractionSpeed = 0.8f;
|
||||
|
||||
camera.Offset = new Vector2(width / 2, height / 2);
|
||||
Vector2 diff = Vector2Subtract(player.Position, camera.Target);
|
||||
float length = Vector2Length(diff);
|
||||
camera.Offset = new Vector2(width / 2.0f, height / 2.0f);
|
||||
var diff = Vector2Subtract(player.Position, camera.Target);
|
||||
var length = Vector2Length(diff);
|
||||
|
||||
if (length > minEffectLength)
|
||||
{
|
||||
float speed = Math.Max(fractionSpeed * length, minSpeed);
|
||||
var speed = Math.Max(fractionSpeed * length, minSpeed);
|
||||
camera.Target = Vector2Add(camera.Target, Vector2Scale(diff, speed * delta / length));
|
||||
}
|
||||
}
|
||||
|
||||
static void UpdateCameraEvenOutOnLanding(
|
||||
private static void UpdateCameraEvenOutOnLanding(
|
||||
ref Camera2D camera,
|
||||
ref Player player,
|
||||
EnvItem[] envItems,
|
||||
|
|
@ -318,10 +327,10 @@ public class Camera2dPlatformer
|
|||
)
|
||||
{
|
||||
float evenOutSpeed = 700;
|
||||
int eveningOut = 0;
|
||||
float evenOutTarget = 0.0f;
|
||||
var eveningOut = 0;
|
||||
var evenOutTarget = 0.0f;
|
||||
|
||||
camera.Offset = new Vector2(width / 2, height / 2);
|
||||
camera.Offset = new Vector2(width / 2.0f, height / 2.0f);
|
||||
camera.Target.X = player.Position.X;
|
||||
|
||||
if (eveningOut != 0)
|
||||
|
|
@ -357,7 +366,7 @@ public class Camera2dPlatformer
|
|||
}
|
||||
}
|
||||
|
||||
static void UpdateCameraPlayerBoundsPush(
|
||||
private static void UpdateCameraPlayerBoundsPush(
|
||||
ref Camera2D camera,
|
||||
ref Player player,
|
||||
EnvItem[] envItems,
|
||||
|
|
@ -368,11 +377,11 @@ public class Camera2dPlatformer
|
|||
{
|
||||
Vector2 bbox = new(0.2f, 0.2f);
|
||||
|
||||
Vector2 bboxWorldMin = GetScreenToWorld2D(
|
||||
var bboxWorldMin = GetScreenToWorld2D(
|
||||
new Vector2((1 - bbox.X) * 0.5f * width, (1 - bbox.Y) * 0.5f * height),
|
||||
camera
|
||||
);
|
||||
Vector2 bboxWorldMax = GetScreenToWorld2D(
|
||||
var bboxWorldMax = GetScreenToWorld2D(
|
||||
new Vector2((1 + bbox.X) * 0.5f * width,
|
||||
(1 + bbox.Y) * 0.5f * height),
|
||||
camera
|
||||
|
|
@ -399,4 +408,32 @@ public class Camera2dPlatformer
|
|||
camera.Target.Y = bboxWorldMin.Y + (player.Position.Y - bboxWorldMax.Y);
|
||||
}
|
||||
}
|
||||
|
||||
public static int Main()
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
InitWindow(screenWidth, screenHeight, "raylib [core] example - 2d camera platformer");
|
||||
|
||||
SetTargetFPS(60);
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
var game = new Camera2dPlatformer();
|
||||
game.Init();
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose())
|
||||
{
|
||||
game.Update();
|
||||
}
|
||||
|
||||
game.Unload();
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue