Player Object

This commit is contained in:
2026-02-13 14:54:30 +00:00
parent b0511f3830
commit f617f799c2
3 changed files with 231 additions and 91 deletions

View File

@@ -80,13 +80,17 @@ namespace VoxelIsometricRenderer
RelativeBlockZ >= 0 && RelativeBlockZ < LightLevels.GetLength(2))
{
LightLevel = LightLevels[RelativeBlockX,RelativeBlockY,RelativeBlockZ];
float DistanceX = RelativeBlockX - CentreX;
float DistanceZ = RelativeBlockZ - CentreZ;
double CorrectLightLevel =Math.Atan2(RelativeBlockX - CentreX, RelativeBlockZ - CentreZ);
int DirectionX = 0;
if (BlocksLight)
{
switch (face)
{
case 0:
if (CentreZ < RelativeBlockZ)
CorrectLightLevel += (Math.PI * 0 / 180);
if (CentreZ > RelativeBlockZ)
{
DirectionX = 180;
}
@@ -94,13 +98,14 @@ namespace VoxelIsometricRenderer
{
DirectionX = 90;
}
if (CentreZ > RelativeBlockZ)
if (CentreZ < RelativeBlockZ)
{
DirectionX = 0;
}
break;
case 1:
if (CentreZ > RelativeBlockZ)
CorrectLightLevel += (Math.PI * 180 / 180);
if (CentreZ < RelativeBlockZ)
{
DirectionX = 180;
}
@@ -108,12 +113,13 @@ namespace VoxelIsometricRenderer
{
DirectionX = 90;
}
if (CentreZ < RelativeBlockZ)
if (CentreZ > RelativeBlockZ)
{
DirectionX = 0;
}
break;
case 2:
CorrectLightLevel += (Math.PI * 90 / 180);
if (CentreX < RelativeBlockX)
{
DirectionX = 180;
@@ -128,6 +134,7 @@ namespace VoxelIsometricRenderer
}
break;
case 3:
CorrectLightLevel += (Math.PI * 270 / 180);
if (CentreX > RelativeBlockX)
{
DirectionX = 180;
@@ -170,37 +177,38 @@ namespace VoxelIsometricRenderer
}
break;
}
/* if (CentreZ < RelativeBlockZ && RelativeBlockZ > RelativeBlockX)
{
for (int i = RelativeBlockZ; i < LightLevels.GetLength(2); i++)
{
LightLevels[RelativeBlockX, RelativeBlockY, i] = 0;
}
} else if (CentreZ > RelativeBlockZ && RelativeBlockZ < RelativeBlockX)
{
for (int i = RelativeBlockZ; i >= 0; i--)
{
LightLevels[RelativeBlockX, RelativeBlockY, i] = 0;
}
} else
{
if (CentreX < RelativeBlockX && RelativeBlockX > RelativeBlockZ)
{
for (int i = RelativeBlockX; i < LightLevels.GetLength(0); i++)
{
LightLevels[i, RelativeBlockY, RelativeBlockZ] = 0;
}
}
else if (CentreX > RelativeBlockX && RelativeBlockX < RelativeBlockZ)
{
for (int i = RelativeBlockZ; i >= 0; i--)
{
LightLevels[i, RelativeBlockY, RelativeBlockZ] = 0;
}
}
}*/
LightLevel = (int)(LightLevel * ((1 + Math.Cos(Math.PI * DirectionX / 180)) / 2));
// MessageBox.Show(LightLevel + "");
/* if (CentreZ < RelativeBlockZ && RelativeBlockZ > RelativeBlockX)
{
for (int i = RelativeBlockZ; i < LightLevels.GetLength(2); i++)
{
LightLevels[RelativeBlockX, RelativeBlockY, i] = 0;
}
} else if (CentreZ > RelativeBlockZ && RelativeBlockZ < RelativeBlockX)
{
for (int i = RelativeBlockZ; i >= 0; i--)
{
LightLevels[RelativeBlockX, RelativeBlockY, i] = 0;
}
} else
{
if (CentreX < RelativeBlockX && RelativeBlockX > RelativeBlockZ)
{
for (int i = RelativeBlockX; i < LightLevels.GetLength(0); i++)
{
LightLevels[i, RelativeBlockY, RelativeBlockZ] = 0;
}
}
else if (CentreX > RelativeBlockX && RelativeBlockX < RelativeBlockZ)
{
for (int i = RelativeBlockZ; i >= 0; i--)
{
LightLevels[i, RelativeBlockY, RelativeBlockZ] = 0;
}
}
}*/
LightLevel = face > 3? (int)(LightLevel * ((1 + Math.Cos(Math.PI * DirectionX / 180)) / 2)) : (int)(LightLevel * ((1 + Math.Cos(CorrectLightLevel)) / 2));
//LightLevel = (int)(LightLevel * ((1 + Math.Cos(Math.PI * DirectionX / 180)) / 2));
// MessageBox.Show(LightLevel + "");
}
}
return LightLevel;
@@ -229,14 +237,16 @@ namespace VoxelIsometricRenderer
{
int[] Distances = new int[] { CentreX - PosX, CentreY - PosY, CentreZ - PosZ};
int[] Centres = new int[] { CentreX, CentreY, CentreZ };
float[] LightLevel = new float[] {255.0f/ RangeX * (MaxLevel / 255.0f), 255.0f/ RangeY * (MaxLevel / 255.0f), 255.0f/ RangeZ * (MaxLevel / 255.0f) };
float[] LightLevel = new float[] {(255.0f/ RangeX) * (MaxLevel / 255.0f), (255.0f/ RangeY) * (MaxLevel / 255.0f), (255.0f/ RangeZ) * (MaxLevel / 255.0f) };
float TotalLightLevel = 0;
int Dimensions = 0;
for (int i = 0; i < 3; i++)
{
Distances[i] = Distances[i] > 0 ? Distances[i] : -Distances[i];
}
LightLevel = new float[] { LightLevel[0] * (float)Distances[0], LightLevel[1] * (float)Distances[1], LightLevel[2] * (float)Distances[2] };
//if (PosY < CentreY && PosX == CentreX && PosZ == CentreZ) { MessageBox.Show(Distances[1] + ""); }
//if (RangeX >= Distances[0] && PosY == CentreX && PosZ == CentreZ) MessageBox.Show(String.Format("LightLevel: {0}\nRange:{1}\nDistance:{2}\nRange/Distance:{3}", LightLevel[0], RangeX, Distances[0], RangeX / (float)Distances[0]));
LightLevel = new float[] { RangeX >= Distances[0] ? LightLevel[0] * (RangeX / ((float)Distances[0] > 0 ? (float)Distances[0] : 1)) : 0, RangeY >= Distances[1] ? LightLevel[1] * (RangeY / ((float)Distances[1] > 0 ? (float)Distances[1] : 1)) : 0, RangeZ >= Distances[2] ? LightLevel[2] * (RangeZ / ((float)Distances[2] > 0 ? (float)Distances[2] : 1)) : 0 };
for (int i = 0; i < 3; i++)
{
if (Centres[i] >= 0)
@@ -343,7 +353,7 @@ namespace VoxelIsometricRenderer
int DistX = WorldX - (Light.GetPosition()[0] - Light.GetRadius()[0]);
int DistY = WorldY - (Light.GetPosition()[1] - Light.GetRadius()[1]);
int DistZ = WorldZ - (Light.GetPosition()[2] - Light.GetRadius()[2]);
if (DistX > 0 && DistY > 0 && DistZ > 0 && DistX - Light.GetRadius()[0] < 0 && DistY - Light.GetRadius()[1] < 0 && DistZ - Light.GetRadius()[2] < 0)
if (DistX >= 0 && DistY >= 0 && DistZ >= 0 && DistX - Light.GetRadius()[0] <= 0 && DistY - Light.GetRadius()[1] <= 0 && DistZ - Light.GetRadius()[2] <= 0)
{
//MessageBox.Show("X: " + DistX + " Y: " + DistY + " Z: " + DistZ);
float[] LightRGB = new float[3];
@@ -356,7 +366,6 @@ namespace VoxelIsometricRenderer
}
});
//return RGBLevels;
return new float[] { RGBLevels[0] != 0 ? RGBLevels[0]/LightCount : 0, RGBLevels[1] != 0 ? RGBLevels[1]/LightCount : 0, RGBLevels[2] != 0 ? RGBLevels[2]/LightCount : 0 };
}
public static void UpdateLighting()
@@ -367,11 +376,11 @@ namespace VoxelIsometricRenderer
if (sunAngles[0] > 360) sunAngles[0] -= 360;
if (sunAngles[1] > 360) sunAngles[1] -= 360;
if (sunAngles[0] <= -360) sunAngles[0] += 360;
if (sunAngles[1] <= -360) sunAngles[1] += 360;
if (sunAngles[1] <= -360) sunAngles[1] += 360;
float CurrentAngle = 0;
int AngleIndex = 0;
float MoonAngle = (float)((1 + Math.Sin(Math.PI * (sunAngles[1] - 60) / 180)) / 2.0);
float SunLightAngle = (float)((1 + Math.Cos(Math.PI * sunAngles[1] / 180)) / 1.8);
float MoonAngle = (float)((1 + Math.Sin(Math.PI * (sunAngles[1] - 90) / 180)) / 2.0);
float SunLightAngle = (float)((1 + Math.Cos(Math.PI * sunAngles[1] / 180)) / 2.0);
for (int i = 0; i < AngleRanges.Length; i++)
{
if (sunAngles[0] < AngleRanges[i])
@@ -392,7 +401,7 @@ namespace VoxelIsometricRenderer
}
for (int i = SkyLightModifiedValues.Length - 2; i < SkyLightModifiedValues.Length; i++)
{
SkyLightModifiedValues[i] = (float)Math.Min(Math.Max(SunLightAngle, MoonAngle/2) + (MoonAngle/ 4),MaxBloom);
SkyLightModifiedValues[i] = (float)Math.Min(SunLightAngle + (MoonAngle/ 2),MaxBloom);
}
for (int i = 0; i < AmbientColouring.Length; i++)
{
@@ -401,7 +410,7 @@ namespace VoxelIsometricRenderer
}
AmbientColouring[2] -= sunAngles[1] > 180 || sunAngles[1] < 90 ? Square(Square((float)(Math.Max(Math.Cos(Math.PI * (sunAngles[1] + 45) / 180), 0)))) / 2.0f : 0;//(float)(Math.Max(Math.Cos(Math.PI * (sunAngles[1] + 45) / 180),0));
AmbientColouring[1] -= sunAngles[1] > 180 || sunAngles[1] < 90 ? Square(Square((float)(Math.Max(Math.Cos(Math.PI * (sunAngles[1] + 45) / 180), 0))) / 3.0f) : 0;// (float)(Math.Max(Math.Cos(Math.PI * (sunAngles[1] + 45) / 180),0))/2.0f;
SkyLightMetrics[0] = (float)Math.Max(SunLightAngle, 0.1f) + (float)(MoonAngle / 5);
SkyLightMetrics[0] = (float)SunLightAngle + (float)(MoonAngle / 5);
SkyLightMetrics[2] = MoonAngle;
}
public static float Square(float ValueIn)
@@ -415,6 +424,8 @@ namespace VoxelIsometricRenderer
public static List<Bitmap> PreCompiledModels = new List<Bitmap>();
public static Bitmap[,] GeneratedFaceTextures = new Bitmap[0,0];
public static int VoxelCount = 0;
public static int VoxelSize = 32;
public static float RenderScale = 2.0f;
public static Bitmap GetModel(int modelNum)
{
return PreCompiledModels[modelNum];
@@ -447,8 +458,7 @@ namespace VoxelIsometricRenderer
}
public static float GetShadingValue(float LightValue, int RGBIndex, int FaceIndex, float[] Lights)
{
//if (Lights[RGBIndex] > 0) MessageBox.Show(Lights[RGBIndex] + "");
return ((float)Math.Max(LightValue * LightingManager.AmbientColouring[RGBIndex], LightingManager.AmbientMinimums[FaceIndex])) + Lights[RGBIndex] * 10;
return ((float)Math.Max(LightValue * LightingManager.AmbientColouring[RGBIndex], LightingManager.AmbientMinimums[FaceIndex])) + Lights[RGBIndex];
}
public static ImageAttributes GetShadow(int face) { return GetShadow(face, 0, 0,0,0, false); }
public static ImageAttributes GetShadow(int face, int WorldX, int WorldY, int WorldZ, int ShadowLevel, bool BlocksLight)
@@ -1191,15 +1201,20 @@ namespace VoxelIsometricRenderer
if (!block_pos_lookup.Contains(pos_lookup)) return -1;
return block_pos_lookup.IndexOf(pos_lookup);
}
public static int[] GetDrawLocation(int x, int y, int z, int offsetX, int offsetY, int offsetZ)
public static int[] GetDrawLocation(int x, int y, int z, float offsetX, float offsetY, float offsetZ)
{
return new int[]{(int)((((double)(x + offsetX) * 0.5) + ((double)(z + offsetZ) * -0.5))*(double)BlockSize),
(int)((((double)(x + offsetX) * 0.5) + ((double)(z + offsetZ) * 0.5) - (-offsetY + (y)))*((double)BlockSize/2.0)) };
}
public static void RenderBlock(Graphics g, int BlockID,int WorldX, int WorldY, int WorldZ, int ShadowLevel)
{
int[] location = GetDrawLocation(WorldX, WorldY, WorldZ, PlayerObject.GetPos()[0], PlayerObject.GetPos()[1], PlayerObject.GetPos()[2]);
VoxelRegistry.FetchVoxel(blocks[BlockID].FetchVoxelID()).Draw(g, location[0] + (Terrain4.width/4) , location[1] + (Terrain4.height/2), WorldX, WorldY, WorldZ, ShadowLevel, FetchBlockState(BlockID).hasBlocksLight());
RenderBlock(g, BlockID, WorldX, WorldY, WorldZ, ShadowLevel, false);
}
public static void RenderBlock(Graphics g, int BlockID,int WorldX, int WorldY, int WorldZ, int ShadowLevel, bool player)
{
int[] locationWOffset = GetDrawLocation(WorldX, WorldY, WorldZ, PlayerObject.GetCameraPos()[0], PlayerObject.GetCameraPos()[1], PlayerObject.GetCameraPos()[2]);
int [] location = GetDrawLocation(WorldX, WorldY, WorldZ, 0,0,0);
VoxelRegistry.FetchVoxel(blocks[BlockID].FetchVoxelID()).Draw(g, (player? location[0] : locationWOffset[0])+ (int)(Terrain4.width / (2 * TextureManager.RenderScale)), (player ? location[1] : locationWOffset[1]) + (int)(Terrain4.height / (2 * TextureManager.RenderScale)), WorldX, WorldY, WorldZ, ShadowLevel, FetchBlockState(BlockID).hasBlocksLight());
}
public static int CheckInBlock(Block block,int x, int y, int z)
{