The open source repository for the action RPG game in development by Sig Productions titled 'Adventures in Lestoria'! https://forums.lestoria.net
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
AdventuresInLestoria/Adventures in Lestoria/IBullet.cpp

283 lines
9.9 KiB

#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 "SoundEffect.h"
INCLUDE_ANIMATION_DATA
INCLUDE_game
INCLUDE_GFX
INCLUDE_MONSTER_LIST
INCLUDE_WINDOW_SIZE
IBullet::IBullet(vf2d pos,vf2d vel,float radius,int damage,bool upperLevel,bool friendly,Pixel col,vf2d scale,float image_angle)
:pos(pos),vel(vel),radius(radius),damage(damage),col(col),friendly(friendly),upperLevel(upperLevel),scale(scale),image_angle(image_angle){};
IBullet::IBullet(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,float image_angle,std::string_view hitSound)
:pos(pos),vel(vel),radius(radius),damage(damage),col(col),animated(true),rotates(rotatesWithAngle),lifetime(lifetime),hitsMultiple(hitsMultiple),friendly(friendly),upperLevel(upperLevel),scale(scale),image_angle(image_angle),hitSound(std::string(hitSound)){
this->animation.AddState(animation,ANIMATION_DATA.at(animation));
this->animation.ChangeState(internal_animState,animation);
};
Animate2D::Frame IBullet::GetFrame()const{
return animation.GetFrame(internal_animState);
}
void IBullet::UpdateFadeTime(float fElapsedTime){
aliveTime+=fElapsedTime;
if(fadeInTime>0){
if(fadeInTimer<fadeInTime){
fadeInTimer=std::min(fadeInTime,fadeInTimer+fElapsedTime);
}
}
if(fadeOutTime>0){
if(fadeOutTimer==0){
lifetime=fadeOutTime;
}
fadeOutTimer+=fElapsedTime;
}
}
void IBullet::Update(float fElapsedTime){}
void IBullet::SimulateUpdate(const float fElapsedTime){
simulated=true;
_Update(fElapsedTime);
simulated=false;
}
void IBullet::_Update(const float fElapsedTime){
if(dead)return;
UpdateFadeTime(fElapsedTime);
Update(fElapsedTime);
animation.UpdateState(internal_animState,fElapsedTime);
const bool CollisionCheckRequired=IsActivated()&&fadeInTimer==fadeInTime&&radius!=0.f;
if(CollisionCheckRequired){
float totalDistance=(vel*fElapsedTime).mag();
int iterations=int(std::max(1.f,(vel*fElapsedTime).mag()));
int totalIterations=iterations;
vf2d finalBulletPos=pos+vel*fElapsedTime;
if(IsPlayerAutoAttackProjectile()){finalBulletPos+=(game->GetWindSpeed()*game->GetElapsedTime())/float(totalIterations);}
distanceTraveled+=totalDistance/24.f*100.f;
const auto CollisionCheck=[&](){
if(simulated)return true;
if(friendly){
for(std::shared_ptr<Monster>&m:MONSTER_LIST){
if(geom2d::overlaps(m->BulletCollisionHitbox(),geom2d::circle(pos,radius))){
if(hitList.find(&*m)==hitList.end()){
HurtDamageInfo damageData{&*m,damage,OnUpperLevel(),z,HurtFlag::NONE};
ModifyOutgoingDamageData(damageData);
uint8_t markStacksBeforeHit{m->GetMarkStacks()};
if(m->Hurt(damageData)){
if(!hitsMultiple){
if(_MonsterHit(*m,markStacksBeforeHit)==BulletDestroyState::DESTROY){
dead=true;
}
return false;
}else _MonsterHit(*m,markStacksBeforeHit);
hitList.insert(&*m);
}
}
}
}
} else {
if(geom2d::overlaps(game->GetPlayer()->Hitbox(),geom2d::circle(pos,radius))){
if(hitList.find(game->GetPlayer())==hitList.end()){
HurtDamageInfo damageData{game->GetPlayer(),damage,OnUpperLevel(),z,HurtFlag::NONE};
//NOTE: OnHurt() will potentially change the Damage Data, passing it along to bullet children that might need to modify the flags!
ModifyOutgoingDamageData(damageData);
if(game->GetPlayer()->Hurt(damageData)){
if(!hitsMultiple){
if(_PlayerHit(&*game->GetPlayer())==BulletDestroyState::DESTROY){
dead=true;
}
return false;
}else _PlayerHit(&*game->GetPlayer());
hitList.insert(game->GetPlayer());
}
}
}
}
return true;
};
while(iterations>0){
iterations--;
if(IsPlayerAutoAttackProjectile()){pos+=(game->GetWindSpeed()*game->GetElapsedTime())/float(totalIterations);}
pos+=(vel*fElapsedTime)/float(totalIterations);
if(!CollisionCheck()){
goto DeadBulletCheck;
}
}
pos=finalBulletPos;
if(!CollisionCheck()){
goto DeadBulletCheck;
}
}else{
if(IsPlayerAutoAttackProjectile()){pos+=game->GetWindSpeed()*game->GetElapsedTime();}
pos+=vel*fElapsedTime;
}
DeadBulletCheck:
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 IBullet::_Draw()const{
Pixel blendCol=col;
if(fadeInTime==0&&fadeOutTime==0)blendCol.a=col.a;
else if(fadeOutTime>0)blendCol.a=uint8_t(util::lerp(col.a,0,1-((fadeOutTime-fadeOutTimer)/fadeOutTime)));
else if(fadeInTime>0)blendCol.a=uint8_t(util::lerp(col.a,0,((fadeInTime-fadeInTimer)/fadeInTime)));
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);
}
const bool NotOnTitleScreen=GameState::STATE!=GameState::states[States::MAIN_MENU];
if(NotOnTitleScreen
&&(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.
}
Draw(blendCol);
}
void IBullet::Draw(const Pixel blendCol)const{
if(animated){
game->view.DrawPartialRotatedDecal(pos-vf2d{0,GetZ()}+drawOffsetY,GetFrame().GetSourceImage()->Decal(),rotates?atan2(vel.y,vel.x)-PI/2+image_angle:image_angle,GetFrame().GetSourceRect().size/2,GetFrame().GetSourceRect().pos,GetFrame().GetSourceRect().size,scale,blendCol);
}else{
game->view.DrawRotatedDecal(pos-vf2d{0,GetZ()}+drawOffsetY,GFX["circle.png"].Decal(),image_angle,GFX["circle.png"].Sprite()->Size()/2.f,scale,blendCol);
game->view.DrawRotatedDecal(pos-vf2d{0,GetZ()}+drawOffsetY,GFX["circle_outline.png"].Decal(),image_angle,GFX["circle.png"].Sprite()->Size()/2.f,scale,Pixel{WHITE.r,WHITE.g,WHITE.b,blendCol.a});
}
}
BulletDestroyState IBullet::_PlayerHit(Player*player){
const BulletDestroyState destroyBullet=PlayerHit(player);
if(iframeTimerOnHit>0.f)player->ApplyIframes(iframeTimerOnHit);
if(hitSound)SoundEffect::PlaySFX(hitSound.value(),pos);
return destroyBullet;
}
BulletDestroyState IBullet::_MonsterHit(Monster&monster,const uint8_t markStacksBeforeHit){
const BulletDestroyState destroyBullet=MonsterHit(monster,markStacksBeforeHit);
if(iframeTimerOnHit>0.f)monster.ApplyIframes(iframeTimerOnHit);
if(hitSound)SoundEffect::PlaySFX(hitSound.value(),pos);
return destroyBullet;
}
BulletDestroyState IBullet::PlayerHit(Player*player){
if(!hitsMultiple)fadeOutTime=0.15f;
return BulletDestroyState::KEEP_ALIVE;
}
BulletDestroyState IBullet::MonsterHit(Monster&monster,const uint8_t markStacksBeforeHit){
if(!hitsMultiple)fadeOutTime=0.15f;
return BulletDestroyState::KEEP_ALIVE;
}
bool IBullet::OnUpperLevel(){return upperLevel;}
const bool IBullet::IsDead()const{
return dead;
}
const float IBullet::GetZ()const{
return z;
}
IBullet&IBullet::SetIframeTimeOnHit(float iframeTimer){
iframeTimerOnHit=iframeTimer;
return *this;
}
IBullet&IBullet::SetFadeinTime(float fadeInTime){
const float durationDiff=fadeInTime-fadeInTimer;
this->fadeInTime=fadeInTime;
lifetime+=durationDiff;
return *this;
}
const float&IBullet::GetFadeoutTimer()const{
return fadeOutTimer;
}
IBullet&IBullet::SetIsPlayerAutoAttackProjectile(){
playerAutoAttackProjectile=true;
return *this;
}
const bool IBullet::IsPlayerAutoAttackProjectile()const{
return playerAutoAttackProjectile;
}
void IBullet::AddVelocity(vf2d vel){
this->vel+=vel*game->GetElapsedTime();
}
void IBullet::SetBulletType(const BulletType type){
this->type=type;
}
const BulletType IBullet::GetBulletType()const{
return type;
}
const bool IBullet::IsActivated()const{
return !IsDeactivated();
}
const bool IBullet::IsDeactivated()const{
return deactivated||fadeOutTime>0.f;
}
void IBullet::Deactivate(){
deactivated=true;
}
const double IBullet::GetTimeAlive()const{
return aliveTime;
}
const double IBullet::GetAliveTime()const{
return aliveTime;
}