Skip to content
This repository has been archived by the owner on Mar 7, 2024. It is now read-only.

Commit

Permalink
Update Math class
Browse files Browse the repository at this point in the history
  • Loading branch information
FractalDiane committed Dec 3, 2020
1 parent 08233e8 commit ded33b4
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 63 deletions.
4 changes: 2 additions & 2 deletions Components/AnimatedSprite.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "Components/Component.h"
#include "Components/Transform.h"
#include "Core/Vector2.h"
#include "Core/Mathf.h"
#include "Core/Math.h"

#include "Core/ECSSystem.h"

Expand Down Expand Up @@ -49,7 +49,7 @@ class AnimatedSprite : public Component {
//void set_animation_speed(const float& value) { animation_speed = value; }

unsigned int current_frame() { return frame; }
void advance_frame() { frame = Mathf::wrap(++frame, 0, animations[current_animation]->image_sequence.size() - 1); }
void advance_frame() { frame = Math::wrap(++frame, 0, animations[current_animation]->image_sequence.size() - 1); }

double get_anim_time() { return anim_time; }
void add_anim_time(const double& value) { anim_time += value; }
Expand Down
4 changes: 4 additions & 0 deletions Core/Math.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#include "Math.h"

std::random_device Math::device{};
std::mt19937 Math::random_engine{Math::device()};
56 changes: 56 additions & 0 deletions Core/Math.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#pragma once

#include <random>
#include <cmath>

namespace {
std::random_device device{};
std::mt19937 rand_engine{device()};
}

class Math {
private:
static std::random_device device;
static std::mt19937 random_engine;

public:
static constexpr long double Pi = 3.141592653589793238462643383279502884L;
static constexpr inline float deg2rad(float degrees) { return (degrees * Pi) / 180.0f; }
static constexpr inline float rad2deg(float radians) { return (radians * 180.0f) / Pi; }


static constexpr inline float clamp(float value, float min, float max) { return (value < min) ? min : (max < value) ? max : value; }


static constexpr int wrap(int value, int min, int max) {
int64_t range = max - min;
return range == 0 ? min : min + ((((value - min) % range) + range) % range);
}


static constexpr inline int abs(int value) { return value < 0 ? -value : value; }
static constexpr inline float abs(float value) { return value < 0.0f ? -value : value; }

static inline float lerp(float from, float to, float weight) { return from + (to - from) * weight; }
static inline float lerp_delta(float from, float to, float weight, float delta) { return lerp(from, to, 1.0f - std::pow(weight, delta)); }


static inline int rand_range(int min, int max) {
std::uniform_int_distribution<> distr{min, max};
return distr(random_engine);
}


static inline float rand_range(float min, float max) {
std::uniform_real_distribution<float> distr{min, max};
return distr(random_engine);
}


template <typename T, typename... Args>
static T choose(T first, Args... args) {
T array[] = {first, args...};
std::uniform_int_distribution<> distr{0, sizeof...(Args)};
return array[distr(random_engine)];
}
};
58 changes: 0 additions & 58 deletions Core/Mathf.h

This file was deleted.

4 changes: 2 additions & 2 deletions Systems/PlayerSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "Core/World.h"
#include "Core/Input.h"

#include "Core/Mathf.h"
#include "Core/Math.h"
#include "Core/Serializer.h"

#include "Components/Transform.h"
Expand Down Expand Up @@ -104,7 +104,7 @@ void PlayerSystem::fire_bullet(const Vector2& player_pos, World& world) {
BulletComponent* bullet_comp = new BulletComponent{};

Vector2 dir = Vector2{-1, 0}.rotated(player_pos.angle_to(Window::get_mouse_position()));
bullet_tr->set_rotation(Mathf::rad2deg(dir.angle()));
bullet_tr->set_rotation(Math::rad2deg(dir.angle()));
bullet_tr->set_scale(Vector2{2, 2});
bullet_comp->set_velocity(dir);

Expand Down
2 changes: 1 addition & 1 deletion Systems/RenderSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "Core/World.h"
#include "Core/Vector2.h"
#include "Core/Mathf.h"
#include "Core/Math.h"
#include "Core/ECSSystem.h"

#include <cmath>
Expand Down

0 comments on commit ded33b4

Please sign in to comment.