Skip to content

Commit

Permalink
Cleanup init code, infinite gameplay
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanlangston committed Dec 28, 2023
1 parent 0e2c4bd commit 2c1eaad
Show file tree
Hide file tree
Showing 5 changed files with 258 additions and 205 deletions.
68 changes: 50 additions & 18 deletions src/Models/Alien.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,67 @@ pub const Alien = struct {
shouldDraw: bool = false,

const ANIMATION_SPEED_MOD = 10;
pub const ALIEN_SPEED: f32 = 3;
pub const ALIEN_SPEED: f32 = 4;

pub const AlienStatusType = enum {
shot,
collide,
default,
};

pub const AlienStatus = union(AlienStatusType) {
shot: Shoot,
collide: bool,
default: bool,
};

pub inline fn init(player: Player, screenSize: raylib.Vector2, active: bool) Alien {
var alien = Alien{
.position = raylib.Vector2.init(
0,
0,
),
.speed = raylib.Vector2.init(
0,
0,
),
.radius = 10,
.rotation = Shared.Random.Get().float(f32) * 360,
.active = active,
.color = Shared.Color.Yellow.Base,
.frame = Shared.Random.Get().float(f32) * 10,
};

if (active) {
alien.RandomizePosition(player, screenSize, false);
}

return alien;
}

pub inline fn RandomizePosition(self: *@This(), player: Player, screenSize: raylib.Vector2, offscreen: bool) void {
var posx: f32 = (Shared.Random.Get().float(f32) * (screenSize.x - 150)) + 150;
while (offscreen) {
const visibleX = posx - player.position.x;
if (visibleX < activeRadiusX or visibleX > -activeRadiusX) {
break;
}
posx = (Shared.Random.Get().float(f32) * (screenSize.x - 150)) + 150;
}

var posy: f32 = (Shared.Random.Get().float(f32) * (screenSize.y - 150)) + 150;
while (offscreen) {
const visibleY = posy - player.position.y;
if (visibleY < activeRadiusY or visibleY > -activeRadiusY) {
break;
}
posy = (Shared.Random.Get().float(f32) * (screenSize.y - 150)) + 150;
}

self.position = raylib.Vector2.init(
posx,
posy,
);
}

pub inline fn Update(self: *@This(), player: Player, comptime shoots: []Shoot, comptime alien_shoots: []Shoot, screenSize: raylib.Vector2) AlienStatus {
// If Active
if (self.active) {
Expand All @@ -39,19 +86,6 @@ pub const Alien = struct {
self.frame += raylib.getFrameTime() * ANIMATION_SPEED_MOD;
}

// Check Collision with player
if (raylib.checkCollisionCircles(
raylib.Vector2.init(
player.collider.x,
player.collider.y,
),
player.collider.z,
self.position,
self.radius,
)) {
return AlienStatus{ .collide = true };
}

// Random Motion
if (Shared.Random.Get().intRangeAtMost(u8, 0, 10) < 2) {
if (self.speed.y == 0) {
Expand Down Expand Up @@ -106,8 +140,6 @@ pub const Alien = struct {
shoots[i].lifeSpawn = 0;
self.active = false;

self.color = Shared.Color.Red.Base;

Shared.Sound.Play(.Explosion);

return AlienStatus{ .shot = shoots[i] };
Expand Down
96 changes: 91 additions & 5 deletions src/Models/Meteor.zig
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ pub const Meteor = struct {
frame: f32,

const ANIMATION_SPEED_MOD = 15;
pub const METEORS_SPEED = 3;

const inactivitePoint = -100;

pub const MeteorStatusType = enum {
shot,
Expand All @@ -31,11 +34,94 @@ pub const Meteor = struct {
default: bool,
};

pub inline fn init(player: Player, screenSize: raylib.Vector2, radius: f32, active: bool) Meteor {
var meteor = Meteor{
.position = raylib.Vector2.init(
inactivitePoint,
inactivitePoint,
),
.speed = raylib.Vector2.init(
0,
0,
),
.radius = radius,
.rotation = Shared.Random.Get().float(f32) * 365,
.active = active,
.color = Shared.Color.Blue.Base,
.frame = 0,
};
if (active) {
meteor.RandomizePositionAndSpeed(player, screenSize, false);
}

return meteor;
}

pub inline fn RandomizePositionAndSpeed(self: *@This(), player: Player, screenSize: raylib.Vector2, offscreen: bool) void {
var velx: f32 = undefined;
var vely: f32 = undefined;

var posx: f32 = (Shared.Random.Get().float(f32) * (screenSize.x - 150)) + 150;
while (offscreen) {
const visibleX = posx - player.position.x;
if (visibleX < activeRadiusX or visibleX > -activeRadiusX) {
break;
}
posx = (Shared.Random.Get().float(f32) * (screenSize.x - 150)) + 150;
}

var posy: f32 = (Shared.Random.Get().float(f32) * (screenSize.y - 150)) + 150;
while (offscreen) {
const visibleY = posy - player.position.y;
if (visibleY < activeRadiusY or visibleY > -activeRadiusY) {
break;
}
posy = (Shared.Random.Get().float(f32) * (screenSize.y - 150)) + 150;
}

if (Shared.Random.Get().boolean()) {
velx = Shared.Random.Get().float(f32) * METEORS_SPEED;
} else {
velx = Shared.Random.Get().float(f32) * METEORS_SPEED * -1;
}
if (Shared.Random.Get().boolean()) {
vely = Shared.Random.Get().float(f32) * METEORS_SPEED;
} else {
vely = Shared.Random.Get().float(f32) * METEORS_SPEED * -1;
}

while (true) {
if (velx == 0 and vely == 0) {
if (Shared.Random.Get().boolean()) {
velx = Shared.Random.Get().float(f32) * METEORS_SPEED;
} else {
velx = Shared.Random.Get().float(f32) * METEORS_SPEED * -1;
}
if (Shared.Random.Get().boolean()) {
vely = Shared.Random.Get().float(f32) * METEORS_SPEED;
} else {
vely = Shared.Random.Get().float(f32) * METEORS_SPEED * -1;
}
} else break;
}

self.position = raylib.Vector2.init(
posx,
posy,
);
self.speed = raylib.Vector2.init(
velx,
vely,
);
}

pub inline fn Update(self: *@This(), player: Player, comptime shoots: []Shoot, comptime aliens: []Alien, comptime alien_shoots: []Shoot, screenSize: raylib.Vector2) MeteorStatus {
// If Active
if (self.active) {
// Reset Frame
self.frame = 0;
// Reset color
self.color = Shared.Color.Blue.Base;

// Check Collision with player
if (raylib.checkCollisionCircles(
Expand Down Expand Up @@ -148,16 +234,16 @@ pub const Meteor = struct {
return MeteorStatus{ .default = true };
}

const screenWidth = 500;
const screenHeight = 325;
const activeRadiusX = 500;
const activeRadiusY = 325;

pub inline fn Draw(self: @This(), shipPosition: raylib.Vector2) void {
if (self.position.x == -100 and self.position.y == -100) return;
if (self.position.x == inactivitePoint and self.position.y == inactivitePoint) return;

const visibleX = self.position.x - shipPosition.x;
const visibleY = self.position.y - shipPosition.y;
if (visibleX > screenWidth or visibleX < -screenWidth) return;
if (visibleY > screenHeight or visibleY < -screenHeight) return;
if (visibleX > activeRadiusX or visibleX < -activeRadiusX) return;
if (visibleY > activeRadiusY or visibleY < -activeRadiusY) return;

const spriteFrame = MeteorSprite.getSpriteFrame(@intFromFloat(self.frame));
const color: raylib.Color = if (self.active) self.color else raylib.Color.fade(self.color, 0.3);
Expand Down
60 changes: 58 additions & 2 deletions src/Models/Player.zig
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const std = @import("std");
const raylib = @import("raylib");
const raylib_math = @import("raylib-math");
const Shared = @import("../Shared.zig").Shared;
const Shoot = @import("./Shoot.zig").Shoot;
const Alien = @import("./Alien.zig").Alien;

pub const Player = struct {
position: raylib.Vector2,
Expand All @@ -25,7 +27,29 @@ pub const Player = struct {
default: bool,
};

pub inline fn Update(self: *@This(), comptime shoots: []Shoot, comptime alien_shoots: []Shoot, screenSize: raylib.Vector2, halfShipHeight: f32) PlayerStatus {
pub inline fn init(screenSize: raylib.Vector2, shipHeight: f32) Player {
const position = raylib.Vector2.init(
screenSize.x / 2,
(screenSize.y - shipHeight) / 2,
);
return Player{
.position = position,
.speed = raylib.Vector2.init(
0,
0,
),
.acceleration = 0,
.rotation = 0,
.collider = raylib.Vector3.init(
position.x,
position.y,
12,
),
.color = Shared.Color.Gray.Light,
};
}

pub inline fn Update(self: *@This(), comptime shoots: []Shoot, comptime aliens: []Alien, comptime alien_shoots: []Shoot, screenSize: raylib.Vector2, halfShipHeight: f32) PlayerStatus {
// Player logic: rotation
if (Shared.Input.Left_Held()) {
self.rotation -= 2.5;
Expand Down Expand Up @@ -111,7 +135,7 @@ pub const Player = struct {
// Collision logic: player vs meteors
self.collider.x = self.position.x;
self.collider.y = self.position.y;
self.collider.z = 12;
//self.collider.z = 12;

// Check if alien shot hit
inline for (0..alien_shoots.len) |i| {
Expand All @@ -128,6 +152,38 @@ pub const Player = struct {
}
}

// Check if alien will collide
inline for (0..aliens.len) |i| {
if (aliens[i].active and raylib.checkCollisionCircles(
aliens[i].position,
aliens[i].radius,
raylib.Vector2.init(
self.position.x - 25,
self.position.y - 25,
),
self.collider.z + 50,
)) {
aliens[i].rotation = std.math.radiansToDegrees(f32, raylib_math.vector2LineAngle(aliens[i].position, self.position)) - 90;

aliens[i].position.x = aliens[i].position.x + @sin(std.math.degreesToRadians(
f32,
aliens[i].rotation,
)) * aliens[i].radius;
aliens[i].position.y = aliens[i].position.y - @cos(std.math.degreesToRadians(
f32,
aliens[i].rotation,
)) * aliens[i].radius;
aliens[i].speed.x = @sin(std.math.degreesToRadians(
f32,
aliens[i].rotation,
)) * Alien.ALIEN_SPEED;
aliens[i].speed.y = @cos(std.math.degreesToRadians(
f32,
aliens[i].rotation,
)) * Alien.ALIEN_SPEED;
}
}

return PlayerStatus{ .default = true };
}

Expand Down
19 changes: 19 additions & 0 deletions src/Models/Shoot.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const raylib = @import("raylib");
const Shared = @import("../Shared.zig").Shared;

pub const Shoot = struct {
position: raylib.Vector2,
Expand All @@ -11,6 +12,24 @@ pub const Shoot = struct {

const ANIMATION_SPEED_MOD = 30;

pub inline fn init(color: raylib.Color) Shoot {
return Shoot{
.position = raylib.Vector2.init(
0,
0,
),
.speed = raylib.Vector2.init(
0,
0,
),
.radius = 2,
.rotation = 0,
.active = false,
.lifeSpawn = 0,
.color = color,
};
}

pub inline fn Update(self: *@This(), screenSize: raylib.Vector2) void {
if (self.active) {
self.lifeSpawn += raylib.getFrameTime() * ANIMATION_SPEED_MOD;
Expand Down
Loading

0 comments on commit 2c1eaad

Please sign in to comment.