Skip to content

Commit

Permalink
Hand positions on guns, changes to guns
Browse files Browse the repository at this point in the history
  • Loading branch information
Matqyou committed Dec 8, 2024
1 parent 927320d commit 5193c29
Show file tree
Hide file tree
Showing 25 changed files with 188 additions and 78 deletions.
Binary file modified bin/assets/images/weapons/Sniper.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
16 changes: 9 additions & 7 deletions src/GameReference.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ void GameReference::Event(const SDL_Event& event) {

void GameReference::TestEnvironment() {
m_GameWorld = new GameWorld(this, 50, 30);
m_GameWorld->SetTestingMode(true); // stop waves
m_Controllers = new GameControllers();

m_Draw->SetWorld(m_GameWorld);
Expand All @@ -251,13 +252,14 @@ void GameReference::TestEnvironment() {
new Error(m_GameWorld, Vec2d(50 + 50 * x, m_GameWorld->GetHeight() - 50), ErrorType(x));

for (int x = 0 ; x < NUM_AMMO_TYPES; x++)
new AmmoBox(m_GameWorld, AmmoType(x), Vec2d(50 + 50 * x, m_GameWorld->GetHeight() - 100), 50);

new EntityGlock(m_GameWorld, nullptr, nullptr, Vec2d(800, 200));
new EntityShotgun(m_GameWorld, nullptr, nullptr, Vec2d(900, 200));
new EntityBurst(m_GameWorld, nullptr, nullptr, Vec2d(1000, 200));
new EntityMinigun(m_GameWorld, nullptr, nullptr, Vec2d(1100, 200));
new EntitySniper(m_GameWorld, nullptr, nullptr, Vec2d(1200, 200));
new AmmoBox(m_GameWorld, AmmoType(x), Vec2d(50 + 50 * x, m_GameWorld->GetHeight() - 100), 1000);

double WeaponsY = m_GameWorld->GetHeight() - 150;
new EntityGlock(m_GameWorld, nullptr, nullptr, Vec2d(100, WeaponsY));
new EntityShotgun(m_GameWorld, nullptr, nullptr, Vec2d(200, WeaponsY));
new EntityBurst(m_GameWorld, nullptr, nullptr, Vec2d(300, WeaponsY));
new EntityMinigun(m_GameWorld, nullptr, nullptr, Vec2d(400, WeaponsY));
new EntitySniper(m_GameWorld, nullptr, nullptr, Vec2d(500, WeaponsY));

auto Player1 = new Player(m_GameWorld, "Keyboard");
auto Char1 = new Character(m_GameWorld,
Expand Down
4 changes: 3 additions & 1 deletion src/game/GameWorld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ GameWorld::GameWorld(GameReference *game_window, int width, int height)
m_GameOver = false;
m_CurrentTick = 0;

m_TestingMode = false;

m_First = nullptr;
m_Last = nullptr;
memset(m_FirstType, 0, sizeof(m_FirstType));
Expand Down Expand Up @@ -335,7 +337,7 @@ void GameWorld::TickCamera()

void GameWorld::TickSpawner()
{
if (m_CurrentTick - m_LastWave < m_TimeBetweenWaves)
if (m_TestingMode || m_CurrentTick - m_LastWave < m_TimeBetweenWaves)
return;

m_LastWave = m_CurrentTick;
Expand Down
3 changes: 3 additions & 0 deletions src/game/GameWorld.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class GameWorld {
Entity* m_First, * m_Last;
unsigned long long m_CurrentTick;

bool m_TestingMode; // Waves paused

// Cool scrolling background cap
Texture* m_Background;
int m_BackgroundW{ }, m_BackgroundH{ };
Expand Down Expand Up @@ -79,6 +81,7 @@ class GameWorld {
void DestroyPlayerByController(GameController* DeletedController) const;
void DestroyCharacterByController(GameController* DeletedController) const;
void ToggleShowNames();
void SetTestingMode(bool testing_mode) { m_TestingMode = testing_mode; }

// Managing
Player* AddPlayer(Player* player);
Expand Down
59 changes: 31 additions & 28 deletions src/game/entities/characters/character/Character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ LoadedTexture Character::sTextureShotgun("weapons.shotgun");
LoadedTexture Character::sTextureBurst("weapons.burst");
LoadedTexture Character::sTextureSniper("weapons.sniper");
LoadedTexture Character::sTexturesMinigun[4] = {
LoadedTexture("weapons.minigun1"),
LoadedTexture("weapons.minigun2"),
LoadedTexture("weapons.minigun3"),
LoadedTexture("weapons.minigun4"),
LoadedTexture("weapons.minigun.1"),
LoadedTexture("weapons.minigun.2"),
LoadedTexture("weapons.minigun.3"),
LoadedTexture("weapons.minigun.4"),
};
LoadedTexture Character::sTextureErrorDisorianted("icons.disoriented");
LoadedTexture Character::sTextureErrorSpiky("icons.cactus");
Expand Down Expand Up @@ -86,7 +86,7 @@ Character::Character(GameWorld* world,
true,
max_health),
m_BaseAcceleration(0.45),
m_Hands(this, 40.0, 10.0),
m_Hands(this, 10.0),
m_Hook(this),
m_HealthBar(world->GameWindow(), &m_HealthComponent, 75, 15, 2, 2),
m_Input(),
Expand Down Expand Up @@ -486,6 +486,11 @@ void Character::EventDeath() {
} else if (i == WEAPON_MINIGUN) {
NewWeapon = new EntityMinigun(m_World, this, (WeaponMinigun*)m_Weapons[WEAPON_MINIGUN], m_Core.Pos);
m_Weapons[WEAPON_MINIGUN] = nullptr;
} else if (i == WEAPON_SNIPER) {
NewWeapon = new EntitySniper(m_World, this, (WeaponSniper*)m_Weapons[WEAPON_SNIPER], m_Core.Pos);
m_Weapons[WEAPON_SNIPER] = nullptr;
} else {
throw std::runtime_error("Unhandled weapon drop on death (TODO)");
}

NewWeapon->Accelerate(m_DirectionalCore.Direction * 5);
Expand Down Expand Up @@ -945,12 +950,13 @@ void Character::DrawCharacter() {

sCharacterTexture.GetTexture()->SetColorMod(m_CharacterColor.r, m_CharacterColor.g, m_CharacterColor.b);

double Angle = m_Core.Vel.Atan2() / M_PI * 180.0;
Vec2d PointingVector = m_DirectionalCore.Direction * 3.0 + m_Core.Vel / 3.0;
double PointingAngle = PointingVector.Atan2() / M_PI * 180.0;
SDL_RendererFlip flip = SDL_FLIP_NONE;
Render->RenderTextureExFCamera(sCharacterTexture.GetTexture()->SDLTexture(),
nullptr,
DrawRect,
Angle - 90.0,
PointingAngle - 90.0,
nullptr,
flip);
}
Expand Down Expand Up @@ -1026,8 +1032,8 @@ void Character::DrawHealthbar() {
void Character::DrawHands() {
Drawing* Render = m_World->GameWindow()->Render();

double XLook = m_Input.m_LookingX * 5.0;
double YLook = m_Input.m_LookingY * 5.0;
m_Hands.SetColor(m_HandColor);
m_Hands.Draw();

if (m_CurrentWeapon) {
Texture* WeaponTexture;
Expand Down Expand Up @@ -1055,14 +1061,16 @@ void Character::DrawHands() {
}
}

SDL_Rect WeaponRect = { 0, 0,
WeaponTexture->GetWidth(),
WeaponTexture->GetHeight() };
double Radians = m_DirectionalCore.Direction.Atan2();
Vec2d HoldPosition = m_CurrentWeapon->GetHoldPosition();
HoldPosition.Rotate(Radians);

SDL_FRect WeaponRect = { 0, 0, (float)WeaponTexture->GetWidth(), (float)WeaponTexture->GetHeight() };
WeaponRect.w *= 4;
WeaponRect.h *= 4;
WeaponRect.x = int(XLook + m_Core.Pos.x);
WeaponRect.y = int(YLook + m_Core.Pos.y - float(WeaponRect.h) / 2.0);
SDL_Point WeaponPivot = { 0, int(double(WeaponRect.h) / 2.0 * Render->GetZoom()) };
WeaponRect.x = float(m_Core.Pos.x + HoldPosition.x);
WeaponRect.y = float(m_Core.Pos.y + HoldPosition.y - float(WeaponRect.h) / 2.0);
SDL_FPoint WeaponPivot = { 0, float(double(WeaponRect.h) / 2.0 * Render->GetZoom()) };

// Laser sight
if (m_CurrentWeapon->WepType() == WeaponType::WEAPON_SNIPER) {
Expand All @@ -1073,17 +1081,13 @@ void Character::DrawHands() {

// TODO Seperate this into gun classes id say and give gun class a different texture and make bullets spawn from the gun
// and not the center of the player
double Angle = m_DirectionalCore.Direction.Atan2() / M_PI * 180.0;
Render->RenderTextureExCamera(WeaponTexture->SDLTexture(),
Render->RenderTextureExFCamera(WeaponTexture->SDLTexture(),
nullptr,
WeaponRect,
Angle,
Radians / M_PI * 180.0,
&WeaponPivot,
SDL_FLIP_VERTICAL);
}

m_Hands.SetColor(m_HandColor);
m_Hands.Draw();
}

void Character::DrawNameplate() {
Expand Down Expand Up @@ -1123,16 +1127,14 @@ void Character::DrawNameplate() {
// TODO when switching guns ammo text renders again, to prevent this save each ammo count texture on the gun
void Character::DrawAmmoCounter() {
Drawing* Render = m_World->GameWindow()->Render();

char msg[64];
std::snprintf(msg, sizeof(msg), "%u/%u", m_CurrentWeapon->GetMagAmmo(), m_CurrentWeapon->GetTrueAmmo());
m_AmmoCount->SetText(msg);
if (m_CurrentWeapon->GetMagAmmo() == 0)
m_AmmoCount->SetColor({ 255, 0, 0 });
else {
m_AmmoCount->SetColor({ 255, 255, 255 });
}
Texture* AmmoTexture = m_AmmoCount->RequestUpdate();
if (m_CurrentWeapon->GetMagAmmo() == 0) m_AmmoCount->SetColor({ 255, 0, 0 });
else m_AmmoCount->SetColor({ 255, 255, 255 });

Texture* AmmoTexture = m_AmmoCount->RequestUpdate();
int AmmoTextureW = AmmoTexture->GetWidth();
int AmmoTextureH = AmmoTexture->GetHeight();
SDL_Rect AmmoRect = { int(m_Core.Pos.x - AmmoTextureW / 2.0),
Expand Down Expand Up @@ -1274,8 +1276,9 @@ void Character::Draw() {
m_NameplateColor = m_HandColor;

DrawHook();
DrawHands();
DrawHands(); // originally here
DrawCharacter();
// DrawHands(); //
DrawHealthbar();
DrawNameplate();
DrawErrorIcons();
Expand Down
67 changes: 37 additions & 30 deletions src/game/entities/characters/character/Hands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@
LoadedTexture Hands::sFistTexture("entity.character.fist");
LoadedSound Hands::sPunchSound("entity.character.punch");

Hands::Hands(Character* parent, double hand_spacing, double fist_animation_duration)
: m_HandSpacing(hand_spacing / 180.0 * M_PI),
m_FistingAnimationDuration(fist_animation_duration) {
Hands::Hands(Character* parent, double fist_animation_duration)
: m_FistingAnimationDuration(fist_animation_duration) {
m_Parent = parent;

m_LastFisted = 0;
Expand All @@ -22,10 +21,8 @@ Hands::Hands(Character* parent, double hand_spacing, double fist_animation_durat
auto& ParentCore = m_Parent->GetDirectionalCore();
m_BaseFistingRadius = ParentCore.sizeRatio / 2.5;
m_FistingRadius = ParentCore.sizeRatio / 2.5;
m_xLeft = ParentCore.Size.x /2.0;
m_yLeft = ParentCore.Size.y /2.0;
m_xRight = ParentCore.Size.x /2.0;
m_yRight = ParentCore.Size.y /2.0;
m_LeftHand = Vec2d(ParentCore.Size.x / 2.0, -ParentCore.Size.y / 3.0);
m_RightHand = Vec2d(ParentCore.Size.x / 2.0, ParentCore.Size.y / 3.0);
m_Size = ParentCore.sizeRatio * 0.36 *2;
m_Size2 = m_Size / 2.0;
}
Expand Down Expand Up @@ -61,16 +58,12 @@ void Hands::Tick() {
double XHands, YHands;
if (m_LastFistedR < m_LastFistedL) {
m_LastFistedR = CurrentTick;
XHands = ParentCore.Pos.x + std::cos(m_HandSpacing + Radians) * m_xRight
+ m_Parent->GetInput().m_LookingX * m_FistingRadius;
YHands = ParentCore.Pos.y + std::sin(m_HandSpacing + Radians) * m_yRight
+ m_Parent->GetInput().m_LookingY * m_FistingRadius;
XHands = ParentCore.Pos.x + std::cos(Radians) * m_RightHand.x + m_Parent->GetInput().m_LookingX * m_FistingRadius;
YHands = ParentCore.Pos.y + std::sin(Radians) * m_RightHand.y + m_Parent->GetInput().m_LookingY * m_FistingRadius;
} else {
m_LastFistedL = CurrentTick;
XHands = ParentCore.Pos.x + std::cos(-m_HandSpacing + Radians) * m_xLeft
+ m_Parent->GetInput().m_LookingX * m_FistingRadius;
YHands = ParentCore.Pos.y + std::sin(-m_HandSpacing + Radians) * m_yLeft
+ m_Parent->GetInput().m_LookingY * m_FistingRadius;
XHands = ParentCore.Pos.x + std::cos(Radians) * m_LeftHand.x + m_Parent->GetInput().m_LookingX * m_FistingRadius;
YHands = ParentCore.Pos.y + std::sin(Radians) * m_LeftHand.y + m_Parent->GetInput().m_LookingY * m_FistingRadius;
}

auto Ent = World->FirstEntity();
Expand All @@ -81,10 +74,8 @@ void Hands::Tick() {
continue;

EntityCore& EntCore = Ent->GetCore();
double XClosest =
std::max(EntCore.Pos.x - EntCore.Size.x / 2.0, std::min(EntCore.Pos.x + EntCore.Size.x / 2.0, XHands));
double YClosest =
std::max(EntCore.Pos.y - EntCore.Size.y / 2.0, std::min(EntCore.Pos.y + EntCore.Size.y / 2.0, YHands));
double XClosest = std::max(EntCore.Pos.x - EntCore.Size.x / 2.0, std::min(EntCore.Pos.x + EntCore.Size.x / 2.0, XHands));
double YClosest = std::max(EntCore.Pos.y - EntCore.Size.y / 2.0, std::min(EntCore.Pos.y + EntCore.Size.y / 2.0, YHands));
double Distance = std::sqrt(std::pow(XClosest - XHands, 2) + std::pow(YClosest - YHands, 2));
if (Distance > m_FistingRadius)
continue;
Expand All @@ -97,15 +88,15 @@ void Hands::Tick() {
if (Ent->GetType() != CHARACTER_ENTITY)
continue;

Ent->Accelerate(Vec2d(m_Parent->GetInput().m_LookingX, m_Parent->GetInput().m_LookingY) * 5.0);
auto BoostDirection = Vec2d(m_Parent->GetInput().m_LookingX, m_Parent->GetInput().m_LookingY);
Ent->Accelerate(BoostDirection * 5.0);

int Damage = (m_Parent->GetBaseDamage() * m_Parent->GetDamageAmp());
if (!m_Parent->GetPlayer()){
double Damage = m_Parent->GetBaseDamage() * m_Parent->GetDamageAmp();
if (!m_Parent->GetPlayer()) {
((Character*)Ent)->Damage(Damage, m_Parent);
continue;
}
if (((CharacterNPC*)Ent)->IsBoss())
{
if (((CharacterNPC*)Ent)->IsBoss()) {
Damage = Damage * m_Parent->GetPlayer()->GetBossDamageAmp();
}
((Character*)Ent)->Damage(Damage, m_Parent);
Expand All @@ -120,8 +111,8 @@ void Hands::Draw() {
Drawing* Render = World->GameWindow()->Render();

// TODO: Make different hand positions for different weapons (interesting)
if (m_Parent->GetCurrentWeapon()) // state
return;
// if (m_Parent->GetCurrentWeapon()) // state
// return;

auto CurrentTick = World->GetTick();
double XDirection = m_Parent->GetDirectionalCore().Direction.x;
Expand All @@ -137,10 +128,23 @@ void Hands::Draw() {
FistingKoefficientL = (1.0 - FistingKoefficientL) * m_FistingRadius;
FistingKoefficientR = (1.0 - FistingKoefficientR) * m_FistingRadius;

double XOffLeft = std::cos(-m_HandSpacing + Radians) * m_xLeft + XDirection * FistingKoefficientL;
double YOffLeft = std::sin(-m_HandSpacing + Radians) * m_yLeft+ YDirection * FistingKoefficientL;
double XOffRight = std::cos(m_HandSpacing + Radians) * m_xRight + XDirection * FistingKoefficientR;
double YOffRight = std::sin(m_HandSpacing + Radians) * m_yRight + YDirection * FistingKoefficientR;
Vec2d LeftHand, RightHand;
auto CurrentWeapon = m_Parent->GetCurrentWeapon();
if (CurrentWeapon != nullptr) {
LeftHand = CurrentWeapon->GetHoldPosition() + CurrentWeapon->GetLeftHand();
RightHand = CurrentWeapon->GetHoldPosition() + CurrentWeapon->GetRightHand();
} else {
LeftHand = m_LeftHand;
RightHand = m_RightHand;
}
LeftHand.Rotate(Radians);
RightHand.Rotate(Radians);

// Punching related stuff
double XOffLeft = LeftHand.x + XDirection * FistingKoefficientL;
double YOffLeft = LeftHand.y + YDirection * FistingKoefficientL;
double XOffRight = RightHand.x + XDirection * FistingKoefficientR;
double YOffRight = RightHand.y + YDirection * FistingKoefficientR;

SDL_FRect HandRectLeft = { float(ParentCore.Pos.x - m_Size2 + XOffLeft),
float(ParentCore.Pos.y - m_Size2 + YOffLeft),
Expand All @@ -152,4 +156,7 @@ void Hands::Draw() {
sFistTexture.GetTexture()->SetColorMod(m_Color.r, m_Color.g, m_Color.b);
Render->RenderTextureExFCamera(sFistTexture.GetTexture()->SDLTexture(), nullptr, HandRectLeft, Angle, nullptr, SDL_FLIP_NONE);
Render->RenderTextureExFCamera(sFistTexture.GetTexture()->SDLTexture(), nullptr, HandRectRight, Angle, nullptr, SDL_FLIP_NONE);

Render->SetColor(255, 0, 0, 255);
Render->RenderDebugPointCamera(ParentCore.Pos.x, ParentCore.Pos.y);
}
8 changes: 4 additions & 4 deletions src/game/entities/characters/character/Hands.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "../../../../technical stuff/Colors.h"
#include "../../../../client/Assets.h"
#include "../../../../technical stuff/Vec2.h"

class Character;
class Hands {
Expand All @@ -14,19 +15,18 @@ class Hands {
SDL_Color m_Color;
unsigned long long m_LastFisted;
unsigned long long m_LastFistedL, m_LastFistedR;
const double m_HandSpacing;
const double m_FistingAnimationDuration;
double m_FistingRadius, m_BaseFistingRadius;

double m_xLeft, m_yLeft;
double m_xRight, m_yRight;
Vec2d m_LeftHand;
Vec2d m_RightHand;
double m_Size, m_Size2;

static LoadedTexture sFistTexture;
static LoadedSound sPunchSound;

public:
Hands(Character* parent, double hand_spacing, double fist_animation_duration);
Hands(Character* parent, double fist_animation_duration);
~Hands();

// Getting
Expand Down
8 changes: 7 additions & 1 deletion src/game/weapons/projectile/ProjectileWeapon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ double ProjectileWeapon::GenerateRandomProjectileSpeed() const {

ProjectileWeapon::ProjectileWeapon(DirectionalEntity* owner,
WeaponType type,
Sound* reload_sound,
Sound* reload_sound, // Has default
Vec2d* hold_position, // Has default
std::pair<Vec2d, Vec2d>* hand_positions, // Has default
int tick_cooldown,
int ammo_capacity,
int total_ammo_capacity,
Expand All @@ -47,7 +49,11 @@ ProjectileWeapon::ProjectileWeapon(DirectionalEntity* owner,
m_FullRandomProjectileSpeed = 0;
m_Parent = nullptr;

// Defaults
m_ReloadSound = reload_sound == nullptr ? Assets::Get()->GetSound("weapon.default.reload") : reload_sound;
m_HoldPosition = hold_position == nullptr ? Vec2d(5.0, 0.0) : *hold_position;
m_LeftHandPosition = hand_positions == nullptr ? Vec2d(0, -10.0) : hand_positions->first;
m_RightHandPosition = hand_positions == nullptr ? Vec2d(0, 10.0) : hand_positions->second;
}

void ProjectileWeapon::TickTrigger() {
Expand Down
Loading

0 comments on commit 5193c29

Please sign in to comment.