From 8a951ff412e4b4e51f4e6a1ce6dea43ef52d49e8 Mon Sep 17 00:00:00 2001 From: Dylan Langston Date: Sun, 10 Dec 2023 15:00:56 -0500 Subject: [PATCH] Add Draw functions to Models --- src/Models/Meteor.zig | 28 +++++++++++ src/Models/Player.zig | 22 +++++++++ src/Models/Shoot.zig | 10 ++++ src/ViewModels/AsteroidsViewModel.zig | 20 ++++++++ src/Views/AsteroidsView.zig | 67 ++------------------------- 5 files changed, 85 insertions(+), 62 deletions(-) diff --git a/src/Models/Meteor.zig b/src/Models/Meteor.zig index 55a7468..2848332 100644 --- a/src/Models/Meteor.zig +++ b/src/Models/Meteor.zig @@ -1,9 +1,37 @@ +const std = @import("std"); const raylib = @import("raylib"); +const Shared = @import("../Shared.zig").Shared; + +pub const MeteorSprite = Shared.Sprite.init(5, .Meteor); pub const Meteor = struct { position: raylib.Vector2, speed: raylib.Vector2, radius: f32, + rotation: f32, active: bool, color: raylib.Color, + frame: f32, + + pub fn Draw(self: @This()) void { + const spriteFrame = MeteorSprite.getSpriteFrame(@intFromFloat(self.frame)); + const color: raylib.Color = if (self.active) self.color else raylib.Color.fade(self.color, 0.3); + + raylib.drawTextureNPatch( + spriteFrame.Texture, + spriteFrame.NPatchInfo, + raylib.Rectangle.init( + self.position.x, + self.position.y, + self.radius * 2, + self.radius * 2, + ), + raylib.Vector2.init( + self.radius, + self.radius, + ), + self.rotation * 365, + color, + ); + } }; diff --git a/src/Models/Player.zig b/src/Models/Player.zig index ba915fc..83e42fd 100644 --- a/src/Models/Player.zig +++ b/src/Models/Player.zig @@ -1,3 +1,4 @@ +const std = @import("std"); const raylib = @import("raylib"); pub const Player = struct { @@ -7,4 +8,25 @@ pub const Player = struct { rotation: f32, collider: raylib.Vector3, color: raylib.Color, + + pub fn Draw(self: @This(), shipHeight: f32, base_size: f32) void { + const v1 = raylib.Vector2.init( + self.position.x + @sin(std.math.degreesToRadians(f32, self.rotation)) * (shipHeight), + self.position.y - @cos(std.math.degreesToRadians(f32, self.rotation)) * (shipHeight), + ); + const v2 = raylib.Vector2.init( + self.position.x - @cos(std.math.degreesToRadians(f32, self.rotation)) * (base_size / 2), + self.position.y - @sin(std.math.degreesToRadians(f32, self.rotation)) * (base_size / 2), + ); + const v3 = raylib.Vector2.init( + self.position.x + @cos(std.math.degreesToRadians(f32, self.rotation)) * (base_size / 2), + self.position.y + @sin(std.math.degreesToRadians(f32, self.rotation)) * (base_size / 2), + ); + raylib.drawTriangle( + v1, + v2, + v3, + self.color, + ); + } }; diff --git a/src/Models/Shoot.zig b/src/Models/Shoot.zig index eae52ca..5f59f63 100644 --- a/src/Models/Shoot.zig +++ b/src/Models/Shoot.zig @@ -8,4 +8,14 @@ pub const Shoot = struct { lifeSpawn: i8, active: bool, color: raylib.Color, + + pub fn Draw(self: @This()) void { + if (self.active) { + raylib.drawCircleV( + self.position, + self.radius, + self.color, + ); + } + } }; diff --git a/src/ViewModels/AsteroidsViewModel.zig b/src/ViewModels/AsteroidsViewModel.zig index 13c41ca..03806f3 100644 --- a/src/ViewModels/AsteroidsViewModel.zig +++ b/src/ViewModels/AsteroidsViewModel.zig @@ -2,6 +2,7 @@ const std = @import("std"); const Shared = @import("../Shared.zig").Shared; const raylib = @import("raylib"); const Meteor = @import("../Models/Meteor.zig").Meteor; +const MeteorSprite = @import("../Models/Meteor.zig").MeteorSprite; const Player = @import("../Models/Player.zig").Player; const Shoot = @import("../Models/Shoot.zig").Shoot; @@ -13,6 +14,7 @@ pub const AsteroidsViewModel = Shared.View.ViewModel.Create( pub const PLAYER_MAX_SHOOTS: i32 = 10; const METEORS_SPEED = 2; + const ANIMATION_SPEED_MOD = 15; pub const MAX_BIG_METEORS = 4; pub const MAX_MEDIUM_METEORS = MAX_BIG_METEORS * 2; @@ -152,8 +154,10 @@ pub const AsteroidsViewModel = Shared.View.ViewModel.Create( vely, ), .radius = 40, + .rotation = Shared.Random.Get().float(f32), .active = true, .color = Shared.Color.Blue.Base, + .frame = 0, }; } @@ -169,8 +173,10 @@ pub const AsteroidsViewModel = Shared.View.ViewModel.Create( 0, ), .radius = 20, + .rotation = Shared.Random.Get().float(f32), .active = false, .color = Shared.Color.Blue.Base, + .frame = 0, }; } @@ -186,8 +192,10 @@ pub const AsteroidsViewModel = Shared.View.ViewModel.Create( 0, ), .radius = 10, + .rotation = Shared.Random.Get().float(f32), .active = false, .color = Shared.Color.Blue.Base, + .frame = 0, }; } @@ -368,6 +376,8 @@ pub const AsteroidsViewModel = Shared.View.ViewModel.Create( // Meteors logic: big meteors for (0..MAX_BIG_METEORS) |i| { if (bigMeteors[i].active) { + bigMeteors[i].frame = 0; + // Movement bigMeteors[i].position.x += bigMeteors[i].speed.x; bigMeteors[i].position.y += bigMeteors[i].speed.y; @@ -383,12 +393,16 @@ pub const AsteroidsViewModel = Shared.View.ViewModel.Create( } else if (bigMeteors[i].position.y < 0 - bigMeteors[i].radius) { bigMeteors[i].position.y = screenSize.height + bigMeteors[i].radius; } + } else if (bigMeteors[i].frame < MeteorSprite.Frames - 1) { + bigMeteors[i].frame += raylib.getFrameTime() * ANIMATION_SPEED_MOD; } } // Meteors logic: medium meteors for (0..MAX_MEDIUM_METEORS) |i| { if (mediumMeteors[i].active) { + mediumMeteors[i].frame = 0; + // Movement mediumMeteors[i].position.x += mediumMeteors[i].speed.x; mediumMeteors[i].position.y += mediumMeteors[i].speed.y; @@ -404,12 +418,16 @@ pub const AsteroidsViewModel = Shared.View.ViewModel.Create( } else if (mediumMeteors[i].position.y < 0 - mediumMeteors[i].radius) { mediumMeteors[i].position.y = screenSize.height + mediumMeteors[i].radius; } + } else if (mediumMeteors[i].frame < MeteorSprite.Frames - 1) { + mediumMeteors[i].frame += raylib.getFrameTime() * ANIMATION_SPEED_MOD; } } // Meteors logic: small meteors for (0..MAX_SMALL_METEORS) |i| { if (smallMeteors[i].active) { + smallMeteors[i].frame = 0; + // Movement smallMeteors[i].position.x += smallMeteors[i].speed.x; smallMeteors[i].position.y += smallMeteors[i].speed.y; @@ -425,6 +443,8 @@ pub const AsteroidsViewModel = Shared.View.ViewModel.Create( } else if (smallMeteors[i].position.y < 0 - smallMeteors[i].radius) { smallMeteors[i].position.y = screenSize.height + smallMeteors[i].radius; } + } else if (smallMeteors[i].frame < MeteorSprite.Frames - 1) { + smallMeteors[i].frame += raylib.getFrameTime() * ANIMATION_SPEED_MOD; } } diff --git a/src/Views/AsteroidsView.zig b/src/Views/AsteroidsView.zig index 9eff18f..e1b5552 100644 --- a/src/Views/AsteroidsView.zig +++ b/src/Views/AsteroidsView.zig @@ -16,81 +16,24 @@ fn DrawFunction() Shared.View.Views { vm.Update(); // Draw spaceship - const v1 = raylib.Vector2.init( - vm.player.position.x + @sin(std.math.degreesToRadians(f32, vm.player.rotation)) * (vm.shipHeight), - vm.player.position.y - @cos(std.math.degreesToRadians(f32, vm.player.rotation)) * (vm.shipHeight), - ); - const v2 = raylib.Vector2.init( - vm.player.position.x - @cos(std.math.degreesToRadians(f32, vm.player.rotation)) * (vm.PLAYER_BASE_SIZE / 2), - vm.player.position.y - @sin(std.math.degreesToRadians(f32, vm.player.rotation)) * (vm.PLAYER_BASE_SIZE / 2), - ); - const v3 = raylib.Vector2.init( - vm.player.position.x + @cos(std.math.degreesToRadians(f32, vm.player.rotation)) * (vm.PLAYER_BASE_SIZE / 2), - vm.player.position.y + @sin(std.math.degreesToRadians(f32, vm.player.rotation)) * (vm.PLAYER_BASE_SIZE / 2), - ); - raylib.drawTriangle( - v1, - v2, - v3, - vm.player.color, - ); + vm.player.Draw(vm.shipHeight, vm.PLAYER_BASE_SIZE); // Draw meteors for (0..vm.MAX_BIG_METEORS) |i| { - if (vm.bigMeteors[i].active) { - raylib.drawCircleV( - vm.bigMeteors[i].position, - vm.bigMeteors[i].radius, - vm.bigMeteors[i].color, - ); - } else raylib.drawCircleV( - vm.bigMeteors[i].position, - vm.bigMeteors[i].radius, - raylib.Color.fade(vm.bigMeteors[i].color, 0.3), - ); + vm.bigMeteors[i].Draw(); } for (0..vm.MAX_MEDIUM_METEORS) |i| { - if (vm.mediumMeteors[i].active) { - raylib.drawCircleV( - vm.mediumMeteors[i].position, - vm.mediumMeteors[i].radius, - vm.mediumMeteors[i].color, - ); - } else { - raylib.drawCircleV( - vm.mediumMeteors[i].position, - vm.mediumMeteors[i].radius, - raylib.Color.fade(vm.mediumMeteors[i].color, 0.3), - ); - } + vm.mediumMeteors[i].Draw(); } for (0..vm.MAX_SMALL_METEORS) |i| { - if (vm.smallMeteors[i].active) { - raylib.drawCircleV( - vm.smallMeteors[i].position, - vm.smallMeteors[i].radius, - vm.smallMeteors[i].color, - ); - } else { - raylib.drawCircleV( - vm.smallMeteors[i].position, - vm.smallMeteors[i].radius, - raylib.Color.fade(vm.smallMeteors[i].color, 0.3), - ); - } + vm.smallMeteors[i].Draw(); } // Draw shoot for (0..vm.PLAYER_MAX_SHOOTS) |i| { - if (vm.shoot[i].active) { - raylib.drawCircleV( - vm.shoot[i].position, - vm.shoot[i].radius, - vm.shoot[i].color, - ); - } + vm.shoot[i].Draw(); } if (vm.victory) Shared.Helpers.DrawTextCentered(