mirror of
https://github.com/italicsjenga/agb.git
synced 2025-01-11 09:31:34 +11:00
Use agb's random rather than own implementation
This commit is contained in:
parent
a57043604d
commit
7ad52a2974
|
@ -3,15 +3,12 @@
|
|||
|
||||
extern crate alloc;
|
||||
|
||||
mod rng;
|
||||
mod sfx;
|
||||
|
||||
use core::cmp::Ordering;
|
||||
|
||||
use alloc::{boxed::Box, vec::Vec};
|
||||
|
||||
use rng::get_random;
|
||||
|
||||
use agb::{
|
||||
display::{
|
||||
object::{Graphics, Object, ObjectController, Sprite, Tag, TagMap},
|
||||
|
@ -21,6 +18,7 @@ use agb::{
|
|||
fixnum::{FixedNum, Rect, Vector2D},
|
||||
input::{Button, ButtonController, Tri},
|
||||
interrupt::VBlank,
|
||||
rng,
|
||||
};
|
||||
use generational_arena::Arena;
|
||||
use sfx::Sfx;
|
||||
|
@ -1117,7 +1115,7 @@ impl MiniFlameData {
|
|||
self.sprite_offset = 0;
|
||||
self.state = MiniFlameState::Dead;
|
||||
|
||||
if get_random() % 4 == 0 {
|
||||
if rng::next() % 4 == 0 {
|
||||
instruction = UpdateInstruction::CreateParticle(
|
||||
ParticleData::new_health(),
|
||||
entity.position,
|
||||
|
@ -1140,7 +1138,7 @@ impl MiniFlameData {
|
|||
self.sprite_offset = 0;
|
||||
self.state = MiniFlameState::Dead;
|
||||
|
||||
if get_random() % 4 == 0 {
|
||||
if rng::next() % 4 == 0 {
|
||||
instruction = UpdateInstruction::CreateParticle(
|
||||
ParticleData::new_health(),
|
||||
entity.position,
|
||||
|
@ -1705,7 +1703,7 @@ impl<'a> Boss<'a> {
|
|||
Self {
|
||||
entity,
|
||||
health: 5,
|
||||
target_location: get_random().rem_euclid(5) as u8,
|
||||
target_location: rng::next().rem_euclid(5) as u8,
|
||||
state: BossActiveState::Damaged(60),
|
||||
timer: 0,
|
||||
screen_coords,
|
||||
|
@ -1806,9 +1804,9 @@ impl<'a> Boss<'a> {
|
|||
fn commit(&mut self, offset: Vector2D<Number>) {
|
||||
let shake = if self.shake_magnitude != 0.into() {
|
||||
(
|
||||
Number::from_raw(get_random()).rem_euclid(self.shake_magnitude)
|
||||
Number::from_raw(rng::next()).rem_euclid(self.shake_magnitude)
|
||||
- self.shake_magnitude / 2,
|
||||
Number::from_raw(get_random()).rem_euclid(self.shake_magnitude)
|
||||
Number::from_raw(rng::next()).rem_euclid(self.shake_magnitude)
|
||||
- self.shake_magnitude / 2,
|
||||
)
|
||||
.into()
|
||||
|
@ -1821,8 +1819,8 @@ impl<'a> Boss<'a> {
|
|||
}
|
||||
fn explode(&self, enemies: &mut Arena<Enemy<'a>>, object_controller: &'a ObjectController) {
|
||||
for _ in 0..(6 - self.health) {
|
||||
let x_offset: Number = Number::from_raw(get_random()).rem_euclid(2.into()) - 1;
|
||||
let y_offset: Number = Number::from_raw(get_random()).rem_euclid(2.into()) - 1;
|
||||
let x_offset: Number = Number::from_raw(rng::next()).rem_euclid(2.into()) - 1;
|
||||
let y_offset: Number = Number::from_raw(rng::next()).rem_euclid(2.into()) - 1;
|
||||
let mut flame = Enemy::new(
|
||||
object_controller,
|
||||
EnemyData::MiniFlame(MiniFlameData::new()),
|
||||
|
@ -1835,7 +1833,7 @@ impl<'a> Boss<'a> {
|
|||
|
||||
fn get_next_target_location(&self) -> u8 {
|
||||
loop {
|
||||
let a = get_random().rem_euclid(5) as u8;
|
||||
let a = rng::next().rem_euclid(5) as u8;
|
||||
if a != self.target_location {
|
||||
break a;
|
||||
}
|
||||
|
@ -1980,8 +1978,8 @@ impl<'a> Game<'a> {
|
|||
if self.shake_time > 0 {
|
||||
let size = self.shake_time.min(4) as i32;
|
||||
let offset: Vector2D<Number> = (
|
||||
Number::from_raw(get_random()) % size - Number::new(size) / 2,
|
||||
Number::from_raw(get_random()) % size - Number::new(size) / 2,
|
||||
Number::from_raw(rng::next()) % size - Number::new(size) / 2,
|
||||
Number::from_raw(rng::next()) % size - Number::new(size) / 2,
|
||||
)
|
||||
.into();
|
||||
this_frame_offset += offset;
|
||||
|
@ -2298,7 +2296,7 @@ fn game_with_level(gba: &mut agb::Gba) {
|
|||
}
|
||||
}
|
||||
|
||||
get_random(); // advance RNG to make it less predictable between runs
|
||||
rng::next(); // advance RNG to make it less predictable between runs
|
||||
};
|
||||
|
||||
game.clear(&mut vram);
|
||||
|
|
|
@ -1,34 +0,0 @@
|
|||
struct RandomNumberGenerator {
|
||||
state: [u32; 4],
|
||||
}
|
||||
|
||||
impl RandomNumberGenerator {
|
||||
const fn new() -> Self {
|
||||
Self {
|
||||
state: [1014776995, 476057059, 3301633994, 706340607],
|
||||
}
|
||||
}
|
||||
|
||||
fn next(&mut self) -> i32 {
|
||||
let result = (self.state[0].wrapping_add(self.state[3]))
|
||||
.rotate_left(7)
|
||||
.wrapping_mul(9);
|
||||
let t = self.state[1].wrapping_shr(9);
|
||||
|
||||
self.state[2] ^= self.state[0];
|
||||
self.state[3] ^= self.state[1];
|
||||
self.state[1] ^= self.state[2];
|
||||
self.state[0] ^= self.state[3];
|
||||
|
||||
self.state[2] ^= t;
|
||||
self.state[3] = self.state[3].rotate_left(11);
|
||||
|
||||
result as i32
|
||||
}
|
||||
}
|
||||
|
||||
static mut RANDOM_GENERATOR: RandomNumberGenerator = RandomNumberGenerator::new();
|
||||
|
||||
pub fn get_random() -> i32 {
|
||||
unsafe { &mut RANDOM_GENERATOR }.next()
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
use super::rng::get_random;
|
||||
use agb::fixnum::Num;
|
||||
use agb::rng;
|
||||
use agb::sound::mixer::{ChannelId, Mixer, SoundChannel};
|
||||
|
||||
const BAT_DEATH: &[u8] = agb::include_wav!("sfx/BatDeath.wav");
|
||||
|
@ -85,7 +85,7 @@ impl<'a> Sfx<'a> {
|
|||
}
|
||||
|
||||
pub fn jump(&mut self) {
|
||||
let r = get_random() % 3;
|
||||
let r = rng::next() % 3;
|
||||
|
||||
let channel = match r {
|
||||
0 => SoundChannel::new(JUMP1),
|
||||
|
|
Loading…
Reference in a new issue