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/Monster.h

252 lines
10 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
#pragma once
#include "Animation.h"
#include "State.h"
#include "Buff.h"
#include "olcUTIL_Animate2D.h"
#include "DEFINES.h"
#include "Attributable.h"
#include "safemap.h"
#include "Pathfinding.h"
#include "GameEvent.h"
#include "TMXParser.h"
#include "MonsterData.h"
INCLUDE_ITEM_DATA
struct DamageNumber;
class AiL;
enum class Attribute;
enum class MonsterAnimation{
IDLE,
JUMP,
SHOOT,
DEATH
};
class GameEvent;
class Monster:IAttributable{
friend struct STRATEGY;
friend class AiL;
friend class InventoryCreator;
public:
Monster()=delete;
Monster(vf2d pos,MonsterData data,bool upperLevel=false,bool bossMob=false);
const vf2d&GetPos()const;
const int GetHealth()const;
const int GetMaxHealth()const;
const float GetRemainingHPPct()const;
int GetAttack();
float GetMoveSpdMult();
//Obtains the size multiplier (from 0.f-1.f).
float GetSizeMult()const;
Animate2D::Frame GetFrame()const;
bool Update(float fElapsedTime);
//Returns true when damage is actually dealt. Provide whether or not the attack is on the upper level or not. Monsters must be on the same level to get hit by it. (there is a death check and level check here.)
//If you need to hurt multiple enemies try AiL::HurtEnemies()
bool Hurt(int damage,bool onUpperLevel,float z);
bool IsAlive();
vf2d&GetTargetPos();
Key GetFacingDirection()const;
//Will make the monster face in the correct direction relative to a given target point to look at.
void UpdateFacingDirection(vf2d facingTargetPoint);
void Draw()const;
void DrawReflection(float drawRatioX,float multiplierX);
void Collision(Player*p);
void Collision(Monster&p);
void Collision();
void SetVelocity(vf2d vel);
//Returns false if the monster could not be moved to the requested location due to collision.
bool SetPos(vf2d pos);
//Returns false if the monster could not be moved to the requested location due to collision.
bool SetX(float x);
//Returns false if the monster could not be moved to the requested location due to collision.
bool SetY(float y);
void PerformJumpAnimation();
void PerformShootAnimation();
void PerformIdleAnimation();
void PerformNPCDownAnimation();
void PerformNPCUpAnimation();
void PerformNPCLeftAnimation();
void PerformNPCRightAnimation();
void PerformOtherAnimation(const uint8_t otherInd);
bool OnUpperLevel();
void Moved();
//Returns false if a path could not be found.
bool StartPathfinding(float pathingTime);
void PathAroundBehavior(float fElapsedTime);
void AddBuff(BuffType type,float duration,float intensity);
std::vector<Buff>GetBuffs(BuffType buff)const;
//Removes all buffs of a given type.
void RemoveBuff(BuffType type);
State::State GetState();
void SetState(State::State newState);
static void InitializeStrategies();
const bool HasIframes()const;
const float GetZ()const;
void SetZ(float z);
const std::function<void(Monster&,float,std::string)>&GetStrategy()const;
void SetSize(float newSize,bool immediate=true);
geom2d::circle<float>Hitbox();
void SetStrategyDrawFunction(std::function<void(AiL*,Monster&,const std::string&)>func);
void SetStrategyDrawOverlayFunction(std::function<void(AiL*,Monster&,const std::string&)>func);
std::function<void(AiL*,Monster&,const std::string&)>strategyDraw=[](AiL*pge,Monster&m,const std::string&strategy){};
std::function<void(AiL*,Monster&,const std::string&)>strategyDrawOverlay=[](AiL*pge,Monster&m,const std::string&strategy){};
const ItemAttributable&GetStats()const;
const EventName&GetHurtSound();
const EventName&GetDeathSound();
const EventName&GetWalkSound();
void Knockback(const vf2d&vel);
//Knockup the player for duration amount of seconds, and Zamt pixels.
void Knockup(float duration);
const bool AttackAvoided(const float attackZ)const;
const std::string&GetName()const;
//Rotates this enemy's sprite towards a given location. Also flips it to face the correct direction.
void RotateTowardsPos(const vf2d&targetPos);
const float GetDamageReductionFromBuffs()const;
const float GetCollisionDamage()const;
const bool IsNPC()const;
private:
std::string name;
vf2d pos;
vf2d vel={0,0};
float friction=400;
vf2d target={0,0};
float targetAcquireTimer=0;
vf2d spawnPos;
int hp;
ItemAttributable stats;
float size;
float attackCooldownTimer=0;
float queueShotTimer=0;
float z=0;
float iframe_timer=0;
Key facingDirection=DOWN;
std::string strategy;
State::State state=State::NORMAL;
std::string overlaySprite="";
uint8_t overlaySpriteTransparency=0U;
Animate2D::Animation<std::string>animation;
Animate2D::AnimationState internal_animState;
float randomFrameOffset=0.f;
float deathTimer=0.f;
float monsterHurtSoundCooldown=0.f;
float monsterWalkSoundTimer;
std::vector<Buff>buffList;
std::string GetDeathAnimationName();
float lastHitPlayer=0.0f;
bool canMove=true; //Set to false when stuck due to collisions.
bool upperLevel=false;
vf2d pathTarget={};
Pathfinding::sSpline path;
float pathIndex=0;
float lastHitTimer=0;
float spriteRot=0;
std::shared_ptr<DamageNumber>damageNumberPtr;
int phase=0;
bool diesNormally=true; //If set to false, the monster death is handled in a special way. Set it to true when it's time to die.
float targetSize=0;
bool isBoss=false;
void OnDeath();
bool hasStrategyDeathFunction=false;
NPCData npcData;
float lastPathfindingCooldown=0.f;
std::function<bool(GameEvent&,Monster&,const std::string&)>strategyDeathFunc;
void SetStrategyDeathFunction(std::function<bool(GameEvent&,Monster&,const std::string&)>func);
ItemAttribute&Get(std::string_view attr);
//Returns false if the monster could not be moved to the requested location due to collision.
//If monsterInvoked is true, this means the monster was the one that instantiated this input, and it's not an extra movement done via collision.
//Set monsterInvoked to false when you don't want a movement loop due to collisions.
//Typical usage is monsterInvoked is true on first call, and monsterInvoked is false on all subsequent chained calls.
bool _SetX(float x,const bool monsterInvoked=true);
//Returns false if the monster could not be moved to the requested location due to collision.
//If monsterInvoked is true, this means the monster was the one that instantiated this input, and it's not an extra movement done via collision.
//Set monsterInvoked to false when you don't want a movement loop due to collisions.
//Typical usage is monsterInvoked is true on first call, and monsterInvoked is false on all subsequent chained calls.
bool _SetY(float y,const bool monsterInvoked=true);
float knockUpTimer=0.f;
float totalKnockupTime=0.f;
float knockUpZAmt=0.f;
//Spawns the drops a monster would drop as if they were defeated. Returns what items were dropped and their amounts.
std::map<ItemInfo*,uint16_t>SpawnDrops();
private:
struct STRATEGY{
static int _GetInt(Monster&m,std::string param,std::string strategy,int index=0);
static float _GetFloat(Monster&m,std::string param,std::string strategy,int index=0);
static Pixel _GetPixel(Monster&m,std::string param,std::string strategy,int index=0);
static const std::string&_GetString(Monster&m,std::string param,std::string strategy,int index=0);
static datafile _Get(Monster&m,std::string param,std::string strategy);
static void RUN_STRATEGY(Monster&m,float fElapsedTime);
static void RUN_TOWARDS(Monster&m,float fElapsedTime,std::string strategy);
static void SHOOT_AFAR(Monster&m,float fElapsedTime,std::string strategy);
static void TURRET(Monster&m,float fElapsedTime,std::string strategy);
static void SLIMEKING(Monster&m,float fElapsedTime,std::string strategy);
static void RUN_AWAY(Monster&m,float fElapsedTime,std::string strategy);
static void FROG(Monster&m,float fElapsedTime,std::string strategy);
static void WOLF(Monster&m,float fElapsedTime,std::string strategy);
static void BEAR(Monster&m,float fElapsedTime,std::string strategy);
static void URSULE(Monster&m,float fElapsedTime,std::string strategy);
static void NPC(Monster&m,float fElapsedTime,std::string strategy);
static void BOAR(Monster&m,float fElapsedTime,std::string strategy);
};
};
struct MonsterSpawner{
private:
vf2d pos;
vf2d range;
std::vector<std::pair<std::string,vf2d>>monsters;
bool triggered=false;
bool upperLevel=false;
std::string bossNameDisplay="";
public:
MonsterSpawner();
//For the monster list, the second pair item is the position relative to the spawner to spawn the monster.
MonsterSpawner(vf2d pos,vf2d range,std::vector<std::pair<std::string,vf2d>>MONSTER_LIST,bool upperLevel=false,std::string bossNameDisplay="");
bool SpawnTriggered();
vf2d GetRange();
vf2d GetPos();
bool DoesUpperLevelSpawning();
void SetTriggered(bool trigger,bool spawnMonsters=true);
friend std::ostream&operator<<(std::ostream&os,MonsterSpawner&rhs);
};