Skip to content

Commit

Permalink
added vignette, hit flash, fire spread, and more
Browse files Browse the repository at this point in the history
  • Loading branch information
SleepiDreamer committed Apr 21, 2023
1 parent 0689395 commit 95b88ff
Show file tree
Hide file tree
Showing 18 changed files with 105 additions and 35 deletions.
2 changes: 1 addition & 1 deletion bullet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ namespace Tmpl8
{
//sprite->DrawScaled(pos.x, pos.y, size.x * scale, size.y * scale, _screen, false); // draw sprite centered around point
_screen->CircleShadow({ pos.x - 5, pos.y + 5 }, 6 * scale, 0.25f);
_screen->CircleFull(pos, 0.0f * scale, 3.0f * scale, 0xff4400);
_screen->CircleFull(pos, 0.0f * scale, 3.0f * scale, color);
}
};
5 changes: 4 additions & 1 deletion bullet.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "surface.h"
#include "template.h"
#include "entity.h"
#include "util.h"

namespace Tmpl8 {
class Bullet : public Entity
Expand All @@ -10,6 +11,7 @@ namespace Tmpl8 {
bool active;
float scale = 2.0f; // factor to multiply the sprite size with
int id;
int color;
public:
void setActive(bool _active) { active = _active; }
bool getActive() const { return active; }
Expand All @@ -26,10 +28,11 @@ namespace Tmpl8 {
else size = { 5, 5 };
damage = 10.0f;
active = false;
color = rgbCombine(randint(0xdd, 0xff), randint(0x33, 0x55), 0);
}

void update(float _dt);

void render(const Surface* _screen) const;
};
}
}
2 changes: 1 addition & 1 deletion data.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5750
4060
3 changes: 3 additions & 0 deletions enemy.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "enemy.h"

#include "surface.h"
#include "player.h"
#include "util.h"

Expand All @@ -8,13 +9,15 @@ namespace Tmpl8
void Enemy::update(float _dt)
{
pos += vel * _dt;
if (whiteFlashTimer > 0.0f) { whiteFlashTimer -= _dt * 12; }
}

