6 Commits

Author SHA1 Message Date
Maple Redleaf
6b1e840810 Add light spread 2025-12-18 14:33:29 -06:00
Maple Redleaf
5f079d45a3 Resize game for better visual clarity / looks 2025-12-18 12:31:00 -06:00
Maple Redleaf
992907dfad Add Lights Manager, set up Lights, and add Tree. 2025-12-18 12:24:49 -06:00
Maple Redleaf
61168bee04 Fix lights cycle 2025-12-18 11:00:26 -06:00
Maple Redleaf
fd7d44e2ce Finish creating singular lights 2025-12-18 07:37:02 -06:00
Maple
f01cbc782f Begin work on lights 2025-12-15 06:01:58 -06:00
11 changed files with 171 additions and 24 deletions

BIN
assets/tree.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

View File

@@ -1,13 +1,31 @@
#include "Color.hpp"
#include <array>
#include <raylib.h>
#include <sys/types.h>
// SCREEN CONSTS
const int SCREEN_WIDTH = 320;
const int SCREEN_HEIGHT = 240;
const int SCREEN_WIDTH = 80;
const int SCREEN_HEIGHT = 60;
const int WIN_WIDTH = 1280;
const int WIN_HEIGHT = 720;
namespace Snow {
const int MAX_SECS = 4;
const int INV_CHANCE = 5;
const int MAX_CYCLE_FRAMES = 4;
const int INV_HORMOVE_CHANCE = 5;
} // namespace Snow
namespace Lights {
const int MAX_CYCLE_FRAMES = 30;
const std::array<Vector2, 9> POSITIONS{{{28, 17},
{36, 15},
{49, 16},
{22, 31},
{34, 31},
{61, 30},
{59, 42},
{28, 28},
{47, 29}}};
const raylib::Color col1 = raylib::Color::Green();
const raylib::Color col2 = raylib::Color::Red();
} // namespace Lights

38
src/lights/light.cpp Normal file
View File

@@ -0,0 +1,38 @@
#include "light.h"
#include "../consts.cpp"
#include "Vector2.hpp"
#include "raylib.h"
#include <cstdio>
namespace Lights {
Light::Light(raylib::Vector2 position, raylib::Color color1,
raylib::Color color2, int radius, LightState startingState,
int startingCycle)
: position(position), radius(radius), color1(color1), color2(color2),
state(startingState), cycle(startingCycle) {};
Light::~Light() = default;
void Light::Update() {
cycle++;
if (cycle >= MAX_CYCLE_FRAMES) {
SwitchState();
cycle = 0;
}
}
void Light::Draw() {
raylib::Color col = (state == Lights::LightState::color1) ? color1 : color2;
DrawCircleV(position, radius + 1, col.Alpha(0.5));
DrawCircleV(position, radius, col);
}
Lights::LightState Light::SwitchState() {
switch (state) {
case Lights::LightState::color1:
return state = Lights::LightState::color2;
case Lights::LightState::color2:
return state = Lights::LightState::color1;
}
return state;
}
} // namespace Lights

29
src/lights/light.h Normal file
View File

@@ -0,0 +1,29 @@
#include "Color.hpp"
#include "Vector2.hpp"
namespace Lights {
enum LightState {
color1,
color2,
};
class Light {
public:
Light(raylib::Vector2 position, raylib::Color color1, raylib::Color color2,
int radius, LightState startingState, int startingCycle);
void Update();
void Draw();
~Light();
private:
LightState SwitchState();
raylib::Vector2 position;
int radius;
raylib::Color color1;
raylib::Color color2;
LightState state;
int cycle;
};
} // namespace Lights

31
src/lights/manager.cpp Normal file
View File

@@ -0,0 +1,31 @@
#include "manager.h"
#include "../consts.cpp"
#include <memory>
namespace Lights {
Manager::Manager() {
int cycle = 0;
lights.reserve(POSITIONS.size());
for (const auto &position : POSITIONS) {
lights.emplace_back(new Light(
position, col1, col2, 2,
(cycle % 2 == 0) ? LightState::color1 : LightState::color2, 0));
cycle++;
}
};
Manager::~Manager() = default;
void Manager::Update() {
for (auto lighti = 0; lighti < lights.size(); lighti++) {
auto &light = lights[lighti];
light->Update();
}
}
void Manager::Draw() {
for (auto lighti = 0; lighti < lights.size(); lighti++)
lights[lighti]->Draw();
}
} // namespace Lights

16
src/lights/manager.h Normal file
View File

@@ -0,0 +1,16 @@
#include "light.h"
#include <memory>
#include <vector>
namespace Lights {
class Manager {
public:
Manager();
void Update();
void Draw();
~Manager();
private:
std::vector<std::unique_ptr<Light>> lights;
};
} // namespace Lights

