static void InitializePlayerStartingArea()
{
int baseX = (int)Math.Floor(posX);
int baseY = (int)Math.Floor(posY);
Random random = new Random(1);
code
// Define the radius of the open area
int openRadius = 500;
// Clear the base area
for (int x = baseX - openRadius; x <= baseX + openRadius; x++)
{
for (int y = baseY - openRadius; y <= baseY + openRadius; y++)
{
map[(x, y)] = ' ';
}
}
// Add random walls
for (int x = baseX - openRadius + 1; x < baseX + openRadius; x++)
{
for (int y = baseY - openRadius + 1; y < baseY + openRadius; y++)
{
if (random.Next(100) < 30) // 30% chance of placing a wall
{
map[(x, y)] = '#';
}
}
}
// Add random 3x3 rooms
int numRooms = random.Next(3, 6); // 3 to 5 rooms
for (int i = 0; i < numRooms; i++)
{
int roomX = random.Next(baseX - openRadius + 2, baseX + openRadius - 2);
int roomY = random.Next(baseY - openRadius + 2, baseY + openRadius - 2);
for (int x = roomX - 1; x <= roomX + 1; x++)
{
for (int y = roomY - 1; y <= roomY + 1; y++)
{
map[(x, y)] = ' '; // Clear the room area
}
}
}
// Ensure the player's starting position is clear
map[(baseX, baseY)] = ' ';
// Add walls around the perimeter
for (int x = baseX - openRadius - 1; x <= baseX + openRadius + 1; x++)
{
for (int y = baseY - openRadius - 1; y <= baseY + openRadius + 1; y++)
{
if (Math.Abs(x - baseX) == openRadius + 1 || Math.Abs(y - baseY) == openRadius + 1)
{
map[(x, y)] = '#';
}
}
}
}
static void RenderFrame()
{
StringBuilder sb = new StringBuilder();
Assistant
code
// Initialize a 2D character array for the screen
char[,] screenBuffer = new char[screenHeight, screenWidth];
string[,] colorBuffer = new string[screenHeight, screenWidth];
// Fill screen buffer with default characters and colors
for (int y = 0; y < screenHeight; y++)
for (int x = 0; x < screenWidth; x++)
{
screenBuffer[y, x] = ' ';
colorBuffer[y, x] = "\u001b[0m"; // Reset color
}
// Calculate the vertical center shift based on pitch
// Positive pitch means looking up, so shift center down
// Negative pitch means looking down, so shift center up
// Define a maximum shift based on the pitch range
double pitchFactor = screenHeight; // Adjust as needed
int pitchShift = (int)(pitch / maxPitch * pitchFactor);
// Raycasting
for (int x = 0; x < screenWidth; x++)
{
// Calculate ray position and direction
double cameraX = 2 * x / (double)screenWidth - 1; // x-coordinate in camera space
double rayDirX = dirX + planeX * cameraX;
double rayDirY = dirY + planeY * cameraX;
// Which box of the map we're in
int mapX = (int)Math.Floor(posX);
int mapY = (int)Math.Floor(posY);
// Length of ray from current position to next x or y-side
double sideDistX;
double sideDistY;
// Length of ray from one x or y-side to next x or y-side
double deltaDistX = (rayDirX == 0) ? 1e30 : Math.Abs(1 / rayDirX);
double deltaDistY = (rayDirY == 0) ? 1e30 : Math.Abs(1 / rayDirY);
double perpWallDist;
// What direction to step in x or y-direction (either +1 or -1)
int stepX, stepY;
bool hit = false; // Was a wall hit?
int side = 0; // Was the wall vertical or horizontal?
// Calculate step and initial sideDist
if (rayDirX < 0)
{
stepX = -1;
sideDistX = (posX - mapX) * deltaDistX;
}
else
{
stepX = 1;
sideDistX = (mapX + 1.0 - posX) * deltaDistX;
}
if (rayDirY < 0)
{
stepY = -1;
sideDistY = (posY - mapY) * deltaDistY;
}
else
{
stepY = 1;
sideDistY = (mapY + 1.0 - posY) * deltaDistY;
}
// Perform DDA
while (!hit)
{
// Jump to next map square, either in x-direction or y-direction
if (sideDistX < sideDistY)
{
sideDistX += deltaDistX;
mapX += stepX;
side = 0;
}
else
{
sideDistY += deltaDistY;
mapY += stepY;
side = 1;
}
// Check if ray has hit a wall
if (map.ContainsKey((mapX, mapY)))
{
if (map[(mapX, mapY)] == '#') hit = true;
}
else
{
// Treat unknown areas as walls
hit = true;
}
}
// Calculate distance to the point of impact
if (side == 0)
perpWallDist = (sideDistX - deltaDistX);
else
perpWallDist = (sideDistY - deltaDistY);
// Calculate height of line to draw on screen
int lineHeight = (int)(screenHeight / perpWallDist);
// Calculate lowest and highest pixel to fill in current stripe
// **Modified to include pitchShift**
int drawStart = Math.Max(-lineHeight / 2 + screenHeight / 2 + pitchShift, 0);
int drawEnd = Math.Min(lineHeight / 2 + screenHeight / 2 + pitchShift, screenHeight - 1);
if (wallTexture)
{
// Base color components for #9e9a56
int baseR = 158; // 0x9e
int baseG = 154; // 0x9a
int baseB = 86; // 0x56
// Adjust color intensity based on distance
float distanceFactor = (float)Math.Clamp(1.0 / (perpWallDist * 1.5), 0.0, 1.0);
int colorR = (int)(baseR * distanceFactor);
int colorG = (int)(baseG * distanceFactor);
int colorB = (int)(baseB * distanceFactor);
string baseWallColor = $"\u001b[38;2;{colorR};{colorG};{colorB}m";
string darkWallColor = $"\u001b[38;2;{Math.Max(colorR - 30, 0)};{Math.Max(colorG - 30, 0)};{Math.Max(colorB - 30, 0)}m";
// Darken colors for y-sides to add depth
if (side == 1)
{
colorR = Math.Max(colorR - 30, 0);
colorG = Math.Max(colorG - 30, 0);
colorB = Math.Max(colorB - 30, 0);
baseWallColor = $"\u001b[38;2;{colorR};{colorG};{colorB}m";
darkWallColor = $"\u001b[38;2;{Math.Max(colorR - 20, 0)};{Math.Max(colorG - 20, 0)};{Math.Max(colorB - 20, 0)}m";
}
for (int y = drawStart; y <= drawEnd; y++)
{
if (IsInScreenBounds(x, y))
{
// Create a simple pattern
bool isAlternateColor = ((y + x) % 2 == 0);
// Set wall color and pattern
colorBuffer[y, x] = isAlternateColor ? darkWallColor : baseWallColor;
screenBuffer[y, x] = '█'; // Full block for texture
}
}
}
else
{
// Base color components for #9e9a56
int baseR = 158; // 0x9e
int baseG = 154; // 0x9a
int baseB = 86; // 0x56
// Adjust color intensity based on distance
float distanceFactor = (float)Math.Clamp(1.0 / (perpWallDist * 1.5), 0.0, 1.0);
int colorR = (int)(baseR * distanceFactor);
int colorG = (int)(baseG * distanceFactor);
int colorB = (int)(baseB * distanceFactor);
string wallColor = $"\u001b[38;2;{colorR};{colorG};{colorB}m";
// Darken colors for y-sides to add depth
if (side == 1)
{
colorR = Math.Max(colorR - 30, 0);
colorG = Math.Max(colorG - 30, 0);
colorB = Math.Max(colorB - 30, 0);
wallColor = $"\u001b[38;2;{colorR};{colorG};{colorB}m";
}
for (int y = drawStart; y <= drawEnd; y++)
{
if (IsInScreenBounds(x, y))
{
// Set wall color
colorBuffer[y, x] = wallColor;
screenBuffer[y, x] = '\u2588'; // Full block character
}
}
}
// **Modified Ceiling Rendering Based on Pitch**
for (int y = 0; y < drawStart; y++)
{
if (IsInScreenBounds(x, y))
{
colorBuffer[y, x] = "\u001b[38;2;111;99;39m"; // Bright yellow
screenBuffer[y, x] = '\u2588'; // Full block character
}
}
// **Modified Floor Rendering Based on Pitch**
for (int y = drawEnd + 1; y < screenHeight; y++)
{
if (IsInScreenBounds(x, y))
{
//65541e
colorBuffer[y, x] = "\u001b[38;2;101;84;30m"; // Dark yellow
screenBuffer[y, x] = '\u2588'; // Full block character
}
}
}
if (showUI)
{
// Overlay Minimap at bottom-right corner
RenderMinimap(screenBuffer, colorBuffer);
// Overlay Debug Information at bottom-left corner
RenderDebugInfo(screenBuffer, colorBuffer);
}
// Build the final string with colors
for (int y = 0; y < screenHeight; y++)
{
for (int x = 0; x < screenWidth; x++)
{
sb.Append($"{colorBuffer[y, x]}{screenBuffer[y, x]}");
}
sb.Append(resetColor); // Reset color at end of line
if (y != screenHeight - 1)
{
sb.Append('\n');
}
}
// Output to console
try
{
Console.SetCursorPosition(0, 0);
Console.Write(sb.ToString());
}
catch (ArgumentOutOfRangeException)
{
// Handle potential exceptions when writing outside the buffer
}
}
Instruction: render white square lights in the center on the ceiling transformed and skewed based on perspective and pitch if there's an L in that place on the map
Instruction 2: add Ls randomly to the character map
To achieve the functionality you've described, we'll implement the following two features:
Render White Square Lights on the Ceiling: These lights will appear in the center of the ceiling where there's an 'L' character in the map. The lights will be transformed and skewed based on the player's perspective and pitch.
Randomly Add 'L's to the Character Map:'L' characters will be randomly placed on the map to represent light sources.
Below are the detailed steps and the necessary code modifications to implement these features.
1. Adding 'L' Characters Randomly to the Map
We'll modify the InitializePlayerStartingArea method to randomly place 'L' characters on the map. These 'L's will represent the locations where lights will be rendered on the ceiling.
Steps:
Define the Number of Lights: Decide how many 'L's you want to place randomly within the open area.
Random Placement: Ensure that 'L's are placed in open spaces (' ') to avoid overlapping with walls or rooms.
Code Modification:
csharp
static void InitializePlayerStartingArea()
{
int baseX = (int)Math.Floor(posX);
int baseY = (int)Math.Floor(posY);
Random random = new Random(1);
// Define the radius of the open area
int openRadius = 500;
// Clear the base area
for (int x = baseX - openRadius; x <= baseX + openRadius; x++)
{
for (int y = baseY - openRadius; y <= baseY + openRadius; y++)
{
map[(x, y)] = ' ';
}
}
// Add random walls
for (int x = baseX - openRadius + 1; x < baseX + openRadius; x++)
{
for (int y = baseY - openRadius + 1; y < baseY + openRadius; y++)
{
if (random.Next(100) < 30) // 30% chance of placing a wall
{
map[(x, y)] = '#';
}
}
}
// Add random 3x3 rooms
int numRooms = random.Next(3, 6); // 3 to 5 rooms
for (int i = 0; i < numRooms; i++)
{
int roomX = random.Next(baseX - openRadius + 2, baseX + openRadius - 2);
int roomY = random.Next(baseY - openRadius + 2, baseY + openRadius - 2);
for (int x = roomX - 1; x <= roomX + 1; x++)
{
for (int y = roomY - 1; y <= roomY + 1; y++)
{
map[(x, y)] = ' '; // Clear the room area
}
}
}
// **Add Random 'L's to the Map**
int numLights = random.Next(5, 15); // For example, 5 to 15 lights
for (int i = 0; i < numLights; i++)
{
int lightX, lightY;
// Ensure 'L' is placed on an open space
do
{
lightX = random.Next(baseX - openRadius + 2, baseX + openRadius - 2);
lightY = random.Next(baseY - openRadius + 2, baseY + openRadius - 2);
} while (map[(lightX, lightY)] != ' ');
map[(lightX, lightY)] = 'L';
}
// Ensure the player's starting position is clear
map[(baseX, baseY)] = ' ';
// Add walls around the perimeter
for (int x = baseX - openRadius - 1; x <= baseX + openRadius + 1; x++)
{
for (int y = baseY - openRadius - 1; y <= baseY + openRadius + 1; y++)
{
if (Math.Abs(x - baseX) == openRadius + 1 || Math.Abs(y - baseY) == openRadius + 1)
{
map[(x, y)] = '#';
}
}
}
}
Explanation:
numLights: Determines how many 'L's will be placed.
Placement Loop: Randomly selects positions within the open area and ensures that 'L' is only placed on an open space (' ').
2. Rendering White Square Lights on the Ceiling
We'll enhance the RenderFrame method to render white square lights on the ceiling where 'L' characters exist on the map. These lights will be transformed and skewed based on the player's perspective and pitch.
Steps:
Identify Light Positions: Iterate through the map to find all 'L' positions.
Project Light Positions to Screen Coordinates: Calculate where each 'L' should appear on the screen based on the player's position, direction, and pitch.
Render Lights on Ceiling: Draw white square characters ('█') at the calculated screen positions.
Code Modification:
Add Helper Method to Project Map Coordinates to Screen Coordinates
csharp
/// <summary>
/// Projects a map position to screen coordinates based on player's position, direction, and pitch.
/// </summary>
static (int screenX, int screenY) ProjectToScreen(int mapX, int mapY)
{
// Calculate relative position to player
double dx = mapX - posX;
double dy = mapY - posY;
// Apply rotation based on player direction
double invDet = 1.0 / (planeX * dirY - dirX * planeY); // Required for correct matrix multiplication
double cameraX = dx * dirY - dy * dirX;
double cameraY = -dx * planeY + dy * planeX;
// Avoid division by zero
if (cameraY == 0) cameraY = 1e-6;
// Perspective projection
double screenPosX = (cameraX / cameraY) * (screenWidth / 2);
double screenPosY = (cameraX / cameraY) * (pitch / maxPitch) * (screenHeight / 2);
// Translate to screen coordinates
int screenX = (int)(screenWidth / 2 + screenPosX);
int screenY = (int)(screenHeight / 2 - screenPosY + pitchShift);
return (screenX, screenY);
}
Note: Ensure that planeX, planeY, posX, posY, pitch, maxPitch, screenWidth, and screenHeight are properly defined and accessible within this method's context.
Modify RenderFrame to Include Light Rendering
csharp
Explanation:
Light Position Collection:
As rays are cast during the rendering process, if a ray hits an 'L', its map coordinates are added to the lightPositions list.
Projecting Light Positions:
For each 'L' in lightPositions, the ProjectToScreen method calculates where on the screen the light should appear based on the player's position, direction, and pitch.
Rendering the Light:
A simple white square ('█') is drawn at the projected screen coordinates to represent the light.
The size of the light can be adjusted using the lightSize variable.
Transformation and Skewing: The projection accounts for the player's pitch, which shifts the light's vertical position (screenY) accordingly. The ProjectToScreen method ensures that as the player looks up or down, the lights on the ceiling appear to move and skew based on the perspective.
Color Blending (Optional Enhancement):
Currently, the lights overwrite the ceiling's bright yellow color. To make the lights blend more naturally, you can implement color blending or transparency. This would require a more complex color handling mechanism.
Handling Screen Bounds:
The IsInScreenBounds method ensures that the lights are only rendered within the visible screen area to prevent any out-of-bound errors.
3. Ensure Helper Methods and Variables Are Defined
Make sure the following helper methods and variables are defined in your code:
IsInScreenBounds(int x, int y)
csharp
static bool IsInScreenBounds(int x, int y)
{
return x >= 0 && x < screenWidth && y >= 0 && y < screenHeight;
}
Player and Camera Variables:
Ensure that variables like dirX, dirY, planeX, planeY, posX, posY, pitch, maxPitch, screenWidth, and screenHeight are properly initialized and accessible within the RenderFrame and ProjectToScreen methods.
ANSI Reset Code:
Ensure that resetColor is defined. You can define it at the beginning of the RenderFrame method or globally.
Here's a simplified complete example integrating both features. Due to the complexity of the original code, some parts are condensed for clarity.
csharp
Notes:
The ProjectToScreen method has been simplified for demonstration purposes. Depending on the desired visual effect, you may need to adjust the projection calculations.
The number of lights and their placement can be tweaked by adjusting numLights and the lightSize.
Ensure that your console supports ANSI escape codes and wide characters for proper rendering.
Conclusion
By implementing the above modifications:
Random 'L' Placement:'L' characters are randomly placed on the map during initialization, representing light sources on the ceiling.
Rendering Lights: During each frame rendering, the program identifies these 'L' positions, projects them onto the screen considering the player's perspective and pitch, and renders white square lights at the appropriate screen coordinates.
These enhancements add dynamic lighting to your console-based renderer, improving the visual experience by highlighting specific areas on the ceiling based on map data.
Feel free to further refine the projection logic and rendering details to achieve the exact visual effect you desire.
Not yet included in a published release.
static void RenderFrame()
{
StringBuilder sb = new StringBuilder();
// Initialize a 2D character array for the screen
char[,] screenBuffer = new char[screenHeight, screenWidth];
string[,] colorBuffer = new string[screenHeight, screenWidth];
// Fill screen buffer with default characters and colors
for (int y = 0; y < screenHeight; y++)
for (int x = 0; x < screenWidth; x++)
{
screenBuffer[y, x] = ' ';
colorBuffer[y, x] = "\u001b[0m"; // Reset color
}
// Calculate the vertical center shift based on pitch
// Positive pitch means looking up, so shift center down
// Negative pitch means looking down, so shift center up
// Define a maximum shift based on the pitch range
double pitchFactor = screenHeight; // Adjust as needed
int pitchShift = (int)(pitch / maxPitch * pitchFactor);
// **Store Light Positions**
var lightPositions = new List<(int mapX, int mapY)>();
// Raycasting
for (int x = 0; x < screenWidth; x++)
{
// Calculate ray position and direction
double cameraX = 2 * x / (double)screenWidth - 1; // x-coordinate in camera space
double rayDirX = dirX + planeX * cameraX;
double rayDirY = dirY + planeY * cameraX;
// Which box of the map we're in
int mapX = (int)Math.Floor(posX);
int mapY = (int)Math.Floor(posY);
// Length of ray from current position to next x or y-side
double sideDistX;
double sideDistY;
// Length of ray from one x or y-side to next x or y-side
double deltaDistX = (rayDirX == 0) ? 1e30 : Math.Abs(1 / rayDirX);
double deltaDistY = (rayDirY == 0) ? 1e30 : Math.Abs(1 / rayDirY);
double perpWallDist;
// What direction to step in x or y-direction (either +1 or -1)
int stepX, stepY;
bool hit = false; // Was a wall hit?
int side = 0; // Was the wall vertical or horizontal?
// Calculate step and initial sideDist
if (rayDirX < 0)
{
stepX = -1;
sideDistX = (posX - mapX) * deltaDistX;
}
else
{
stepX = 1;
sideDistX = (mapX + 1.0 - posX) * deltaDistX;
}
if (rayDirY < 0)
{
stepY = -1;
sideDistY = (posY - mapY) * deltaDistY;
}
else
{
stepY = 1;
sideDistY = (mapY + 1.0 - posY) * deltaDistY;
}
// Perform DDA
while (!hit)
{
// Jump to next map square, either in x-direction or y-direction
if (sideDistX < sideDistY)
{
sideDistX += deltaDistX;
mapX += stepX;
side = 0;
}
else
{
sideDistY += deltaDistY;
mapY += stepY;
side = 1;
}
// Check if ray has hit a wall
if (map.ContainsKey((mapX, mapY)))
{
if (map[(mapX, mapY)] == '#') hit = true;
if (map[(mapX, mapY)] == 'L')
{
// Optionally, treat 'L' as a wall or transparent
// For this example, we'll treat it as a wall
hit = true;
}
}
else
{
// Treat unknown areas as walls
hit = true;
}
}
// Calculate distance to the point of impact
if (side == 0)
perpWallDist = (sideDistX - deltaDistX);
else
perpWallDist = (sideDistY - deltaDistY);
// Calculate height of line to draw on screen
int lineHeight = (int)(screenHeight / perpWallDist);
// Calculate lowest and highest pixel to fill in current stripe
// **Modified to include pitchShift**
int drawStart = Math.Max(-lineHeight / 2 + screenHeight / 2 + pitchShift, 0);
int drawEnd = Math.Min(lineHeight / 2 + screenHeight / 2 + pitchShift, screenHeight - 1);
// **Store Light Positions if 'L' is hit**
if (map.ContainsKey((mapX, mapY)) && map[(mapX, mapY)] == 'L')
{
lightPositions.Add((mapX, mapY));
}
if (wallTexture)
{
// ... [Existing wall rendering code remains unchanged]
// (Omitted for brevity)
}
else
{
// ... [Existing non-texture wall rendering code remains unchanged]
// (Omitted for brevity)
}
// **Modified Ceiling Rendering Based on Pitch**
for (int y = 0; y < drawStart; y++)
{
if (IsInScreenBounds(x, y))
{
colorBuffer[y, x] = "\u001b[38;2;111;99;39m"; // Bright yellow
screenBuffer[y, x] = '\u2588'; // Full block character
}
}
// **Modified Floor Rendering Based on Pitch**
for (int y = drawEnd + 1; y < screenHeight; y++)
{
if (IsInScreenBounds(x, y))
{
//65541e
colorBuffer[y, x] = "\u001b[38;2;101;84;30m"; // Dark yellow
screenBuffer[y, x] = '\u2588'; // Full block character
}
}
}
// **Render Lights on the Ceiling**
foreach (var (mapX, mapY) in lightPositions)
{
// Project the map position to screen coordinates
var (screenX, screenY) = ProjectToScreen(mapX, mapY);
// Define the size of the light (e.g., 2x2 blocks)
int lightSize = 2;
for (int ly = -lightSize / 2; ly <= lightSize / 2; ly++)
{
for (int lx = -lightSize / 2; lx <= lightSize / 2; lx++)
{
int px = screenX + lx;
int py = screenY + ly;
if (IsInScreenBounds(px, py))
{
// Blend light color with existing ceiling color
// For simplicity, we'll overwrite with white
colorBuffer[py, px] = "\u001b[38;2;255;255;255m"; // White color
screenBuffer[py, px] = '█'; // Full block character
}
}
}
}
if (showUI)
{
// Overlay Minimap at bottom-right corner
RenderMinimap(screenBuffer, colorBuffer);
// Overlay Debug Information at bottom-left corner
RenderDebugInfo(screenBuffer, colorBuffer);
}
// Build the final string with colors
string resetColor = "\u001b[0m"; // Define resetColor if not already defined
for (int y = 0; y < screenHeight; y++)
{
for (int x = 0; x < screenWidth; x++)
{
sb.Append($"{colorBuffer[y, x]}{screenBuffer[y, x]}");
}
sb.Append(resetColor); // Reset color at end of line
if (y != screenHeight - 1)
{
sb.Append('\n');
}
}
// Output to console
try
{
Console.SetCursorPosition(0, 0);
Console.Write(sb.ToString());
}
catch (ArgumentOutOfRangeException)
{
// Handle potential exceptions when writing outside the buffer
}
}
using System;
using System.Collections.Generic;
using System.Text;
class Program
{
// Screen dimensions
const int screenWidth = 120;
const int screenHeight = 40;
// Player position and direction
static double posX = 0.0;
static double posY = 0.0;
static double dirX = 1.0;
static double dirY = 0.0;
// Camera plane (for field of view)
static double planeX = 0.0;
static double planeY = 0.66;
// Pitch variables
static double pitch = 0.0;
const double maxPitch = 45.0; // Maximum pitch in degrees
// Map dictionary
static Dictionary<(int, int), char> map = new Dictionary<(int, int), char>();
// UI flag
static bool showUI = true;
static void Main(string[] args)
{
InitializePlayerStartingArea();
RenderFrame();
Console.ReadKey();
}
static void InitializePlayerStartingArea()
{
int baseX = (int)Math.Floor(posX);
int baseY = (int)Math.Floor(posY);
Random random = new Random(1);
// Define the radius of the open area
int openRadius = 50; // Reduced for demonstration
// Clear the base area
for (int x = baseX - openRadius; x <= baseX + openRadius; x++)
{
for (int y = baseY - openRadius; y <= baseY + openRadius; y++)
{
map[(x, y)] = ' ';
}
}
// Add random walls
for (int x = baseX - openRadius + 1; x < baseX + openRadius; x++)
{
for (int y = baseY - openRadius + 1; y < baseY + openRadius; y++)
{
if (random.Next(100) < 30) // 30% chance of placing a wall
{
map[(x, y)] = '#';
}
}
}
// Add random 3x3 rooms
int numRooms = random.Next(3, 6); // 3 to 5 rooms
for (int i = 0; i < numRooms; i++)
{
int roomX = random.Next(baseX - openRadius + 2, baseX + openRadius - 2);
int roomY = random.Next(baseY - openRadius + 2, baseY + openRadius - 2);
for (int x = roomX - 1; x <= roomX + 1; x++)
{
for (int y = roomY - 1; y <= roomY + 1; y++)
{
map[(x, y)] = ' '; // Clear the room area
}
}
}
// **Add Random 'L's to the Map**
int numLights = random.Next(5, 15); // For example, 5 to 15 lights
for (int i = 0; i < numLights; i++)
{
int lightX, lightY;
// Ensure 'L' is placed on an open space
do
{
lightX = random.Next(baseX - openRadius + 2, baseX + openRadius - 2);
lightY = random.Next(baseY - openRadius + 2, baseY + openRadius - 2);
} while (map[(lightX, lightY)] != ' ');
map[(lightX, lightY)] = 'L';
}
// Ensure the player's starting position is clear
map[(baseX, baseY)] = ' ';
// Add walls around the perimeter
for (int x = baseX - openRadius - 1; x <= baseX + openRadius + 1; x++)
{
for (int y = baseY - openRadius - 1; y <= baseY + openRadius + 1; y++)
{
if (Math.Abs(x - baseX) == openRadius + 1 || Math.Abs(y - baseY) == openRadius + 1)
{
map[(x, y)] = '#';
}
}
}
}
static void RenderFrame()
{
StringBuilder sb = new StringBuilder();
// Initialize a 2D character array for the screen
char[,] screenBuffer = new char[screenHeight, screenWidth];
string[,] colorBuffer = new string[screenHeight, screenWidth];
// Fill screen buffer with default characters and colors
for (int y = 0; y < screenHeight; y++)
for (int x = 0; x < screenWidth; x++)
{
screenBuffer[y, x] = ' ';
colorBuffer[y, x] = "\u001b[0m"; // Reset color
}
// Calculate the vertical center shift based on pitch
// Positive pitch means looking up, so shift center down
// Negative pitch means looking down, so shift center up
// Define a maximum shift based on the pitch range
double pitchFactor = screenHeight / maxPitch;
int pitchShift = (int)(pitch * pitchFactor);
// **Store Light Positions**
var lightPositions = new List<(int mapX, int mapY)>();
// Raycasting
for (int x = 0; x < screenWidth; x++)
{
// Calculate ray position and direction
double cameraX = 2 * x / (double)screenWidth - 1; // x-coordinate in camera space
double rayDirX = dirX + planeX * cameraX;
double rayDirY = dirY + planeY * cameraX;
// Which box of the map we're in
int mapX = (int)Math.Floor(posX);
int mapY = (int)Math.Floor(posY);
// Length of ray from current position to next x or y-side
double sideDistX;
double sideDistY;
// Length of ray from one x or y-side to next x or y-side
double deltaDistX = (rayDirX == 0) ? 1e30 : Math.Abs(1 / rayDirX);
double deltaDistY = (rayDirY == 0) ? 1e30 : Math.Abs(1 / rayDirY);
double perpWallDist;
// What direction to step in x or y-direction (either +1 or -1)
int stepX, stepY;
bool hit = false; // Was a wall hit?
int side = 0; // Was the wall vertical or horizontal?
// Calculate step and initial sideDist
if (rayDirX < 0)
{
stepX = -1;
sideDistX = (posX - mapX) * deltaDistX;
}
else
{
stepX = 1;
sideDistX = (mapX + 1.0 - posX) * deltaDistX;
}
if (rayDirY < 0)
{
stepY = -1;
sideDistY = (posY - mapY) * deltaDistY;
}
else
{
stepY = 1;
sideDistY = (mapY + 1.0 - posY) * deltaDistY;
}
// Perform DDA
while (!hit)
{
// Jump to next map square, either in x-direction or y-direction
if (sideDistX < sideDistY)
{
sideDistX += deltaDistX;
mapX += stepX;
side = 0;
}
else
{
sideDistY += deltaDistY;
mapY += stepY;
side = 1;
}
// Check if ray has hit a wall
if (map.ContainsKey((mapX, mapY)))
{
if (map[(mapX, mapY)] == '#') hit = true;
if (map[(mapX, mapY)] == 'L')
{
// Optionally, treat 'L' as a wall or transparent
// For this example, we'll treat it as a wall
hit = true;
}
}
else
{
// Treat unknown areas as walls
hit = true;
}
}
// Calculate distance to the point of impact
if (side == 0)
perpWallDist = (sideDistX - deltaDistX);
else
perpWallDist = (sideDistY - deltaDistY);
// Calculate height of line to draw on screen
int lineHeight = (int)(screenHeight / perpWallDist);
// Calculate lowest and highest pixel to fill in current stripe
// **Modified to include pitchShift**
int drawStart = Math.Max(-lineHeight / 2 + screenHeight / 2 + pitchShift, 0);
int drawEnd = Math.Min(lineHeight / 2 + screenHeight / 2 + pitchShift, screenHeight - 1);
// **Store Light Positions if 'L' is hit**
if (map.ContainsKey((mapX, mapY)) && map[(mapX, mapY)] == 'L')
{
lightPositions.Add((mapX, mapY));
}
// **Wall Rendering (Simplified)**
for (int y = drawStart; y <= drawEnd; y++)
{
if (IsInScreenBounds(x, y))
{
colorBuffer[y, x] = "\u001b[38;2;158;154;86m"; // Base wall color (#9e9a56)
screenBuffer[y, x] = '█'; // Full block
}
}
// **Ceiling Rendering**
for (int y = 0; y < drawStart; y++)
{
if (IsInScreenBounds(x, y))
{
colorBuffer[y, x] = "\u001b[38;2;111;99;39m"; // Bright yellow
screenBuffer[y, x] = '█'; // Full block character
}
}
// **Floor Rendering**
for (int y = drawEnd + 1; y < screenHeight; y++)
{
if (IsInScreenBounds(x, y))
{
colorBuffer[y, x] = "\u001b[38;2;101;84;30m"; // Dark yellow
screenBuffer[y, x] = '█'; // Full block character
}
}
}
// **Render Lights on the Ceiling**
foreach (var (mapX, mapY) in lightPositions)
{
// Project the map position to screen coordinates
var (screenX, screenY) = ProjectToScreen(mapX, mapY);
// Define the size of the light (e.g., 2x2 blocks)
int lightSize = 2;
for (int ly = -lightSize / 2; ly <= lightSize / 2; ly++)
{
for (int lx = -lightSize / 2; lx <= lightSize / 2; lx++)
{
int px = screenX + lx;
int py = screenY + ly;
if (IsInScreenBounds(px, py))
{
// Blend light color with existing ceiling color
// For simplicity, we'll overwrite with white
colorBuffer[py, px] = "\u001b[38;2;255;255;255m"; // White color
screenBuffer[py, px] = '█'; // Full block character
}
}
}
}
if (showUI)
{
// Placeholder for RenderMinimap and RenderDebugInfo
// You can implement these methods as needed
}
// Build the final string with colors
const string resetColor = "\u001b[0m"; // ANSI reset code
for (int y = 0; y < screenHeight; y++)
{
for (int x = 0; x < screenWidth; x++)
{
sb.Append($"{colorBuffer[y, x]}{screenBuffer[y, x]}");
}
sb.Append(resetColor); // Reset color at end of line
if (y != screenHeight - 1)
{
sb.Append('\n');
}
}
// Output to console
try
{
Console.SetCursorPosition(0, 0);
Console.Write(sb.ToString());
}
catch (ArgumentOutOfRangeException)
{
// Handle potential exceptions when writing outside the buffer
}
}
/// <summary>
/// Projects a map position to screen coordinates based on player's position, direction, and pitch.
/// </summary>
static (int screenX, int screenY) ProjectToScreen(int mapX, int mapY)
{
// Calculate relative position to player
double dx = mapX - posX;
double dy = mapY - posY;
// Apply rotation based on player direction
double invDet = 1.0 / (planeX * dirY - dirX * planeY); // Required for correct matrix multiplication
double transformedX = invDet * (dirY * dx - dirX * dy);
double transformedY = invDet * (-planeY * dx + planeX * dy);
// Avoid division by zero
if (transformedY == 0) transformedY = 1e-6;
// Perspective projection
double screenPosX = (transformedX / transformedY) * (screenWidth / 2);
double screenPosY = (transformedX / transformedY) * (pitch / maxPitch) * (screenHeight / 2);
// Translate to screen coordinates
int screenX = (int)(screenWidth / 2 + screenPosX);
int screenY = (int)(screenHeight / 2 - screenPosY + (pitchShift));
return (screenX, screenY);
}
static bool IsInScreenBounds(int x, int y)
{
return x >= 0 && x < screenWidth && y >= 0 && y < screenHeight;
}
}