diff --git a/src/Camera.zig b/src/Camera.zig index 7d9127e..9995fb6 100644 --- a/src/Camera.zig +++ b/src/Camera.zig @@ -36,4 +36,11 @@ pub const Camera = struct { const result: T = drawFunction(); return result; } + + pub fn DrawWithArg(self: @This(), comptime T: type, comptime A: type, drawFunction: *const fn (arg: A) T, arg: A) T { + raylib.beginMode2D(self.camera2D); + defer raylib.endMode2D(); + const result: T = drawFunction(arg); + return result; + } }; diff --git a/src/Models/Starscape.zig b/src/Models/Starscape.zig index fee9698..45e3569 100644 --- a/src/Models/Starscape.zig +++ b/src/Models/Starscape.zig @@ -100,8 +100,8 @@ pub const Starscape = struct { raylib.Rectangle.init( 0, 0, - -textWidthF, - -textHeightF, + textWidthF, + textHeightF, ), raylib.Rectangle.init( position.x - (textWidthF / 2), diff --git a/src/ViewModels/AsteroidsViewModel.zig b/src/ViewModels/AsteroidsViewModel.zig index 5a608e8..f3690b8 100644 --- a/src/ViewModels/AsteroidsViewModel.zig +++ b/src/ViewModels/AsteroidsViewModel.zig @@ -31,8 +31,11 @@ pub const AsteroidsViewModel = Shared.View.ViewModel.Create( pub const screenSize: raylib.Vector2 = raylib.Vector2.init(3200, 1800); - pub var shipHeight: f32 = 0; - pub var halfShipHeight: f32 = 0; + pub const shipHeight: f32 = (PLAYER_BASE_SIZE / 2) / @tan(std.math.degreesToRadians( + f32, + 20, + )); + pub const halfShipHeight: f32 = shipHeight / 2; pub var player: Player = undefined; pub var shoot: [PLAYER_MAX_SHOOTS]Shoot = undefined; @@ -58,7 +61,7 @@ pub const AsteroidsViewModel = Shared.View.ViewModel.Create( // Initialize game variables pub inline fn init() void { - starScape = Starscape.init(screenSize); + //starScape = Starscape.init(screenSize); Shared.Music.SetVolume(.BackgroundMusic, 0.35); @@ -70,12 +73,6 @@ pub const AsteroidsViewModel = Shared.View.ViewModel.Create( shieldLevel = MAX_SHIELD; nextShieldLevel = MAX_SHIELD; - shipHeight = (PLAYER_BASE_SIZE / 2) / @tan(std.math.degreesToRadians( - f32, - 20, - )); - halfShipHeight = shipHeight / 2; - player = Player.init(screenSize, shipHeight); score = 0; diff --git a/src/ViewModels/MenuViewModel.zig b/src/ViewModels/MenuViewModel.zig index 6798e91..0e6769d 100644 --- a/src/ViewModels/MenuViewModel.zig +++ b/src/ViewModels/MenuViewModel.zig @@ -2,6 +2,8 @@ const std = @import("std"); const Shared = @import("../Shared.zig").Shared; const Logger = @import("../Logger.zig").Logger; const raylib = @import("raylib"); +const Starscape = @import("../Models/Starscape.zig").Starscape; +const AsteroidsViewModel = @import("../ViewModels/AsteroidsViewModel.zig").AsteroidsViewModel; pub const Selection = enum { Start, @@ -10,6 +12,8 @@ pub const Selection = enum { None, }; +const AsteroidsVM = AsteroidsViewModel.GetVM(); + pub const MenuViewModel = Shared.View.ViewModel.Create( struct { pub var selection = Selection.Start; @@ -17,8 +21,13 @@ pub const MenuViewModel = Shared.View.ViewModel.Create( pub var frameCount: f32 = 0; + var starScape: Starscape = undefined; + pub inline fn init() void { frameCount = 0; + + starScape = Starscape.init(AsteroidsVM.screenSize); + AsteroidsVM.starScape = starScape; } pub inline fn GetSelectionText(select: Selection) [:0]const u8 { diff --git a/src/Views/AsteroidsView.zig b/src/Views/AsteroidsView.zig index 738f0e5..71b123e 100644 --- a/src/Views/AsteroidsView.zig +++ b/src/Views/AsteroidsView.zig @@ -69,11 +69,8 @@ fn DrawWithCamera() Shared.View.Views { const shakeAmount = screenWidth / 400; const target = if (vm.player.status == .collide) raylib.Vector2.init( vm.player.position.x - (if (Shared.Random.Get().boolean()) shakeAmount else -shakeAmount), - vm.player.position.y - vm.shipHeight - (if (Shared.Random.Get().boolean()) shakeAmount else -shakeAmount), - ) else raylib.Vector2.init( - vm.player.position.x, - vm.player.position.y - vm.shipHeight, - ); + vm.player.position.y - (if (Shared.Random.Get().boolean()) shakeAmount else -shakeAmount), + ) else vm.player.position; const camera = Shared.Camera.initScaledTargetCamera( vm.screenSize, screenSize, diff --git a/src/Views/MenuView.zig b/src/Views/MenuView.zig index a183a58..6ad0bfa 100644 --- a/src/Views/MenuView.zig +++ b/src/Views/MenuView.zig @@ -2,13 +2,29 @@ const std = @import("std"); const builtin = @import("builtin"); const raylib = @import("raylib"); const MenuViewModel = @import("../ViewModels/MenuViewModel.zig").MenuViewModel; +const AsteroidsViewModel = @import("../ViewModels/AsteroidsViewModel.zig").AsteroidsViewModel; const Selection = @import("../ViewModels/MenuViewModel.zig").Selection; const Shared = @import("../Shared.zig").Shared; const vm: type = MenuViewModel.GetVM(); +const AsteroidsVM = AsteroidsViewModel.GetVM(); + +pub fn Background(target: raylib.Vector2) void { + raylib.drawRectangleLinesEx( + raylib.Rectangle.init(0, 0, AsteroidsVM.screenSize.x, AsteroidsVM.screenSize.y), + 5, + Shared.Color.Green.Light, + ); + + AsteroidsVM.starScape.Draw( + AsteroidsVM.screenSize.x, + AsteroidsVM.screenSize.y, + target, + ); +} pub fn DrawFunction() Shared.View.Views { - raylib.clearBackground(Shared.Color.Gray.Base); + raylib.clearBackground(Shared.Color.Tone.Dark); Shared.Music.Play(.TitleScreenMusic); @@ -16,10 +32,23 @@ pub fn DrawFunction() Shared.View.Views { const font = Shared.Font.Get(.Unknown); const title = locale.Title; - const screenWidth = raylib.getScreenWidth(); - const screenHeight = raylib.getScreenHeight(); - const fontSize = @divFloor(screenWidth, 20); - const startY = @divFloor(screenHeight, 4); + const screenWidth: f32 = @floatFromInt(raylib.getScreenWidth()); + const screenHeight: f32 = @floatFromInt(raylib.getScreenHeight()); + const fontSize = screenWidth / 20; + const startY = screenHeight / 4; + + const playerPosition = raylib.Vector2.init( + AsteroidsVM.screenSize.x / 2, + (AsteroidsVM.screenSize.y - AsteroidsVM.shipHeight) / 2, + ); + + const camera = Shared.Camera.initScaledTargetCamera( + AsteroidsVM.screenSize, + raylib.Vector2.init(screenWidth, screenHeight), + 3.5, + playerPosition, + ); + camera.DrawWithArg(void, raylib.Vector2, Background, playerPosition); const foregroundColor = Shared.Color.Blue.Base; const backgroundColor = Shared.Color.Blue.Light.alpha(0.75); @@ -31,7 +60,7 @@ pub fn DrawFunction() Shared.View.Views { const TitleTextSize = raylib.measureTextEx( titleFont, title, - @as(f32, @floatFromInt(fontSize)) * 2.5, + fontSize * 2.5, @floatFromInt(font.glyphPadding), ); const titleFontsizeF: f32 = TitleTextSize.y; @@ -39,8 +68,8 @@ pub fn DrawFunction() Shared.View.Views { titleFont, title, raylib.Vector2.init( - ((@as(f32, @floatFromInt(screenWidth)) - TitleTextSize.x) / 2), - @as(f32, @floatFromInt(startY)) - (titleFontsizeF / 2), + ((screenWidth - TitleTextSize.x) / 2), + startY - (titleFontsizeF / 2), ), titleFontsizeF, @floatFromInt(titleFont.glyphPadding), @@ -58,11 +87,11 @@ pub fn DrawFunction() Shared.View.Views { var index: usize = 0; for (std.enums.values(Selection)) |select| { const text = vm.GetSelectionText(select); - const i: i8 = @intCast(index); - const x: f32 = @floatFromInt(@divFloor(screenWidth, 2) - @divFloor(screenWidth, 3)); - const y: f32 = @floatFromInt(startY + (fontSize * 3) + (fontSize + 32) * i); - const width: f32 = @floatFromInt(@divFloor(screenWidth, 3) * 2); - const height: f32 = @floatFromInt(fontSize + 16); + const i: f32 = @floatFromInt(index); + const x: f32 = (screenWidth / 2) - (screenWidth / 3); + const y: f32 = startY + (fontSize * 3) + (fontSize + 32) * i; + const width: f32 = (screenWidth / 3) * 2; + const height: f32 = fontSize + 16; vm.Rectangles[index] = raylib.Rectangle.init( x, y, @@ -72,7 +101,7 @@ pub fn DrawFunction() Shared.View.Views { const selected = select == vm.selection; - const TextSize = raylib.measureTextEx(font, text, @floatFromInt(fontSize), @floatFromInt(font.glyphPadding)); + const TextSize = raylib.measureTextEx(font, text, fontSize, @floatFromInt(font.glyphPadding)); const fontsizeF: f32 = TextSize.y; vm.frameCount += raylib.getFrameTime();