Skip to content

Commit

Permalink
Random Asteroid Starting size and Infinate gameplay loop
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanlangston committed Dec 29, 2023
1 parent 8211b26 commit 54284e4
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 54 deletions.
12 changes: 4 additions & 8 deletions src/Models/Alien.zig
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub const Alien = struct {
default: bool,
};

pub inline fn init(player: Player, screenSize: raylib.Vector2, active: bool) Alien {
pub inline fn init() Alien {
var alien = Alien{
.position = raylib.Vector2.init(
0,
Expand All @@ -40,23 +40,19 @@ pub const Alien = struct {
),
.radius = 10,
.rotation = Shared.Random.Get().float(f32) * 360,
.active = active,
.active = false,
.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) {
if (visibleX > activeRadiusX or visibleX < -activeRadiusX) {
break;
}
posx = (Shared.Random.Get().float(f32) * (screenSize.x - 150)) + 150;
Expand All @@ -65,7 +61,7 @@ pub const Alien = struct {
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) {
if (visibleY > activeRadiusY or visibleY < -activeRadiusY) {
break;
}
posy = (Shared.Random.Get().float(f32) * (screenSize.y - 150)) + 150;
Expand Down
11 changes: 4 additions & 7 deletions src/Models/Meteor.zig
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub const Meteor = struct {
default: bool,
};

pub inline fn init(player: Player, screenSize: raylib.Vector2, radius: f32, active: bool) Meteor {
pub inline fn init(radius: f32) Meteor {
var meteor = Meteor{
.position = raylib.Vector2.init(
inactivitePoint,
Expand All @@ -46,13 +46,10 @@ pub const Meteor = struct {
),
.radius = radius,
.rotation = Shared.Random.Get().float(f32) * 365,
.active = active,
.active = false,
.color = Shared.Color.Blue.Base,
.frame = 0,
};
if (active) {
meteor.RandomizePositionAndSpeed(player, screenSize, false);
}

return meteor;
}
Expand All @@ -64,7 +61,7 @@ pub const Meteor = struct {
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) {
if (visibleX > activeRadiusX or visibleX < -activeRadiusX) {
break;
}
posx = (Shared.Random.Get().float(f32) * (screenSize.x - 150)) + 150;
Expand All @@ -73,7 +70,7 @@ pub const Meteor = struct {
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) {
if (visibleY > activeRadiusY or visibleY < -activeRadiusY) {
break;
}
posy = (Shared.Random.Get().float(f32) * (screenSize.y - 150)) + 150;
Expand Down
116 changes: 77 additions & 39 deletions src/ViewModels/AsteroidsViewModel.zig
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ pub const AsteroidsViewModel = Shared.View.ViewModel.Create(
pub const PLAYER_BASE_SIZE: f32 = 20;
pub const PLAYER_MAX_SHOOTS: i32 = 10;

pub const MAX_BIG_METEORS = 8;
pub const MAX_MEDIUM_METEORS = MAX_BIG_METEORS * 2;
pub const MAX_SMALL_METEORS = MAX_MEDIUM_METEORS * 2;
pub const MAX_BIG_METEORS: u16 = 64;
pub const MAX_MEDIUM_METEORS: u32 = MAX_BIG_METEORS * 2;
pub const MAX_SMALL_METEORS: u32 = MAX_MEDIUM_METEORS * 2;

pub const MAX_ALIENS = 4;
pub const MAX_ALIENS: u8 = 8;
pub const ALIENS_MAX_SHOOTS: i32 = MAX_ALIENS * 2;
pub const MAX_SHIELD: u8 = 50;

Expand All @@ -42,10 +42,13 @@ pub const AsteroidsViewModel = Shared.View.ViewModel.Create(
pub var aliens: [MAX_ALIENS]Alien = undefined;
pub var alien_shoot: [ALIENS_MAX_SHOOTS]Shoot = undefined;

var bigMeteorsCount: u8 = 0;
var midMeteorsCount: u8 = 0;
var smallMeteorsCount: u16 = 0;
var smallMeteorsDestroyedCount: u2 = 0;
var bigMeteorsCount: u16 = 0;
var midMeteorsCount: u32 = 0;
var smallMeteorsCount: u32 = 0;
var smallMeteorsDestroyedCount: u4 = 0;
var alienCount: u8 = 0;

const totalStartingMeteors: u8 = 32;

pub var score: u64 = 0;

Expand Down Expand Up @@ -76,44 +79,73 @@ pub const AsteroidsViewModel = Shared.View.ViewModel.Create(
score = 0;

// Initialization shoot
for (0..PLAYER_MAX_SHOOTS) |i| {
inline for (0..PLAYER_MAX_SHOOTS) |i| {
shoot[i] = Shoot.init(Shared.Color.Tone.Light);
}

// Initialization Big Meteor
for (0..MAX_BIG_METEORS) |i| {
bigMeteors[i] = Meteor.init(player, screenSize, 40, true);
bigMeteorsCount += 1;
}

// Initialization Medium Meteor
for (0..MAX_MEDIUM_METEORS) |i| {
mediumMeteors[i] = Meteor.init(player, screenSize, 20, false);
}

// Initialization Small Meteor
// Initialization Meteors
for (0..MAX_SMALL_METEORS) |i| {
smallMeteors[i] = Meteor.init(player, screenSize, 10, false);
if (i < MAX_BIG_METEORS) {
bigMeteors[i] = Meteor.init(40);
}
if (i < MAX_MEDIUM_METEORS) {
mediumMeteors[i] = Meteor.init(20);
}
smallMeteors[i] = Meteor.init(10);
}
bigMeteorsCount = 0;
midMeteorsCount = 0;
smallMeteorsCount = 0;
var initialMeteorCount: u8 = 0;
while (initialMeteorCount < totalStartingMeteors) {
switch (Shared.Random.Get().intRangeAtMost(u2, 0, 2)) {
2 => {
bigMeteors[@intCast(bigMeteorsCount)].RandomizePositionAndSpeed(player, screenSize, true);
bigMeteors[@intCast(bigMeteorsCount)].active = true;
initialMeteorCount += 4;
bigMeteorsCount += 1;
},
1 => {
mediumMeteors[@intCast(midMeteorsCount)].RandomizePositionAndSpeed(player, screenSize, true);
mediumMeteors[@intCast(midMeteorsCount)].active = true;
initialMeteorCount += 2;
midMeteorsCount += 1;
},
else => {
smallMeteors[@intCast(smallMeteorsCount)].RandomizePositionAndSpeed(player, screenSize, true);
smallMeteors[@intCast(smallMeteorsCount)].active = true;
initialMeteorCount += 1;
smallMeteorsCount += 1;
},
}
}

// Initialization Aliens
for (0..MAX_ALIENS) |i| {
aliens[i] = Alien.init(player, screenSize, true);
inline for (0..MAX_ALIENS) |i| {
aliens[i] = Alien.init();
}

// Initialization alien shoot
for (0..ALIENS_MAX_SHOOTS) |i| {
inline for (0..ALIENS_MAX_SHOOTS) |i| {
alien_shoot[i] = Shoot.init(Shared.Color.Green.Light);
}

midMeteorsCount = 0;
smallMeteorsCount = 0;
}

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

var lastScore: u64 = 0;
inline fn NewAlien() void {
while (alienCount < MAX_ALIENS and score - lastScore > 10) {
aliens[@intCast(alienCount)].RandomizePosition(player, screenSize, true);
aliens[@intCast(alienCount)].active = true;
alienCount += 1;

lastScore += 10;
}
}

// Update game (one frame)
pub inline fn Update() void {

Expand All @@ -139,11 +171,10 @@ pub const AsteroidsViewModel = Shared.View.ViewModel.Create(
inline for (0..MAX_ALIENS) |i| {
switch (aliens[i].Update(player, &shoot, &alien_shoot, screenSize)) {
.shot => {
alienCount -= 1;
score += 8;

// Move to new random position and reactivate
aliens[i].RandomizePosition(player, screenSize, false);
aliens[i].active = true;
NewAlien();
},
.default => {},
}
Expand Down Expand Up @@ -191,6 +222,8 @@ pub const AsteroidsViewModel = Shared.View.ViewModel.Create(
mediumMeteors[@intCast(midMeteorsCount)].active = true;
midMeteorsCount += 1;
}

NewAlien();
},
.collide => {
shieldLevel = 0;
Expand Down Expand Up @@ -227,6 +260,8 @@ pub const AsteroidsViewModel = Shared.View.ViewModel.Create(
smallMeteors[@intCast(smallMeteorsCount)].active = true;
smallMeteorsCount += 1;
}

NewAlien();
},
.collide => {
shieldLevel = 0;
Expand All @@ -239,18 +274,21 @@ pub const AsteroidsViewModel = Shared.View.ViewModel.Create(
.default => {},
.shot => {
smallMeteorsCount -= 1;
smallMeteorsDestroyedCount += 1;
score += 1;

// After 4 small meteors are destroyed, create a new big one
if (smallMeteorsDestroyedCount == 3) {
bigMeteors[@intCast(bigMeteorsCount)].RandomizePositionAndSpeed(player, screenSize, true);
bigMeteors[@intCast(bigMeteorsCount)].active = true;
// After 4 small meteors are destroyed, create two big ones (until the max meteors is reached)
for (0..2) |_| {
if (bigMeteorsCount < MAX_BIG_METEORS and smallMeteorsDestroyedCount / 4 >= 1) {
bigMeteors[@intCast(bigMeteorsCount)].RandomizePositionAndSpeed(player, screenSize, true);
bigMeteors[@intCast(bigMeteorsCount)].active = true;

smallMeteorsDestroyedCount = 0;
bigMeteorsCount += 1;
Shared.Log.Info("New Big Meteor");
smallMeteorsDestroyedCount -= 4;
bigMeteorsCount += 1;
}
}
smallMeteorsDestroyedCount += 1;

NewAlien();
},
.collide => {
shieldLevel = 0;
Expand Down

0 comments on commit 54284e4

Please sign in to comment.