2023-11-20 23:25:36 -06:00
# pragma region License
2023-11-14 18:11:32 -06:00
/*
License ( OLC - 3 )
~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
2024-01-02 00:46:32 -06:00
Copyright 2024 Joshua Sigona < sigonasr2 @ gmail . com >
2023-11-14 18:11:32 -06:00
Redistribution and use in source and binary forms , with or without modification ,
are permitted provided that the following conditions are met :
1. Redistributions or derivations of source code must retain the above copyright
notice , this list of conditions and the following disclaimer .
2. Redistributions or derivative works in binary form must reproduce the above
copyright notice . This list of conditions and the following disclaimer must be
reproduced in the documentation and / or other materials provided with the distribution .
3. Neither the name of the copyright holder nor the names of its contributors may
be used to endorse or promote products derived from this software without specific
prior written permission .
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS " AS IS " AND ANY
EXPRESS OR IMPLIED WARRANTIES , INCLUDING , BUT NOT LIMITED TO , THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED . IN NO EVENT
SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT , INDIRECT ,
INCIDENTAL , SPECIAL , EXEMPLARY , OR CONSEQUENTIAL DAMAGES ( INCLUDING , BUT NOT LIMITED
TO , PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES ; LOSS OF USE , DATA , OR PROFITS ; OR
BUSINESS INTERRUPTION ) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY , WHETHER IN
CONTRACT , STRICT LIABILITY , OR TORT ( INCLUDING NEGLIGENCE OR OTHERWISE ) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE , EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE .
2023-11-29 00:50:00 -06:00
2024-01-30 14:48:49 +00:00
Portions of this software are copyright © 2024 The FreeType
2023-11-29 00:50:00 -06:00
Project ( www . freetype . org ) . Please see LICENSE_FT . txt for more information .
All rights reserved .
2023-11-14 18:11:32 -06:00
*/
2023-11-20 23:25:36 -06:00
# pragma endregion
2023-07-06 04:35:40 -05:00
# pragma once
# include <stdlib.h>
2023-07-25 18:37:12 -05:00
# include "olcUTIL_Geometry2D.h"
2024-01-10 17:49:24 -06:00
# include "olcPGEX_TTF.h"
2024-11-05 20:58:36 -08:00
# include "Direction.h"
2023-12-31 16:32:30 -06:00
# include <random>
2025-01-03 01:15:31 -06:00
# include <optional>
class Entity ;
2023-11-29 06:05:29 -06:00
2023-11-29 16:43:41 -06:00
namespace olc : : util {
2023-07-06 04:35:40 -05:00
//Returns 0-range (as a float).
float random ( float range ) ;
2024-04-28 18:00:13 -05:00
//Returns a random float value min(inclusive) to max(exclusive).
const float random_range ( const float min , const float max ) ;
2023-12-31 16:32:30 -06:00
//Returns 0-32767 (as an int).
int random ( ) ;
2023-07-25 18:37:12 -05:00
//Returns a normalized vector pointing from posFrom towards posTo.
vf2d pointTo ( vf2d posFrom , vf2d posTo ) ;
2023-09-19 05:58:55 -05:00
//Returns the angle (in radians) pointing from posFrom towards posTo.
float angleTo ( vf2d posFrom , vf2d posTo ) ;
2023-09-09 05:51:15 -05:00
float degToRad ( float deg ) ;
float radToDeg ( float rad ) ;
2024-11-05 20:58:36 -08:00
const float dirToAngle ( const Direction dir ) ;
2024-09-05 14:24:01 -05:00
# pragma region Lerp templates + specializations
2024-11-12 22:52:41 -08:00
template < class T >
inline auto lerp ( const T val1 , const T val2 , const float t ) {
return T ( val1 * ( 1 - t ) + val2 * t ) ;
2024-09-05 14:24:01 -05:00
}
template < >
2024-11-12 22:52:41 -08:00
inline auto lerp < vf2d > ( const vf2d val1 , const vf2d val2 , const float t ) {
2024-09-05 14:24:01 -05:00
return val1 . lerp ( val2 , t ) ;
}
template < >
//NOTE: Also interpolates the alpha!!!
2024-11-12 22:52:41 -08:00
inline auto lerp < Pixel > ( const Pixel val1 , const Pixel val2 , const float t ) {
2024-09-05 14:24:01 -05:00
Pixel col { PixelLerp ( val1 , val2 , t ) } ;
col . a = lerp ( val1 . a , val2 . a , t ) ;
return col ;
}
# pragma endregion
2024-09-22 17:43:51 -05:00
template < class T , class U >
inline auto smoothstep ( const T val1 , const U val2 , const float t ) {
auto x { decltype ( val1 + val2 ) ( 1 - pow ( 1 - t , 3 ) ) } ;
return val1 * ( 1 - x ) + val2 * x ;
}
2023-09-26 06:35:21 -05:00
std : : string timerStr ( float time ) ;
2023-10-11 19:50:12 -05:00
std : : string WrapText ( PixelGameEngine * pge , std : : string str , int width , bool proportional , vd2d scale ) ;
2023-11-29 06:05:29 -06:00
std : : u32string WrapText ( PixelGameEngine * pge , std : : u32string str , int width , Font & font , vd2d scale ) ;
2024-03-26 18:53:17 -05:00
float angle_difference ( float angle_1 , float angle_2 ) ;
2024-03-29 02:40:06 -05:00
std : : string GetHash ( std : : string file ) ;
2024-05-13 12:05:57 -05:00
const float distance ( const vf2d & point1 , const vf2d & point2 ) ;
2024-07-26 03:09:55 -05:00
//Modifies angle argument directly to turn towards said direction. rate is in radians, please multiply by fElapsedTime if you intend to use this per frame.
void turn_towards_direction ( float & angle , float target , float rate ) ;
2025-01-03 01:15:31 -06:00
//Modifies angle argument directly to turn from a given position and angle towards said target. rate is in radians, no need to multiply by fElapsedTime.
//If you need a raw angle amount, then DO NOT DEFINE A fElapsedTime value!!!
void turn_towards_target ( float & angle , const vf2d fromPos , const vf2d toPos , float rate , const std : : optional < float > fElapsedTime ) ;
2024-08-09 18:13:08 -05:00
std : : wstring to_wstring ( const std : : string & str ) ;
template < class . . . _Args >
2024-09-10 17:54:58 -05:00
const std : : string vformat ( const std : : string_view str , _Args . . . _Vals ) {
2024-08-09 18:13:08 -05:00
return std : : vformat ( str , std : : make_format_args ( _Vals . . . ) ) ;
}
template < class . . . _Args >
2024-09-10 17:54:58 -05:00
const std : : wstring wformat ( const std : : string_view str , _Args . . . _Vals ) {
2024-08-09 18:13:08 -05:00
return util : : to_wstring ( std : : vformat ( str , std : : make_format_args ( _Vals . . . ) ) ) ;
}
2024-09-04 02:15:26 -05:00
template < class T >
T map_range ( T x , T in_min , T in_max , T out_min , T out_max ) {
2024-09-10 17:54:58 -05:00
return ( x - in_min ) * ( out_max - out_min ) / ( in_max - in_min ) + out_min ;
2024-09-04 02:15:26 -05:00
}
2024-01-15 01:30:06 -06:00
}
2024-01-21 23:49:40 -06:00
template < class TL , class TR >
constexpr auto circ_add (
const TL & lhs ,
const TR & rhs ,
const decltype ( lhs + rhs ) rmin = 0 ,
const decltype ( lhs + rhs ) rmax = 360 )
{
auto c = lhs + rhs ;
auto range = rmax - rmin ;
while ( c > = rmax ) c - = range ;
while ( c < rmin ) c + = range ;
return c ;
}
2024-01-15 01:30:06 -06:00
//Converts unit distances to pixels. (Every 100 units = 24 pixels)
2024-09-05 14:24:01 -05:00
long double operator " " _Pixels ( long double unitDist ) ;