Skip to content

Commit

Permalink
BLOOD, sparks (incomplete)
Browse files Browse the repository at this point in the history
  • Loading branch information
Matqyou committed Dec 10, 2024
1 parent 4763cf9 commit 552faa8
Show file tree
Hide file tree
Showing 14 changed files with 207 additions and 6 deletions.
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ set(PROJECT_FILES
"src/game/interface/LevelUpMenu.cpp"
"src/game/interface/LevelUpMenu.h"
src/game/weapons/projectile/PatersonNavy.cpp
src/game/weapons/projectile/PatersonNavy.h)
src/game/weapons/projectile/PatersonNavy.h
src/client/Particles.cpp
src/client/Particles.h)

add_executable(${PROJECT_NAME} ${BUILD_OPTIONS} ${PROJECT_FILES})

Expand Down
Binary file added bin/assets/images/entity/character/blood.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added bin/assets/images/entity/projectile/spark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 0 additions & 2 deletions src/GameReference.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ void GameReference::UpdateDimensions(int width, int height) {
m_Height = height;
m_Width2 = width / 2.0;
m_Height2 = height / 2.0;


}

bool GameReference::InitializeSDL() {
Expand Down
99 changes: 99 additions & 0 deletions src/client/Particles.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
//
// Created by Matq on 10/12/2024.
//

#include "Particles.h"

#include <utility>
#include "../technical stuff/TextManager.h"
#include "../game/GameWorld.h"

void Particle::Tick() {
m_Vel *= m_VelDamping;
m_OrientationVel *= m_OrientationVelDamping;
m_Position += m_Vel;
m_Orientation += m_OrientationVel;


if (m_Lifetime == 0 || m_Vel.Length() <= 0.001)
m_Active = false;

m_Lifetime--;
}

void Particle::Draw(Drawing* drawing) const {
SDL_FRect asd = {
(float)m_Position.x,
(float)m_Position.y,
(float)m_Size.x,
(float)m_Size.y,
};

drawing->RenderTextureExFCamera(m_Texture->SDLTexture(), nullptr, asd, m_Orientation, nullptr, SDL_FLIP_NONE);
}

Particles::Particles(GameWorld* game_world) {
m_World = game_world;
m_Drawing = game_world->GameWindow()->Render();
m_CreateParticleIndex = 0;

for (auto & particle : m_Particles)
particle = Particle();
}

bool Particles::PlayParticle(Particle particle) {
bool success = false;
for (int i = 0; i < 1024; i++) {
// Occupy space if available
if (!m_Particles[m_CreateParticleIndex].m_Active) {
m_Particles[m_CreateParticleIndex] = std::move(particle);
success = true;
}

// Go to the next index
m_CreateParticleIndex++;
if (m_CreateParticleIndex >= 1024)
m_CreateParticleIndex = 0;

// Leave if found a space successfully
if (success) return true;
}

// There was no space for the particle
return false;
}

void Particles::Tick() {
for (auto& particle : m_Particles) {
if (!particle.m_Active)
continue;

if (particle.m_Position.x < 0) {
particle.m_Position.x = 0;
particle.m_Vel.x *= -1;
}
if (particle.m_Position.x > m_World->GetWidth()) {
particle.m_Position.x = m_World->GetWidth();
particle.m_Vel.x *= -1;
}
if (particle.m_Position.y < 0) {
particle.m_Position.y = 0;
particle.m_Vel.y *= -1;
}
if (particle.m_Position.y > m_World->GetHeight()) {
particle.m_Position.y = m_World->GetHeight();
particle.m_Vel.y *= -1;
}

particle.Tick();
}
}

void Particles::Draw() {
for (auto& particle : m_Particles) {
if (!particle.m_Active)
continue;

particle.Draw(m_Drawing);
}
}
69 changes: 69 additions & 0 deletions src/client/Particles.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//
// Created by Matq on 10/12/2024.
//

#pragma once

#include "../technical stuff/Drawing.h"
#include "../technical stuff/Vec2.h"

struct Particle {
Texture* m_Texture;
Vec2d m_Position;
Vec2d m_Size;
Vec2d m_Vel;
double m_VelDamping;
double m_Orientation;
double m_OrientationVel;
double m_OrientationVelDamping;
unsigned long long m_Lifetime;
bool m_Active;

Particle() {
m_Texture = nullptr;
m_Position = {};
m_Size = {};
m_Vel = {};
m_Orientation = {};
m_OrientationVel = {};
m_Active = false;
}
Particle(Texture* texture, const Vec2d& pos, const Vec2d& size, const Vec2d& vel, double vel_damping, double orientation, double orientation_vel, double orientation_vel_damping, unsigned long long lifetime) {
m_Texture = texture;
m_Position = pos;
m_Size = size;
m_Vel = vel;
m_VelDamping = vel_damping;
m_Orientation = orientation;
m_OrientationVel = orientation_vel;
m_OrientationVelDamping = orientation_vel_damping;
m_Lifetime = lifetime;

m_Active = true;
}

void Tick();
void Draw(Drawing* drawing) const;

};

class GameWorld;
class Particles {
private:
GameWorld* m_World;
Drawing* m_Drawing;
Particle m_Particles[1024];
size_t m_CreateParticleIndex;

public:
explicit Particles(GameWorld* game_world);

// Manipulating
bool PlayParticle(Particle particle);

// Ticking
void Tick();
void Draw();

};

4 changes: 4 additions & 0 deletions src/game/GameWorld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
GameWorld::GameWorld(GameReference *game_window, int width, int height)
{
m_GameWindow = game_window;
m_Particles = new Particles(this);
m_Tiles = new TileMap(game_window->Render(), 32, width, height);
m_Width = m_Tiles->TotalWidth();
m_Height = m_Tiles->TotalHeight();
Expand Down Expand Up @@ -47,6 +48,7 @@ GameWorld::GameWorld(GameReference *game_window, int width, int height)
GameWorld::~GameWorld()
{
delete m_Tiles;
delete m_Particles;

Entity *CurrentEntity = m_Last;
while (CurrentEntity)
Expand Down Expand Up @@ -435,6 +437,7 @@ void GameWorld::Tick()
TickSpawner();
TickEntities();
TickDestroy();
m_Particles->Tick();
TickCamera();

m_CurrentTick++;
Expand All @@ -454,6 +457,7 @@ void GameWorld::Draw()
Render->SetColor(255, 0, 0, 255);
Render->DrawRectCamera(DrawRect);

m_Particles->Draw();
for (auto Current : m_FirstType)
for (; Current; Current = Current->NextType())
Current->Draw();
Expand Down
3 changes: 3 additions & 0 deletions src/game/GameWorld.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "indicators/TextSurface.h"
#include "collision/TileMap.h"
#include "../Protocol.h"
#include "../client/Particles.h"

class Player;
class Entity;
Expand All @@ -18,6 +19,7 @@ class GameWorld {
private:
GameReference* m_GameWindow;
TileMap* m_Tiles;
Particles* m_Particles;
double m_Width, m_Height;
double m_ShowNamesVisibility;
bool m_ShowNames;
Expand Down Expand Up @@ -53,6 +55,7 @@ class GameWorld {

// Getting
[[nodiscard]] GameReference* GameWindow() const { return m_GameWindow; }
[[nodiscard]] Particles* GetParticles() const { return m_Particles; };
[[nodiscard]] double GetWidth() const { return m_Width; }
[[nodiscard]] double GetHeight() const { return m_Height; }
[[nodiscard]] double GetNamesShown() const { return m_ShowNamesVisibility < 0.1 ? 0.0 : m_ShowNamesVisibility; }
Expand Down
1 change: 1 addition & 0 deletions src/game/entities/Entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ const char* Entity::toString() const {
// Add some velocity to this characters
void Entity::Accelerate(const Vec2d& direction) {
m_Core.Vel += direction;
std::cout << FStringColors("Accelerate %f, %f", m_Core.Vel.x, m_Core.Vel.y) << std::endl;
}

void Entity::Tick() {
Expand Down
14 changes: 12 additions & 2 deletions src/game/entities/Projectile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ LoadedTexture Projectile::sTextureBurst("entity.projectile.burst");
LoadedTexture Projectile::sTextureShotgun("entity.projectile.shotgun");
LoadedTexture Projectile::sTextureSniper("entity.projectile.sniper");
LoadedTexture Projectile::sTextureMinigun("entity.projectile.minigun");
LoadedTexture Projectile::sTextureSpark("entity.projectile.spark");
LoadedSound Projectile::sMetalImpactSounds[2] = {
LoadedSound("entity.projectile.impact.metal.1"),
LoadedSound("entity.projectile.impact.metal.2"),
Expand Down Expand Up @@ -101,7 +102,7 @@ void Projectile::TickCollision() {
if (Entity->GetType() == CHARACTER_ENTITY) {
auto ShootableCharacter = (Character*)Entity;
ShootableCharacter->Damage(m_Damage, m_Shooter);
ShootableCharacter->Accelerate(m_Core.Vel * 0.01 * m_Damage);
ShootableCharacter->Accelerate(direction * 0.5 * m_Damage);
} else if (Entity->GetType() == CRATE_ENTITY) {
auto ShootableCrate = (Crate*)Entity;
ShootableCrate->Damage(m_Damage, m_Shooter);
Expand All @@ -120,6 +121,15 @@ void Projectile::TickWallCollision() {
if (m_Core.Pos.x < 0 || m_Core.Pos.x > m_World->GetWidth() ||
m_Core.Pos.y < 0 || m_Core.Pos.y > m_World->GetHeight()) {
sMetalImpactSounds[rand() % 2].GetSound()->PlaySound();

if (m_Core.Pos.x < 0 || m_Core.Pos.x > m_World->GetWidth()) { m_Core.Vel.x *= -1; }
if (m_Core.Pos.y < 0 || m_Core.Pos.y > m_World->GetHeight()) { m_Core.Vel.y *= -1; }
for (int i = 0; i < 6; i++) {
auto vel = Vec2d(m_Core.Vel.x * (0.15 + 0.1 * (double)(rand()) / RAND_MAX) + 0.05 * (double)(rand()) / RAND_MAX,
m_Core.Vel.y * (0.15 + 0.1 * (double)(rand()) / RAND_MAX) + 0.05 * (double)(rand()) / RAND_MAX);
m_World->GetParticles()->PlayParticle(Particle(sTextureSpark.GetTexture(), m_Core.Pos, Vec2d(3.0, 5.0), vel, 0.98, 0.0, 0.0, 1.0, 20));
}

m_Alive = false;
}
}
Expand All @@ -133,7 +143,7 @@ void Projectile::Tick() {

void Projectile::Draw() {
Drawing* Render = m_World->GameWindow()->Render();
double Angle = std::atan2(m_Core.Vel.y, m_Core.Vel.x) / M_PI * 180.0 + 90.0;
double Angle = std::atan2(m_Core.Vel.y, m_Core.Vel.x) / M_PI * 180.0 - 90.0;
SDL_Rect BulletRect = { int(m_Core.Pos.x - m_Core.Size.x / 2.0),
int(m_Core.Pos.y - m_Core.Size.y / 2.0),
int(m_Core.Size.x),
Expand Down
1 change: 1 addition & 0 deletions src/game/entities/Projectile.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Projectile : public Entity {
static LoadedTexture sTextureShotgun;
static LoadedTexture sTextureSniper;
static LoadedTexture sTextureMinigun;
static LoadedTexture sTextureSpark;
static LoadedSound sMetalImpactSounds[2];

Projectile(GameWorld* world,
Expand Down
13 changes: 13 additions & 0 deletions src/game/entities/characters/character/Character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ LoadedTexture Character::sTextureErrorHealersParadise("icons.healing");
LoadedTexture Character::sTextureErrorRanged("icons.ranged");
LoadedTexture Character::sTextureErrorSlowDown("icons.slow");
LoadedTexture Character::sTextureErrorDangerousRecoil("icons.golden_apple");
LoadedTexture Character::BLOOD("entity.character.blood");

// Link sounds
LoadedSound Character::sHitSounds[3] = {
Expand Down Expand Up @@ -473,6 +474,18 @@ void Character::AmmoPickup(AmmoBox* ammo_box) {
}

void Character::EventDeath() {
// Play a toned down version particle effect :)
BLOOD.GetTexture()->SetColorMod(255, 0, 0); //
auto particles = m_World->GetParticles();
for (int i = 0; i < 50; i++) {
Vec2d vel = { m_Core.Vel.x * (double)(rand()) / RAND_MAX + 2.0 * ((double)(rand()) / RAND_MAX * 2.0 - 1.0),
m_Core.Vel.y * (double)(rand()) / RAND_MAX + 2.0 * ((double)(rand()) / RAND_MAX * 2.0 - 1.0) };

double size = 5.0 + (double)(rand()) / RAND_MAX * 10.0;
double orientation = (double)(rand()) / RAND_MAX * 360.0;
particles->PlayParticle(Particle(BLOOD.GetTexture(), m_Core.Pos, Vec2d(size, size), vel, 0.95, orientation, 20, 0.98, 200));
}

for (int i = 0; i < NUM_WEAPONS; i++) {
if (!m_Weapons[i])
continue;
Expand Down
1 change: 1 addition & 0 deletions src/game/entities/characters/character/Character.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ class Character : public DirectionalEntity {
static LoadedTexture sTextureErrorRanged;
static LoadedTexture sTextureErrorSlowDown;
static LoadedTexture sTextureErrorDangerousRecoil;
static LoadedTexture BLOOD;
static LoadedSound sHitSounds[3];
static LoadedSound sInvincibleHitSound;
static LoadedSound sDeathSound;
Expand Down
2 changes: 1 addition & 1 deletion src/game/weapons/projectile/WeaponShotgun.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ WeaponShotgun::WeaponShotgun(Character* owner)
m_PelletCount = 6;
m_BaseRecoilForce = 20.0;
m_RecoilForce = m_BaseRecoilForce;
m_Damage = 6;
m_Damage = 8;

SetSpread(10.0, 3);
SetRandomProjectileSpeed(13.0, 0.5, 1);
Expand Down

0 comments on commit 552faa8

Please sign in to comment.