Skip to content

Commit

Permalink
Star fine tuning
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanlangston committed Dec 25, 2023
1 parent a367920 commit 9b483a8
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 26 deletions.
68 changes: 45 additions & 23 deletions src/ViewModels/AsteroidsViewModel.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ 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;
const Starscape = @import("../Models/Starscape.zig").Starscape;

pub const AsteroidsViewModel = Shared.View.ViewModel.Create(
struct {
// Define Constants
pub const PLAYER_BASE_SIZE: f32 = 20;
const PLAYER_SPEED: f32 = 6;
const PLAYER_SPEED: f32 = 5;
pub const PLAYER_MAX_SHOOTS: i32 = 10;

const METEORS_SPEED = 2;
const METEORS_SPEED = 3;
const ANIMATION_SPEED_MOD = 15;

pub const MAX_BIG_METEORS = 8;
Expand All @@ -38,8 +39,12 @@ pub const AsteroidsViewModel = Shared.View.ViewModel.Create(
var smallMeteorsCount: i32 = 0;
var destroyedMeteorsCount: i32 = 0;

pub var starScape: Starscape = undefined;

// Initialize game variables
pub inline fn init() void {
starScape = Starscape.init(screenSize);

var posx: f32 = undefined;
var posy: f32 = undefined;
var velx: f32 = undefined;
Expand All @@ -64,14 +69,8 @@ pub const AsteroidsViewModel = Shared.View.ViewModel.Create(
.acceleration = 0,
.rotation = 0,
.collider = raylib.Vector3.init(
player.position.x + @sin(std.math.degreesToRadians(
f32,
player.rotation,
)) * (shipHeight / 2.5),
player.position.y - @cos(std.math.degreesToRadians(
f32,
player.rotation,
)) * (shipHeight / 2.5),
player.position.x,
player.position.y,
12,
),
.color = Shared.Color.Gray.Light,
Expand Down Expand Up @@ -199,14 +198,18 @@ pub const AsteroidsViewModel = Shared.View.ViewModel.Create(
smallMeteorsCount = 0;
}

pub inline fn deinit() void {
starScape.deinit();
}

// Update game (one frame)
pub inline fn Update() void {
// Player logic: rotation
if (Shared.Input.Left_Held()) {
player.rotation -= 5;
player.rotation -= 2.5;
}
if (Shared.Input.Right_Held()) {
player.rotation += 5;
player.rotation += 2.5;
}

// Player logic: speed
Expand All @@ -224,7 +227,7 @@ pub const AsteroidsViewModel = Shared.View.ViewModel.Create(
if (player.acceleration < 1) player.acceleration += 0.04;
} else {
if (player.acceleration > 0) {
player.acceleration -= 0.02;
player.acceleration -= 0.01;
} else if (player.acceleration < 0) {
player.acceleration = 0;
}
Expand Down Expand Up @@ -260,11 +263,11 @@ pub const AsteroidsViewModel = Shared.View.ViewModel.Create(
shoot[i].position.x = player.position.x + @sin(std.math.degreesToRadians(
f32,
player.rotation,
)) * shipHeight;
)) * (shipHeight / 2);
shoot[i].position.y = player.position.y - @cos(std.math.degreesToRadians(
f32,
player.rotation,
)) * shipHeight;
)) * (shipHeight / 2);
shoot[i].speed.x = 1.5 * @sin(std.math.degreesToRadians(
f32,
player.rotation,
Expand All @@ -275,6 +278,9 @@ pub const AsteroidsViewModel = Shared.View.ViewModel.Create(
)) * PLAYER_SPEED;
shoot[i].active = true;
shoot[i].rotation = player.rotation;

Shared.Sound.Play(.pew);

break;
}
}
Expand Down Expand Up @@ -323,16 +329,18 @@ pub const AsteroidsViewModel = Shared.View.ViewModel.Create(
}

// Collision logic: player vs meteors
player.collider.x = player.position.x + @sin(std.math.degreesToRadians(
f32,
player.rotation,
)) * (shipHeight / 2.5);
player.collider.y = player.position.y - @cos(std.math.degreesToRadians(
f32,
player.rotation,
)) * (shipHeight / 2.5);
player.collider.x = player.position.x;
player.collider.y = player.position.y;
player.collider.z = 12;

// Draw collider to check collision logic
// raylib.drawCircle(
// @intFromFloat(player.collider.x),
// @intFromFloat(player.collider.y),
// player.collider.z,
// Shared.Color.Red.Dark,
// );

for (0..MAX_BIG_METEORS) |i| {
if (bigMeteors[i].active and raylib.checkCollisionCircles(
raylib.Vector2.init(
Expand Down Expand Up @@ -482,6 +490,9 @@ pub const AsteroidsViewModel = Shared.View.ViewModel.Create(
}
//bigMeteors[m].position = (Vector2){-100, -100};
bigMeteors[m].color = Shared.Color.Red.Base;

Shared.Sound.Play(.Explosion);

break;
}
}
Expand Down Expand Up @@ -520,6 +531,9 @@ pub const AsteroidsViewModel = Shared.View.ViewModel.Create(
}
//mediumMeteors[m].position = (Vector2){-100, -100};
mediumMeteors[m].color = Shared.Color.Green.Base;

Shared.Sound.Play(.Explosion);

break;
}
}
Expand All @@ -537,6 +551,9 @@ pub const AsteroidsViewModel = Shared.View.ViewModel.Create(
destroyedMeteorsCount += 1;
smallMeteors[m].color = Shared.Color.Yellow.Base;
// smallMeteors[m].position = (Vector2){-100, -100};

Shared.Sound.Play(.Explosion);

break;
}
}
Expand All @@ -550,9 +567,14 @@ pub const AsteroidsViewModel = Shared.View.ViewModel.Create(
},
.{
.Init = init,
.DeInit = deinit,
},
);

fn init() void {
AsteroidsViewModel.GetVM().init();
}

fn deinit() void {
AsteroidsViewModel.GetVM().deinit();
}
13 changes: 11 additions & 2 deletions src/Views/AsteroidsView.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ const vm: type = AsteroidsViewModel.GetVM();

fn DrawFunction() Shared.View.Views {
raylib.clearBackground(Shared.Color.Tone.Dark);
raylib.drawRectangleLinesEx(raylib.Rectangle.init(0,0,3200,1800), 5, Shared.Color.Green.Light);
raylib.drawRectangleLinesEx(
raylib.Rectangle.init(0, 0, vm.screenSize.x, vm.screenSize.y),
5,
Shared.Color.Green.Light,
);

vm.starScape.Draw(vm.screenSize.x, vm.screenSize.y);

vm.Update();

Expand Down Expand Up @@ -60,7 +66,10 @@ fn DrawWithCamera() Shared.View.Views {
const camera = Shared.Camera.initScaledTargetCamera(
vm.screenSize,
3.5,
vm.player.position,
raylib.Vector2.init(
vm.player.position.x,
vm.player.position.y - vm.shipHeight,
),
);
return camera.Draw(Shared.View.Views, &DrawFunction);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Views/MenuView.zig
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const vm: type = MenuViewModel.GetVM();
pub fn DrawFunction() Shared.View.Views {
raylib.clearBackground(Shared.Color.Gray.Base);

Shared.Music.Play(.Test);
Shared.Music.Play(.TitleScreenMusic);

const locale = Shared.Locale.GetLocale().?;
const font = Shared.Font.Get(.Unknown);
Expand Down

0 comments on commit 9b483a8

Please sign in to comment.