void Enemy::render(Surface* _screen)
{
//sprite->DrawScaled(this->getTopLeft().x, this->getTopLeft().y, sprite->GetWidth() * scale, sprite->GetHeight() * scale, _screen, false); // draw centered around point
srand(id);
int color = pastels[id % static_cast<int>(pastels.size())];
if (whiteFlashTimer > 0.0f) { color = static_cast<int>(AlphaBlend(color, 0xffffff, whiteFlashTimer)); }
screen->CircleShadow({ pos.x - 5, pos.y + 5 }, 27, 0.7f);
screen->CircleFull(pos, 0, 20, color);
}
Expand Down
4 changes: 3 additions & 1 deletion enemy.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace Tmpl8 {
bool active;
float hitboxSize = 20.0f;
float scale = 1.0f;
float whiteFlashTimer = 0.0f;
std::vector<int> pastels = { 0xB2A4FF, 0xFFB4B4, 0xFFDEB4, 0xFDF7C3, 0xF7C8E0, 0xDFFFD8, 0xB4E4FF, 0x95BDFF };

public:
Expand All @@ -23,6 +24,7 @@ namespace Tmpl8 {
void setId(int _id) { id = _id; }
int getId() const { return id; }
float getHitboxSize() const { return hitboxSize * scale; }
void setWhiteFlashTimer(float _whiteFlashTimer) { whiteFlashTimer = _whiteFlashTimer; }

Enemy(vec2 _pos, Surface* _screen, std::shared_ptr<Sprite> _sprite, int _id) :
Entity(_pos, _screen, std::move(_sprite))
Expand All @@ -33,7 +35,7 @@ namespace Tmpl8 {
if (sprite != nullptr) size = { static_cast<float>(sprite->GetWidth()) * scale, static_cast<float>(sprite->GetHeight()) * scale };
else size = { 50, 50 };
damage = 0;
hp = 50;
hp = 60;
}

void update(float _dt);
Expand Down
1 change: 1 addition & 0 deletions enemyManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ namespace Tmpl8
if (distanceBetween(_bullet->getPos(), enemy->getPos()) < enemy->getHitboxSize() + _bullet->getHitboxSize())
{
enemy->setHP(enemy->getHP() - _bullet->getDamage());
enemy->setWhiteFlashTimer(1.0f);
if (enemy->getHP() <= 0)
{
disable(enemy->getId());
Expand Down
33 changes: 13 additions & 20 deletions game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ unsigned int constexpr BG_COLOR = 0x363636;
unsigned int constexpr MENU_BG_COLOR = 0x000000;

// TODO: upgrades?
// TODO: enemies white on hit
// TODO: bullet spread
// TODO: randomize bullet color

namespace Tmpl8
{
Expand Down Expand Up @@ -101,22 +104,6 @@ namespace Tmpl8
// ---*--- MOVEMENT ---*---
player->update(dt);
player->PointTowards(mousePos);
if (player->canDash() && GetAsyncKeyState(VK_UP))
{
player->dash( {0, -1} ); // UP
}
if (player->canDash() && GetAsyncKeyState(VK_DOWN))
{
player->dash({0, 1}); // DOWN
}
if (player->canDash() && GetAsyncKeyState(VK_LEFT))
{
player->dash({-1, 0}); // LEFT
}
if (player->canDash() && GetAsyncKeyState(VK_RIGHT))
{
player->dash({1, 0}); // RIGHT
}

// ---*--- ENEMIES ---*---
enemyPool.update(player->getPos(), dt);
Expand Down Expand Up @@ -152,9 +139,12 @@ namespace Tmpl8
else canShoot = player->canShoot();
if (mouseLeftDown && canShoot)
{
float damage = 25.0f;
float damage = 20.0f;
if (powerupType == 1) damage *= 3.0f;
bulletPool.enable(player->getPos(), player->getDir() * 1000.0f , 1.0f, damage);
const float playerDir = atan2(player->getDir().x, player->getDir().y);
const float shootDir = -playerDir + randfloat(-0.03f, 0.03f) + PI / 2.0f;
const vec2 shootVel = { cos(shootDir) * 1000.0f, sin(shootDir) * 1000.0f };
bulletPool.enable(player->getPos(), shootVel , 1.0f, damage);
player->resetShotTimer();
}
bulletPool.update(player->getPos(), dt);
Expand Down Expand Up @@ -210,7 +200,7 @@ namespace Tmpl8
case 2: powerupTimer = POWERUP_DURATION; break; // fire rate
case 3: powerupTimer = POWERUP_DURATION; break; // invincibility
case 4: powerupTimer = POWERUP_DURATION; break; // speed
case 5: powerupTimer = POWERUP_MESSAGE_DURATION; player->setHp(min(player->getHp() + 1, 3)); break; // health
case 5: powerupTimer = POWERUP_MESSAGE_DURATION; particlePool.playerHealed(player->getPos()); player->setHp(min(player->getHp() + 1, 3)); break; // health
case 6: powerupTimer = POWERUP_MESSAGE_DURATION; particlePool.clear(); enemyPool.clear(); score += enemyPool.getActiveEnemies() * 10; break; // nuke
}
score += 10;
Expand Down Expand Up @@ -245,6 +235,7 @@ namespace Tmpl8
enemyPool.render(screen);

player->render();
screen->Vignette(0.15f);
screen->PrintScaled(("Score: " + std::to_string(score)).c_str(), 10, 10, 5, 5, 0xdddddd);
for (int i = 0; i < player->getMaxHp(); i++)
{
Expand All @@ -267,6 +258,7 @@ namespace Tmpl8
screen->CentreBar(ScreenHeight - 20, ScreenHeight - 10, static_cast<int>(powerupTimer / POWERUP_DURATION * 300.0f), 0xffffff);
if (powerupType == 6) screen->Bar(0, 0, ScreenWidth - 1, ScreenHeight - 1, 0xffffff, powerupTimer / POWERUP_MESSAGE_DURATION);


// ---*--- DEATH MECHANIC ---*---
time += dt;

Expand Down Expand Up @@ -319,7 +311,8 @@ namespace Tmpl8
screen->PrintScaled("You move automatically!", 25, 100, 3, 3, 0xffffff);
screen->PrintScaled("You must shoot the enemies! They die after 2 hits", 25, 150, 3, 3, 0xffffff);
screen->PrintScaled("Powerups spawn randomly on the map. Shoot them to get a surprise!", 25, 200, 3, 3, 0xffffff);
screen->PrintScaled("Good luck!", 25, 250, 3, 3, 0xffffff);
screen->PrintScaled("Good luck!", 25, 300, 3, 3, 0xffffff);
screen->PrintScaled("Try to beat my top score of 11760!", 25, 250, 3, 3, 0xffffff);

// menu button
vec4 menuButtonBox = { 1045, 25, 1230, 95 };
Expand Down
Binary file modified game/tmpl_2019-08_x64.exe
Binary file not shown.
37 changes: 32 additions & 5 deletions particle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,43 @@ namespace Tmpl8
void Particle::update(float _dt)
{
pos += vel * _dt;
vel *= pow(0.00015f, _dt); // dampening
lifetime -= _dt;
switch (type)
{
case 0: // enemy splatter
{
vel *= pow(0.00015f, _dt); // dampening
break;
}
case 1: // player heal
{
vel.y -= 50.0f * _dt;
break;
}
}
}

void Particle::render(const Surface* _screen) const
{
//sprite->DrawScaled(pos.x, pos.y, size.x * scale, size.y * scale, _screen, false); // draw sprite centered around point
const float alpha = min(1.0f, lifetime) * 0.85f;
_screen->CircleShadow({ pos.x - 5, pos.y + 5 }, scale * radius * 1.3f, 0.25f * alpha);
const int color = pastels[id % static_cast<int>(pastels.size())];
_screen->CircleFull(pos, 0, static_cast<int>(scale * radius * 1.3f), color, alpha);
switch (type) {
case 0:
{
const float alpha = min(1.0f, lifetime) * 0.9f;
_screen->CircleShadow({ pos.x - 5, pos.y + 5 }, scale * radius * 1.3f, 0.25f * alpha * 2);
const int color = pastels[id % static_cast<int>(pastels.size())];
_screen->CircleFull(pos, 0, static_cast<int>(scale * radius * 1.3f), color, alpha * 2);
break;
}
case 1:
{
const float alpha = min(1.0f, lifetime) * 0.85f;
_screen->CircleShadow({ pos.x - 5, pos.y + 5 }, scale * radius * 1.3f, 0.25f * alpha);
const int color = 0xe80000;
_screen->CircleFull(pos, 0, static_cast<int>(scale * radius * 1.3f), color, alpha);
break;
}
}

}
};
3 changes: 3 additions & 0 deletions particle.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace Tmpl8 {
bool active;
float scale = 1.0f; // factor to multiply the sprite size with
int id;
int type;
float radius = 5.0f;
float lifetime;
std::vector<int> pastels = { 0xB2A4FF, 0xFFB4B4, 0xFFDEB4, 0xFDF7C3, 0xF7C8E0, 0xDFFFD8, 0xB4E4FF, 0x95BDFF };
Expand All @@ -23,6 +24,8 @@ namespace Tmpl8 {
int getId() const { return id; }
float getLifetime() const { return lifetime; }
void setLifetime(const float _lifetime) { lifetime = _lifetime; }
int getType() const { return type; }
void setType(const int _type) { type = _type; }

Particle(vec2 _pos, Surface* _screen, std::shared_ptr<Sprite> _sprite, const int _id) :
Entity(_pos, _screen, std::move(_sprite))
Expand Down
15 changes: 13 additions & 2 deletions particleManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Tmpl8
}
}

void ParticleManager::enable(vec2 _pos, vec2 _vel, float _scale, float _lifetime)
void ParticleManager::enable(vec2 _pos, vec2 _vel, float _scale, float _lifetime, int _type)
{
Particle* element = pool[n_active++];
element->setActive(true);
Expand All @@ -21,6 +21,7 @@ namespace Tmpl8
element->setSize(_scale);
element->setLifetime(_lifetime);
element->setScale(_scale);
element->setType(_type);
}

void ParticleManager::disable(int _id)
Expand Down Expand Up @@ -79,7 +80,17 @@ namespace Tmpl8
const float angle = randfloat(0, 2 * PI);
vec2 vel = { cos(angle), sin(angle) };
vel *= randfloat(150.0f, 500.0f);
enable(_pos, vel, randfloat(0.75f, 1.25f), randfloat(1.5f, 2.5f));
enable(_pos, vel, randfloat(0.75f, 1.25f), randfloat(0.75f, 1.25f), 0);
}
}

void ParticleManager::playerHealed(vec2 _pos)
{
for (int i = 0; i < 5; i++)
{
const vec2 pos = { randfloat(_pos.x - 30, _pos.x + 30), randfloat(_pos.y - 30, _pos.y + 30) };
enable(pos, 0, randfloat(1.25f, 2.0f), randfloat(0.75f, 1.25f), 1);
}
}

}
6 changes: 5 additions & 1 deletion particleManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ namespace Tmpl8 {
std::vector<Particle*> pool;
int n_active = 0;
int size;
// TYPES:
// 0: enemy splatter
// 1: player heal
public:
void init(Surface* _screen, const std::shared_ptr<Sprite>& _sprite);
void enable(vec2 _pos, vec2 _vel, float _scale, float _lifetime);
void enable(vec2 _pos, vec2 _vel, float _scale, float _lifetime, int _type);
void disable(int id);
int getActiveBullets() const { return n_active; }
int getSize() const { return size; }
Expand All @@ -29,6 +32,7 @@ namespace Tmpl8 {
void clear();

void enemyDied(vec2 _pos);
void playerHealed(vec2 _pos);

ParticleManager(int _size)
{
Expand Down
2 changes: 0 additions & 2 deletions powerup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ namespace Tmpl8
screen->Bar(pos * scale- size.x / 2, pos * scale+ size.x / 2, 0xffa071); // body
screen->BoxThick(pos - size.x / 2, pos + size.x / 2, 5, 0xfedd9e); // outline
screen->PrintScaled("?", pos.x - 15, pos.y - 12, 5, 5, 0xffffff); // question mark


}
}
};
13 changes: 13 additions & 0 deletions surface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,19 @@ namespace Tmpl8 {
return 0;
}

void Surface::Vignette(float _strength)
{
for (int x = 0; x < m_Width; x++)
{
for (int y = 0; y < m_Height; y++)
{
const float dist = distanceBetween({ static_cast<float>(x), static_cast<float>(y) }, { static_cast<float>(m_Width / 2), static_cast<float>(m_Height / 2) });
const float strength = (dist / (m_Width / 2));
Plot(x, y, 0x000000, strength * _strength);
}
}
}

void Surface::CopyTo( Surface* a_Dst, int a_X, int a_Y ) const
{
Pixel* dst = a_Dst->GetBuffer();
Expand Down
3 changes: 3 additions & 0 deletions surface.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ inline Pixel SubBlend( Pixel a_Color1, Pixel a_Color2 )
return static_cast<Pixel>(red + green + blue);
}

Pixel AlphaBlend(Pixel dest, Pixel src, float alpha);

class Surface
{
enum { OWNER = 1 };
Expand Down Expand Up @@ -83,6 +85,7 @@ class Surface
bool CheckVisibility(float x1, float y1, float x2, float y2, float bx1, float by1, float bx2, float by2); // OLD
bool CheckFullVisibility(float x1, float y1, float x2, float y2, float bx1, float by1, float bx2, float by2); // OLD
int Visibility(float x1, float y1, float x2, float y2, float bx1, float by1, float bx2, float by2); // OLD
void Vignette(float _strength);
private:
// Attributes
Pixel* m_Buffer{nullptr};
Expand Down
Binary file modified tmpl_2019-08_x64.exe
Binary file not shown.
9 changes: 8 additions & 1 deletion util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include <string>
#include <charconv>

#include "surface.h"

#define HYPOT(x, y) sqrt((x) * (x) + (y) * (y))

namespace Tmpl8
Expand Down Expand Up @@ -116,7 +118,7 @@ namespace Tmpl8
file.close();
}

//https://codereview.stackexchange.com/questions/175566/compute-shortest-distance-between-point-and-a-rectangle
//https://www.shadertoy.com/view/MtBGWc
float distanceToRect(double x, double y, double x_min, double y_min, double x_max, double y_max)
{
if (x < x_min) {
Expand All @@ -140,4 +142,9 @@ namespace Tmpl8
{
return distanceToRect(static_cast<int>(_point.x), static_cast<int>(_point.y), static_cast<int>(_pos1.x), static_cast<int>(_pos1.y), static_cast<int>(_pos2.x), static_cast<int>(_pos2.y));
}

int rgbCombine(int r, int g, int b)
{
return (r << 16) + (g << 8) + b;
}
}
2 changes: 2 additions & 0 deletions util.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,6 @@ namespace Tmpl8
float distanceToRect(double x, double y, double x_min, double y_min, double x_max, double y_max);

float distanceToRect(vec2 _point, vec2 _pos1, vec2 _pos2);

int rgbCombine(int r, int g, int b);
}

0 comments on commit 95b88ff

Please sign in to comment.