Setup Item Drop rendering/physics. Handle items landing on the ground. Config file options related to item drops added. Added a spawning item function.
parent
a29df8b954
commit
403690747d
@ -0,0 +1,123 @@ |
||||
#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) |
||||
:item(item),pos(pos){ |
||||
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; |
||||
} |
||||
|
||||
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 |
||||
|
||||
game->view.DrawRotatedDecal(pos-vf2d{0,GetZ()},item->Decal(),0,item->Decal()->sprite->Size()/2,{"ItemDrop.Item Drop Scale"_F,"ItemDrop.Item Drop Scale"_F}); |
||||
game->SetDecalMode(DecalMode::ADDITIVE); |
||||
game->view.DrawRotatedDecal(pos-vf2d{0,GetZ()},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)),255}); |
||||
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; |
||||
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(); |
||||
drop.pos+=pointVel*(1/dist*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 |
||||
} |
||||
} |
||||
|
||||
float ItemDrop::GetZ(){ |
||||
return z; |
||||
} |
||||
|
||||
void ItemDrop::SpawnItem(ItemInfo*item,vf2d pos){ |
||||
drops.push_back(ItemDrop{item,pos}); |
||||
} |
@ -0,0 +1,59 @@ |
||||
#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 "olcPixelGameEngine.h" |
||||
#include "Item.h" |
||||
#include "util.h" |
||||
#include "Crawler.h" |
||||
|
||||
class ItemDrop{ |
||||
friend class Crawler; |
||||
vf2d pos; |
||||
vf2d speed{}; |
||||
float zSpeed=0; |
||||
float z=0; |
||||
float randomSpinOffset=0; |
||||
ItemInfo*item=nullptr; |
||||
bool upperLevel=false; |
||||
static float gravity; |
||||
static std::vector<ItemDrop>drops; |
||||
ItemDrop(ItemInfo*item,vf2d pos); |
||||
public: |
||||
static void Initialize(); |
||||
vf2d GetPos(); |
||||
bool OnUpperLevel(); |
||||
void Draw(); |
||||
static void UpdateDrops(float fElapsedTime); |
||||
float GetZ(); |
||||
static void SpawnItem(ItemInfo*item,vf2d pos); |
||||
}; |
Loading…
Reference in new issue