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.
230 lines
8.0 KiB
230 lines
8.0 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_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,float image_angle)
|
|
:pos(pos),vel(vel),radius(radius),damage(damage),col(col),friendly(friendly),upperLevel(upperLevel),scale(scale),image_angle(image_angle){};
|
|
|
|
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,float image_angle)
|
|
: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){
|
|
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(fadeInTime>0){
|
|
if(fadeInTimer<fadeInTime){
|
|
fadeInTimer=std::min(fadeInTime,fadeInTimer+fElapsedTime);
|
|
if(fadeInTimer==fadeInTime)deactivated=false;
|
|
}
|
|
}
|
|
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->BulletCollisionHitbox(),geom2d::circle(pos,radius))){
|
|
if(hitList.find(&*m)==hitList.end()&&m->Hurt(damage,OnUpperLevel(),z)){
|
|
if(!hitsMultiple){
|
|
if(_MonsterHit(*m)){
|
|
dead=true;
|
|
}
|
|
return false;
|
|
}else _MonsterHit(*m);
|
|
hitList.insert(&*m);
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
if(geom2d::overlaps(game->GetPlayer()->Hitbox(),geom2d::circle(pos,radius))){
|
|
if(hitList.find(game->GetPlayer())==hitList.end()&&game->GetPlayer()->Hurt(damage,OnUpperLevel(),z)){
|
|
if(!hitsMultiple){
|
|
if(_PlayerHit(&*game->GetPlayer())){
|
|
dead=true;
|
|
}
|
|
return false;
|
|
}else _PlayerHit(&*game->GetPlayer());
|
|
hitList.insert(game->GetPlayer());
|
|
}
|
|
}
|
|
}
|
|
return true;
|
|
};
|
|
|
|
while(iterations>0){
|
|
iterations--;
|
|
pos+=(vel*fElapsedTime)/float(totalIterations);
|
|
if(!CollisionCheck()){
|
|
return;
|
|
}
|
|
}
|
|
pos=finalBulletPos;
|
|
if(!CollisionCheck()){
|
|
return;
|
|
}
|
|
}else{
|
|
pos+=vel*fElapsedTime;
|
|
}
|
|
if(IsPlayerAutoAttackProjectile()){pos+=game->GetWindSpeed()*game->GetElapsedTime();}
|
|
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;
|
|
|
|
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.
|
|
}
|
|
|
|
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"].Sprite()->Size()*scale/2,GFX["circle.png"].Decal(),image_angle,GFX["circle.png"].Sprite()->Size()/2,scale,blendCol);
|
|
game->view.DrawRotatedDecal(pos-vf2d{0,GetZ()}+drawOffsetY-GFX["circle.png"].Sprite()->Size()*scale/2,GFX["circle_outline.png"].Decal(),image_angle,GFX["circle.png"].Sprite()->Size()/2,scale,Pixel{WHITE.r,WHITE.g,WHITE.b,blendCol.a});
|
|
}
|
|
}
|
|
|
|
bool Bullet::_PlayerHit(Player*player){
|
|
const bool destroyBullet=PlayerHit(player);
|
|
if(iframeTimerOnHit>0.f)player->ApplyIframes(iframeTimerOnHit);
|
|
return destroyBullet;
|
|
}
|
|
bool Bullet::_MonsterHit(Monster&monster){
|
|
const bool destroyBullet=MonsterHit(monster);
|
|
if(iframeTimerOnHit>0.f)monster.ApplyIframes(iframeTimerOnHit);
|
|
return destroyBullet;
|
|
}
|
|
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;
|
|
}
|
|
|
|
Bullet&Bullet::SetIframeTimeOnHit(float iframeTimer){
|
|
iframeTimerOnHit=iframeTimer;
|
|
return *this;
|
|
}
|
|
Bullet&Bullet::SetFadeinTime(float fadeInTime){
|
|
const float durationDiff=fadeInTime-fadeInTimer;
|
|
this->fadeInTime=fadeInTime;
|
|
lifetime+=durationDiff;
|
|
deactivated=true;
|
|
return *this;
|
|
}
|
|
|
|
const float&Bullet::GetFadeoutTimer()const{
|
|
return fadeOutTimer;
|
|
}
|
|
|
|
Bullet&Bullet::SetIsPlayerAutoAttackProjectile(){
|
|
playerAutoAttackProjectile=true;
|
|
return *this;
|
|
}
|
|
|
|
const bool Bullet::IsPlayerAutoAttackProjectile()const{
|
|
return playerAutoAttackProjectile;
|
|
}
|
|
|
|
void Bullet::AddVelocity(vf2d vel){
|
|
this->vel+=vel*game->GetElapsedTime();
|
|
} |