View File

@@ -1,4 +1,8 @@
#include "../data/tree.png.h"
#include "Functions.hpp"
#include "Vector2.hpp"
#include "consts.cpp"
#include "lights/manager.h"
#include "snow/manager.h"
#include <raylib-cpp.hpp>
#include <raylib.h>
@@ -17,13 +21,21 @@ raylib::Window window(WIN_WIDTH, WIN_HEIGHT, "game",
raylib::RenderTexture2D target(SCREEN_WIDTH, SCREEN_HEIGHT);
raylib::Image treeImg =
raylib::LoadImageFromMemory(".png", tree_png, tree_png_len);
raylib::Texture2D tree = treeImg.LoadTexture();
Snow::Manager sMgr;
Lights::Manager lMgr;
int main(void) {
window.SetMinSize({320, 240});
window.SetMinSize({SCREEN_WIDTH, SCREEN_HEIGHT});
window.SetTargetFPS(60);
window.SetExitKey(KEY_BACKSPACE);
tree.SetFilter(TEXTURE_FILTER_POINT);
#if defined(PLATFORM_WEB)
emscripten_set_main_loop(MainLoop, 0, 1);
#else
@@ -43,10 +55,13 @@ void MainLoop() {
MIN((float)winWidth / SCREEN_WIDTH, (float)winHeight / SCREEN_HEIGHT);
sMgr.Update();
lMgr.Update();
target.BeginMode();
{
ClearBackground(BLACK);
// ClearBackground(BLACK);
DrawTexture(tree, 0, 0, raylib::Color::White());
lMgr.Draw();
sMgr.Draw();
}
target.EndMode();

View File

@@ -6,7 +6,7 @@ namespace Snow {
Manager::Manager() = default;
Manager::~Manager() = default;
void Manager::NewSnow() { snows.emplace_back(new Snow()); }
void Manager::NewSnow() { snows.emplace_back(new Snowflake()); }
void Manager::NewSnows(int count) {
snows.reserve(snows.size() + count);
for (int i = 0; i < count; i++)

View File

@@ -13,7 +13,7 @@ public:
~Manager();
private:
std::vector<std::unique_ptr<Snow>> snows;
std::vector<std::unique_ptr<Snowflake>> snows;
void Kill(int pos);
int frames = 0;
};

View File

@@ -5,13 +5,13 @@
#include <raylib.h>
namespace Snow {
Snow::Snow() : Snow(GetRandomValue(0, 320)) {}
Snow::Snow(int x) : Snow(raylib::Vector2(x, 0)) {}
Snow::Snow(raylib::Vector2 pos) : position(pos) {}
Snowflake::Snowflake() : Snowflake(GetRandomValue(0, 320)) {}
Snowflake::Snowflake(int x) : Snowflake(raylib::Vector2(x, 0)) {}
Snowflake::Snowflake(raylib::Vector2 pos) : position(pos) {}
void Snow::Move() {
void Snowflake::Move() {
raylib::Vector2 move;
int hMove = GetRandomValue(0, INV_CHANCE);
int hMove = GetRandomValue(0, INV_HORMOVE_CHANCE);
if (hMove <= 1) {
hMove = hMove * 2 - 1;
} else {
@@ -22,16 +22,16 @@ void Snow::Move() {
position += move;
}
bool Snow::IsAlive() { return live; }
bool Snowflake::IsAlive() { return live; }
void Snow::CheckLive() {
void Snowflake::CheckLive() {
if (position.y >= SCREEN_HEIGHT) {
live = false;
}
}
void Snow::Update() {
if (currCycle >= MAX_SECS) {
void Snowflake::Update() {
if (currCycle >= MAX_CYCLE_FRAMES) {
Move();
CheckLive();
currCycle = 0;
@@ -40,7 +40,7 @@ void Snow::Update() {
currCycle++;
}
void Snow::Draw() { raylib::Color::White().DrawPixel(position); }
void Snowflake::Draw() { raylib::Color::White().DrawPixel(position); }
Snow::~Snow() = default;
Snowflake::~Snowflake() = default;
} // namespace Snow

View File

@@ -2,17 +2,17 @@
#include <sys/types.h>
namespace Snow {
class Snow {
class Snowflake {
public:
Snow();
Snow(int x);
Snow(raylib::Vector2 pos);
Snowflake();
Snowflake(int x);
Snowflake(raylib::Vector2 pos);
void Update();
void Draw();
bool IsAlive();
~Snow();
~Snowflake();
private:
raylib::Vector2 position;