# pragma region License
/*
License ( OLC - 3 )
~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
Copyright 2018 - 2023 OneLoneCoder . 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 .
*/
# pragma endregion
# include "ItemDrop.h"
# include "olcUTIL_Geometry2D.h"
# include "Crawler.h"
INCLUDE_game
INCLUDE_GFX
float ItemDrop : : gravity ;
std : : vector < ItemDrop > ItemDrop : : drops ;
void ItemDrop : : Initialize ( ) {
gravity = " ItemDrop.Item Drop Gravity " _F ;
}
ItemDrop : : ItemDrop ( ItemInfo * item , vf2d pos , bool isUpper )
: item ( item ) , pos ( pos ) , upperLevel ( isUpper ) {
speed . x = util : : random ( " ItemDrop.Item Drop Horizontal Speed " _f [ 1 ] - " ItemDrop.Item Drop Horizontal Speed " _f [ 0 ] ) + " ItemDrop.Item Drop Horizontal Speed " _f [ 0 ] ;
speed . y = util : : random ( " ItemDrop.Item Drop Vertical Speed " _f [ 1 ] - " ItemDrop.Item Drop Vertical Speed " _f [ 0 ] ) + " ItemDrop.Item Drop Vertical Speed " _f [ 0 ] ;
zSpeed = " ItemDrop.Item Drop Initial Rise Speed " _F ;
randomSpinOffset = util : : random ( PI / 2 ) ;
}
vf2d ItemDrop : : GetPos ( ) {
return pos ;
}
bool ItemDrop : : OnUpperLevel ( ) {
return upperLevel ;
}
void ItemDrop : : Draw ( ) {
# pragma region Item Drop Shadow Rendering
if ( GetZ ( ) > 0 ) {
vf2d shadowScale = vf2d { 8 * " ItemDrop.Item Drop Scale " _F / 3.f , 1 } / std : : max ( 1.f , GetZ ( ) / 24 ) ;
game - > view . DrawDecal ( GetPos ( ) - vf2d { 3 , 3 } * shadowScale / 2 + vf2d { 0 , 6 * " ItemDrop.Item Drop Scale " _F } , GFX [ " circle.png " ] . Decal ( ) , shadowScale , BLACK ) ;
}
# pragma endregion
float yOffset = 0 ;
if ( GetZ ( ) = = 0 ) {
yOffset = sin ( ( game - > levelTime + randomSpinOffset ) * 3 ) * 0.5 ;
}
game - > view . DrawRotatedDecal ( pos - vf2d { 0 , GetZ ( ) + yOffset } , GFX [ " skill_overlay_icon_overlay.png " ] . Decal ( ) , 0 , GFX [ " skill_overlay_icon_overlay.png " ] . Decal ( ) - > sprite - > Size ( ) / 2 , { " ItemDrop.Item Drop Scale " _F , " ItemDrop.Item Drop Scale " _F } , YELLOW ) ;
game - > view . DrawRotatedDecal ( pos - vf2d { 0 , GetZ ( ) + yOffset } , item - > Decal ( ) , 0 , item - > Decal ( ) - > sprite - > Size ( ) / 2 , { " ItemDrop.Item Drop Scale " _F , " ItemDrop.Item Drop Scale " _F } , { 255 , 255 , 255 , 128 } ) ;
game - > SetDecalMode ( DecalMode : : ADDITIVE ) ;
game - > view . DrawRotatedDecal ( pos - vf2d { 0 , GetZ ( ) + yOffset } , item - > Decal ( ) , 0 , item - > Decal ( ) - > sprite - > Size ( ) / 2 , { " ItemDrop.Item Drop Scale " _F , " ItemDrop.Item Drop Scale " _F } , { uint8_t ( abs ( sin ( game - > levelTime * 1.5 ) * 255.f ) ) , uint8_t ( abs ( sin ( game - > levelTime * 1.5 ) * 255.f ) ) , uint8_t ( abs ( sin ( game - > levelTime * 1.5 ) * 255.f ) ) , 128 } ) ;
game - > SetDecalMode ( DecalMode : : NORMAL ) ;
}
void ItemDrop : : UpdateDrops ( float fElapsedTime ) {
for ( ItemDrop & drop : drops ) {
# pragma region Handle Z Speed
drop . z + = drop . zSpeed * fElapsedTime ;
if ( drop . z < = 0 ) {
drop . zSpeed = 0 ;
drop . z = 0 ;
}
else {
drop . zSpeed + = gravity * fElapsedTime ;
drop . pos + = drop . speed * fElapsedTime ;
}
# pragma endregion
# pragma region Check for Suction / Player pull-in
if ( drop . zSpeed = = 0 & & drop . OnUpperLevel ( ) = = game - > GetPlayer ( ) - > OnUpperLevel ( ) ) {
geom2d : : line < float > lineTo = geom2d : : line < float > ( drop . pos , game - > GetPlayer ( ) - > GetPos ( ) ) ;
float dist = lineTo . length ( ) ;
if ( dist < = " ItemDrop.Item Drop Suction Range " _F ) {
vf2d pointVel = lineTo . vector ( ) . norm ( ) ;
if ( ( 1.f / dist ) * 200 * fElapsedTime > dist ) {
drop . pos = game - > GetPlayer ( ) - > GetPos ( ) ;
drop . collected = true ;
} else {
drop . pos + = pointVel * ( 1.f / std : : min ( 48.f , dist ) ) * " ItemDrop.Item Drop Suction Strength " _F * fElapsedTime ;
}
}
}
# pragma endregion
# pragma region Handle Upper / Lower Level Zone Intersecting
if ( drop . speed . mag ( ) > 0 ) {
std : : map < std : : string , std : : vector < ZoneData > > & zoneData = game - > GetZoneData ( game - > GetCurrentLevel ( ) ) ;
for ( ZoneData & upperLevelZone : zoneData [ " UpperZone " ] ) {
if ( geom2d : : overlaps ( upperLevelZone . zone , drop . pos ) ) {
drop . upperLevel = true ;
}
}
for ( ZoneData & lowerLevelZone : zoneData [ " LowerZone " ] ) {
if ( geom2d : : overlaps ( lowerLevelZone . zone , drop . pos ) ) {
drop . upperLevel = false ;
}
}
}
# pragma endregion
}
std : : erase_if ( drops , [ ] ( ItemDrop & drop ) {
if ( drop . collected ) {
Inventory : : AddItem ( drop . GetItem ( ) - > Name ( ) , 1 , true ) ;
return true ;
}
return false ;
} ) ;
}
float ItemDrop : : GetZ ( ) {
return z ;
}
void ItemDrop : : SpawnItem ( ItemInfo * item , vf2d pos , bool isUpper ) {
drops . push_back ( ItemDrop { item , pos , isUpper } ) ;
}
ItemInfo * ItemDrop : : GetItem ( ) {
return item ;
}