Skip to content

Commit

Permalink
bug fixes
Browse files Browse the repository at this point in the history
fixed that 2 float components hasn't been used as camera spawn position
world name reader only reads first line now
  • Loading branch information
danilwhale committed Mar 11, 2023
1 parent fdf2b94 commit 5db4c6d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 15 deletions.
19 changes: 9 additions & 10 deletions GameScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class GameScreen : Screen

static byte _currentType = 1;

Camera2D _camera;
public Camera2D camera;
bool _cameraRunning;

World _world = null!;
Expand All @@ -23,7 +23,7 @@ public class GameScreen : Screen

public override void Draw()
{
BeginMode2D(_camera);
BeginMode2D(camera);
ClearBackground(Settings.SkyColor);

DrawRectangleLinesEx(
Expand All @@ -41,10 +41,10 @@ public override void Initialize()
{
_world = new World();

_camera = new Camera2D
camera = new Camera2D
{
offset = new Vector2(Program.WIDTH / 2, Program.HEIGHT / 2),
target = new Vector2(GetRandomValue(0, World.CHUNK_AREA * Chunk.SIZE * 48), GetRandomValue(0, World.CHUNK_AREA * Chunk.SIZE * 48)),
target = new Vector2(GetRandomValue(0, World.CHUNK_AREA * Chunk.SIZE * 48)),
zoom = 1,
rotation = 0
};
Expand All @@ -60,12 +60,11 @@ public override void Update()
float vx = IsKeyDown(KeyboardKey.KEY_D) - IsKeyDown(KeyboardKey.KEY_A);
float vy = IsKeyDown(KeyboardKey.KEY_S) - IsKeyDown(KeyboardKey.KEY_W);

_camera.target += new Vector2(vx, vy) * (_cameraRunning ? CAMERA_SPEED * 2 : CAMERA_SPEED);
_world.SpawnPos = _camera.target;
camera.target += new Vector2(vx, vy) * (_cameraRunning ? CAMERA_SPEED * 2 : CAMERA_SPEED);
#endregion

#region Tile Placement
Vector2 wmPos = GetScreenToWorld2D(GetMousePosition(), _camera);
Vector2 wmPos = GetScreenToWorld2D(GetMousePosition(), camera);

mx = (int)(Math.Round((float)((wmPos.X) / 48)));
my = (int)(Math.Round((float)((wmPos.Y) / 48)));
Expand Down Expand Up @@ -104,10 +103,10 @@ public override void Update()
var mdelta = Vector2Clamp(GetMouseWheelMoveV(), -Vector2.One, Vector2.One);
if (IsKeyDown(KeyboardKey.KEY_LEFT_CONTROL))
{
_camera.zoom += ((float)GetMouseWheelMove() * 0.05f);
camera.zoom += ((float)GetMouseWheelMove() * 0.05f);

if (_camera.zoom > 3.0f) _camera.zoom = 3.0f;
else if (_camera.zoom < 0.1f) _camera.zoom = 0.1f;
if (camera.zoom > 3.0f) camera.zoom = 3.0f;
else if (camera.zoom < 0.1f) camera.zoom = 0.1f;

}
#endregion
Expand Down
7 changes: 3 additions & 4 deletions World.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ public class World
public string WorldFile { get; set; } = "level.dat";
public const int CHUNK_AREA = 16;
public Chunk[,] Chunks { get; private set; } = null!;
public Vector2 SpawnPos { get; set; }

public World()
{
Expand Down Expand Up @@ -230,8 +229,8 @@ public void Save()

bw.Write(WORLD_SAVE_HEADER);

bw.Write(SpawnPos.X);
bw.Write(SpawnPos.Y);
bw.Write(Program.gameScreen.camera.target.X);
bw.Write(Program.gameScreen.camera.target.Y);

for (int x = 0; x < World.CHUNK_AREA * Chunk.SIZE; x++)
{
Expand Down Expand Up @@ -298,7 +297,7 @@ public void Load(string path = "level.dat")
else if (header != WORLD_SAVE_HEADER) throw new IOException("world header is invalid");
else
{
SpawnPos = new Vector2(br.ReadSingle(), br.ReadSingle());
Program.gameScreen.camera.target = new Vector2(br.ReadSingle(), br.ReadSingle());
for (int x = 0; x < World.CHUNK_AREA * Chunk.SIZE; x++)
{
for (int y = 0; y < World.CHUNK_AREA * Chunk.SIZE; y++)
Expand Down
2 changes: 1 addition & 1 deletion WorldSelectScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private string FetchWorldName(int i)
if (IsExistsWorld(i))
{
if (File.Exists("saves/" + i + "/info.txt"))
return File.ReadAllText("saves/" + i + "/info.txt").Replace("\n", "");
return File.ReadAllLines("saves/" + i + "/info.txt").First().Replace("\n", "");
else
return "world #" + (i + 1);
}
Expand Down

0 comments on commit 5db4c6d

Please sign in to comment.