Skip to content

Commit

Permalink
Camera now follows the player and is zoomed in, world size is doubled.
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanlangston committed Dec 22, 2023
1 parent 8dcc4fa commit e8fe4ec
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
16 changes: 15 additions & 1 deletion src/Camera.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ const std = @import("std");
const raylib = @import("raylib");
const Shared = @import("./Shared.zig").Shared;

// This is assuming we scale with a locked aspect ratio
pub const Camera = struct {
camera2D: raylib.Camera2D,

pub fn initScaledCamera(targetScreenSize: raylib.Vector2) Camera {
// This is assuming we scale with a locked aspect ratio
const zoomScale: f32 = @as(f32, @floatFromInt(raylib.getScreenWidth())) / targetScreenSize.x;
return Camera{
.camera2D = raylib.Camera2D{
Expand All @@ -18,6 +18,20 @@ pub const Camera = struct {
};
}

pub fn initScaledTargetCamera(targetScreenSize: raylib.Vector2, scaleFactor: f32, target: raylib.Vector2) Camera {
const screenWidth: f32 = @as(f32, @floatFromInt(raylib.getScreenWidth()));
const screenHeight: f32 = @as(f32, @floatFromInt(raylib.getScreenHeight()));
const zoomScale: f32 = (screenWidth / targetScreenSize.x) * scaleFactor;
return Camera{
.camera2D = raylib.Camera2D{
.target = target,
.offset = raylib.Vector2.init(screenWidth / 2, screenHeight / 2),
.zoom = zoomScale,
.rotation = 0,
},
};
}

pub fn Draw(self: @This(), comptime T: type, drawFunction: *const fn () T) T {
raylib.beginMode2D(self.camera2D);
const result: T = drawFunction();
Expand Down
2 changes: 2 additions & 0 deletions src/Models/Meteor.zig
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ pub const Meteor = struct {
const spriteFrame = MeteorSprite.getSpriteFrame(@intFromFloat(self.frame));
const color: raylib.Color = if (self.active) self.color else raylib.Color.fade(self.color, 0.3);

if (self.position.x == -100 and self.position.y == -100) return;

raylib.drawTextureNPatch(
spriteFrame.Texture,
spriteFrame.NPatchInfo,
Expand Down
4 changes: 2 additions & 2 deletions src/ViewModels/AsteroidsViewModel.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ pub const AsteroidsViewModel = Shared.View.ViewModel.Create(
const METEORS_SPEED = 2;
const ANIMATION_SPEED_MOD = 15;

pub const MAX_BIG_METEORS = 4;
pub const MAX_BIG_METEORS = 8;
pub const MAX_MEDIUM_METEORS = MAX_BIG_METEORS * 2;
pub const MAX_SMALL_METEORS = MAX_MEDIUM_METEORS * 2;

// Variables
pub var gameOver = false;
pub var victory = false;

pub const screenSize: raylib.Vector2 = raylib.Vector2.init(1600, 900);
pub const screenSize: raylib.Vector2 = raylib.Vector2.init(3200, 1800);

pub var shipHeight: f32 = 0;

Expand Down
6 changes: 5 additions & 1 deletion src/Views/AsteroidsView.zig
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ fn DrawFunction() Shared.View.Views {
}

fn DrawWithCamera() Shared.View.Views {
const camera = Shared.Camera.initScaledCamera(vm.screenSize);
const camera = Shared.Camera.initScaledTargetCamera(
vm.screenSize,
3.5,
vm.player.position,
);
return camera.Draw(Shared.View.Views, &DrawFunction);
}

Expand Down

0 comments on commit e8fe4ec

Please sign in to comment.