# pragma region License
/*
License ( OLC - 3 )
~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
Copyright 2024 Joshua Sigona < sigonasr2 @ gmail . com >
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 .
Portions of this software are copyright © 2024 The FreeType
Project ( www . freetype . org ) . Please see LICENSE_FT . txt for more information .
All rights reserved .
*/
# pragma endregion
# include "Bullet.h"
# include "AdventuresInLestoria.h"
# include "DEFINES.h"
# include "safemap.h"
# include "util.h"
INCLUDE_ANIMATION_DATA
INCLUDE_game
INCLUDE_GFX
INCLUDE_MONSTER_LIST
INCLUDE_WINDOW_SIZE
Bullet : : Bullet ( vf2d pos , vf2d vel , float radius , int damage , bool upperLevel , bool friendly , Pixel col , vf2d scale )
: pos ( pos ) , vel ( vel ) , radius ( radius ) , damage ( damage ) , col ( col ) , friendly ( friendly ) , upperLevel ( upperLevel ) , scale ( scale ) { } ;
Bullet : : Bullet ( vf2d pos , vf2d vel , float radius , int damage , std : : string animation , bool upperLevel , bool hitsMultiple , float lifetime , bool rotatesWithAngle , bool friendly , Pixel col , vf2d scale )
: pos ( pos ) , vel ( vel ) , radius ( radius ) , damage ( damage ) , col ( col ) , animated ( true ) , rotates ( rotatesWithAngle ) , lifetime ( lifetime ) , hitsMultiple ( hitsMultiple ) , friendly ( friendly ) , upperLevel ( upperLevel ) , scale ( scale ) {
this - > animation . AddState ( animation , ANIMATION_DATA [ animation ] ) ;
this - > animation . ChangeState ( internal_animState , animation ) ;
} ;
Animate2D : : Frame Bullet : : GetFrame ( ) const {
return animation . GetFrame ( internal_animState ) ;
}
void Bullet : : UpdateFadeTime ( float fElapsedTime )
{
if ( fadeOutTime > 0 ) {
if ( fadeOutTimer = = 0 ) {
lifetime = fadeOutTime ;
}
fadeOutTimer + = fElapsedTime ;
}
}
void Bullet : : Update ( float fElapsedTime ) { }
void Bullet : : SimulateUpdate ( const float fElapsedTime ) {
simulated = true ;
_Update ( fElapsedTime ) ;
simulated = false ;
}
void Bullet : : _Update ( const float fElapsedTime ) {
UpdateFadeTime ( fElapsedTime ) ;
Update ( fElapsedTime ) ;
animation . UpdateState ( internal_animState , fElapsedTime ) ;
if ( ! deactivated ) {
float totalDistance = ( vel * fElapsedTime ) . mag ( ) ;
int iterations = int ( std : : max ( 1.f , ( vel * fElapsedTime ) . mag ( ) ) ) ;
int totalIterations = iterations ;
vf2d finalBulletPos = pos + vel * fElapsedTime ;
distanceTraveled + = totalDistance / 24.f * 100.f ;
const auto CollisionCheck = [ & ] ( ) {
if ( simulated ) return true ;
if ( friendly ) {
for ( std : : unique_ptr < Monster > & m : MONSTER_LIST ) {
if ( geom2d : : overlaps ( m - > Hitbox ( ) , geom2d : : circle ( pos , radius ) ) ) {
if ( hitList . find ( & * m ) = = hitList . end ( ) & & m - > Hurt ( damage , OnUpperLevel ( ) , z ) ) {
if ( ! hitsMultiple ) {
if ( MonsterHit ( * m ) ) {
dead = true ;
}
return false ;
}
hitList . insert ( & * m ) ;
}
}
}
} else {
if ( geom2d : : overlaps ( game - > GetPlayer ( ) - > Hitbox ( ) , geom2d : : circle ( pos , radius ) ) ) {
if ( game - > GetPlayer ( ) - > Hurt ( damage , OnUpperLevel ( ) , z ) ) {
if ( PlayerHit ( & * game - > GetPlayer ( ) ) ) {
dead = true ;
}
return false ;
}
}
}
return true ;
} ;
while ( iterations > 0 ) {
iterations - - ;
pos + = ( vel * fElapsedTime ) / float ( totalIterations ) ;
if ( ! CollisionCheck ( ) ) {
return ;
}
}
pos = finalBulletPos ;
if ( ! CollisionCheck ( ) ) {
return ;
}
} else {
pos + = vel * fElapsedTime ;
}
if ( /*World size in PIXELS!*/ vi2d worldSize = game - > GetCurrentMapData ( ) . MapSize * game - > GetCurrentMapData ( ) . TileSize ; pos . x + radius < - WINDOW_SIZE . x | | pos . x - radius > worldSize . x + WINDOW_SIZE . x | | pos . y + radius < - WINDOW_SIZE . y | | pos . y - radius > worldSize . y + WINDOW_SIZE . y ) {
dead = true ;
return ;
}
lifetime - = fElapsedTime ;
if ( lifetime < = 0 ) {
dead = true ;
return ;
}
}
void Bullet : : Draw ( ) const {
Pixel blendCol = col ;
blendCol . a = fadeOutTime = = 0 ? col . a : uint8_t ( util : : lerp ( col . a , 0 , 1 - ( ( fadeOutTime - fadeOutTimer ) / fadeOutTime ) ) ) ;
if ( GetZ ( ) > 0 ) {
vf2d shadowScale = vf2d { 8 * scale . x / 3.f , 1 } / std : : max ( 1.f , GetZ ( ) / 8 ) ;
game - > view . DrawDecal ( pos - vf2d { 3 , 3 } * shadowScale / 2 + vf2d { 0 , 12 * scale . y } , GFX [ " circle.png " ] . Decal ( ) , shadowScale , BLACK ) ;
}
if ( game - > GetPlayer ( ) - > HasIframes ( ) | | game - > GetPlayer ( ) - > GetZ ( ) > 1 ) {
blendCol . a / = 1.59f ; //Comes from 255 divided by 160 which is roughly what we want the alpha to be when the bullet has full transparency.
}
if ( animated ) {
game - > view . DrawPartialRotatedDecal ( pos - vf2d { 0 , GetZ ( ) } , GetFrame ( ) . GetSourceImage ( ) - > Decal ( ) , rotates ? atan2 ( vel . y , vel . x ) - PI / 2 : 0 , GetFrame ( ) . GetSourceRect ( ) . size / 2 , GetFrame ( ) . GetSourceRect ( ) . pos , GetFrame ( ) . GetSourceRect ( ) . size , scale , blendCol ) ;
} else {
game - > view . DrawDecal ( pos - vf2d { 0 , GetZ ( ) } - GFX [ " circle.png " ] . Sprite ( ) - > Size ( ) * scale / 2 , GFX [ " circle.png " ] . Decal ( ) , scale , blendCol ) ;
game - > view . DrawDecal ( pos - vf2d { 0 , GetZ ( ) } - GFX [ " circle.png " ] . Sprite ( ) - > Size ( ) * scale / 2 , GFX [ " circle_outline.png " ] . Decal ( ) , scale , Pixel { WHITE . r , WHITE . g , WHITE . b , blendCol . a } ) ;
}
}
bool Bullet : : PlayerHit ( Player * player ) { return true ; }
bool Bullet : : MonsterHit ( Monster & monster ) { return true ; }
bool Bullet : : OnUpperLevel ( ) { return upperLevel ; }
const bool Bullet : : IsDead ( ) const {
return dead ;
}
const float Bullet : : GetZ ( ) const {
return z ;
}