Skip to content

Commit

Permalink
added io::color functions for colored text in terminal. fixed project…
Browse files Browse the repository at this point in the history
…ile angle.
  • Loading branch information
vorlac committed Sep 23, 2023
1 parent 5534a7f commit e7912b2
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 51 deletions.
4 changes: 2 additions & 2 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ PenaltyBreakComment: 50
PenaltyBreakFirstLessLess: 120
PenaltyBreakOpenParenthesis: 10
PenaltyBreakString: 1000
PenaltyBreakTemplateDeclaration: 10
PenaltyExcessCharacter: 1000000
PenaltyBreakTemplateDeclaration: 1000
PenaltyExcessCharacter: 10
PenaltyReturnTypeOnItsOwnLine: 100
PenaltyIndentedWhitespace: 0
PointerAlignment: Left
Expand Down
21 changes: 13 additions & 8 deletions project/scenes/projectiles/bullet.tscn
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
[gd_scene load_steps=3 format=3]
[gd_scene load_steps=3 format=3 uid="uid://csam3a1tugjo4"]

[ext_resource type="Texture2D" path="res://assets/art/topdown/tanks_redux/projectiles/bulletdark1_outline.png" id="1_sadn1"]
[ext_resource type="Texture2D" uid="uid://cbx5pr44nef0m" path="res://assets/art/topdown/tanks_redux/projectiles/bulletdark1_outline.png" id="1_sadn1"]

[sub_resource type="CircleShape2D" id="CircleShape2D_70ybn"]
[sub_resource type="CapsuleShape2D" id="CapsuleShape2D_i8hct"]
radius = 10.0
height = 35.0

[node name="Projectile" type="Projectile"]

[node name="Bullet" type="Sprite2D" parent="."]
texture = ExtResource("1_sadn1")
scale = Vector2(0.5, 0.5)

[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
scale = Vector2(0.8, 0.8)
shape = SubResource("CircleShape2D_70ybn")
rotation = 0.785398
scale = Vector2(0.75, 0.75)
shape = SubResource("CapsuleShape2D_i8hct")

[node name="Bullet" type="Sprite2D" parent="CollisionShape2D"]
scale = Vector2(1.33333, 1.33333)
texture = ExtResource("1_sadn1")
5 changes: 5 additions & 0 deletions src/nodes/character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ namespace rl::inline node
}();
}

PlayerController* Character::get_controller() const
{
return m_player_controller;
}

[[signal_slot]]
void Character::on_player_movement(godot::Vector2 movement_velocity, double delta_time)
{
Expand Down
6 changes: 4 additions & 2 deletions src/nodes/character.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ namespace rl::inline node

void _ready() override;

PlayerController* get_controller() const;

protected:
[[property]] double get_movement_speed() const;
[[property]] double get_movement_friction() const;
[[property]] double get_rotation_speed() const;
Expand All @@ -30,8 +33,7 @@ namespace rl::inline node

[[signal_slot]] void on_player_shoot();
[[signal_slot]] void on_player_rotate(double rotation_angle, double delta_time);
[[signal_slot]] void on_player_movement(godot::Vector2 movement_velocity,
double delta_time);
[[signal_slot]] void on_player_movement(godot::Vector2 movement_velocity, double delta_time);

protected:
static void _bind_methods();
Expand Down
24 changes: 11 additions & 13 deletions src/nodes/level.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "nodes/level.hpp"

#include "nodes/character.hpp"
#include "singletons/console.hpp"
#include "util/bind.hpp"
#include "util/conversions.hpp"
#include "util/debug.hpp"
Expand All @@ -14,6 +15,7 @@
namespace rl::inline node
{
Level::Level()
: m_player{ player_scene.instantiate() }
{
// TODO: kill magic string
scene::node::set_unique_name(this, "Level1");
Expand All @@ -22,30 +24,24 @@ namespace rl::inline node

void Level::_ready()
{
resource::preload::scene<Character> player_scene{ path::scene::Player };
m_player = player_scene.instantiate();
m_projectile_spawner = memnew(rl::ProjectileSpawner);

this->add_child(m_player);
this->add_child(m_projectile_spawner);

signal<event::position_changed>::connect<Character>(m_player) <=> [this]() {
return slot(this, on_character_position_changed);
}();
signal<event::position_changed>::connect<PlayerController>(m_player->get_controller()) <=>
slot(this, on_character_position_changed);

signal<event::spawn_projectile>::connect<Character>(m_player) <=> [this]() {
return slot(this, on_character_spawn_projectile);
}();
signal<event::spawn_projectile>::connect<Character>(m_player) <=>
slot(this, on_character_spawn_projectile);
}

void Level::_process(double delta_time)
{
if (engine::editor_active())
return;

if (this->active() && input::cursor_visible()) [[unlikely]]
if (this->active() && input::cursor_visible()) [[likely]]
input::hide_cursor();
if (!this->active() && !input::cursor_visible()) [[unlikely]]
else if (!this->active() && !input::cursor_visible()) [[unlikely]]
input::show_cursor();

this->queue_redraw();
Expand Down Expand Up @@ -91,7 +87,9 @@ namespace rl::inline node
godot::Vector2 location) const
{
runtime_assert(node != nullptr);
log::info(node->get_class() + " location: " + location);
auto console{ Console<godot::RichTextLabel>::get() };
console->print("{} ({},{})\n", io::green(to<std::string>(node->get_class()) + " location: "),
io::orange(location.x), io::orange(location.y));
}

void Level::_bind_methods()
Expand Down
6 changes: 5 additions & 1 deletion src/nodes/level.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#pragma once

#include "nodes/character.hpp"
#include "nodes/projectile_spawner.hpp"
#include "util/bind.hpp"
#include "util/constants.hpp"
#include "util/scene.hpp"

#include <atomic>
#include <vector>
Expand Down Expand Up @@ -37,7 +40,8 @@ namespace rl::inline node

private:
std::atomic<bool> m_active{ false };
resource::preload::scene<Character> player_scene{ path::scene::Player };
ProjectileSpawner* m_projectile_spawner{ memnew(rl::ProjectileSpawner) };
Character* m_player{ nullptr };
ProjectileSpawner* m_projectile_spawner{ nullptr };
};
}
4 changes: 3 additions & 1 deletion src/nodes/player_controller.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "nodes/player_controller.hpp"

#include "util/bind.hpp"
#include "util/constants.hpp"
#include "util/engine.hpp"
Expand Down Expand Up @@ -94,7 +95,7 @@ namespace rl::inline node
godot::Vector2 target_rotation{ input->get_vector("rotate_left", "rotate_right",
"rotate_up", "rotate_down") };
if (!target_rotation.is_zero_approx())
m_rotation_angle = godot::Vector2{ 0, 0 }.angle_to_point(target_rotation) +
m_rotation_angle = godot::Vector2(0, 0).angle_to_point(target_rotation) +
godot::Math::deg_to_rad(90.0);
}
break;
Expand All @@ -109,5 +110,6 @@ namespace rl::inline node
signal_binding<PlayerController, event::player_move>::add<godot::Vector2, double>();
signal_binding<PlayerController, event::player_rotate>::add<double, double>();
signal_binding<PlayerController, event::player_shoot>::add<godot::Object*>();
signal_binding<PlayerController, event::position_changed>::add<godot::Object*, godot::Vector2>();
}
}
5 changes: 5 additions & 0 deletions src/nodes/projectile.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "nodes/projectile.hpp"

#include "util/engine.hpp"

#include <godot_cpp/variant/vector2.hpp>

namespace rl::inline node
Expand All @@ -11,6 +13,9 @@ namespace rl::inline node

void Projectile::_process(double delta_time)
{
if (engine::editor_active())
return;

godot::Vector2 position{ this->get_position() };
position += m_velocity * m_movement_speed * delta_time;
this->set_position(position);
Expand Down
14 changes: 7 additions & 7 deletions src/singletons/console.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ namespace rl
m_static_inst = nullptr;
}

static inline rl::Console<TContext>* get()
{
return m_static_inst;
}

void set_context(TContext* context)
{
m_gui_console = context;
Expand Down Expand Up @@ -62,7 +67,7 @@ namespace rl
const duration_t elapsed{ clock_t::now() - m_start_time };

m_gui_console->append_text(
fmt::format("{:5} [{:>7.2}] [b]=>[/b] [color=yellow]{}[/color]",
fmt::format("[color=gray]{:5} [{:>7.2}] [b]=>[/b] {}[/color]",
m_line_num.fetch_add(1, std::memory_order_relaxed), elapsed,
msg.payload)
.c_str());
Expand All @@ -84,12 +89,7 @@ namespace rl
template <typename... TArgs>
void print(fmt::format_string<TArgs...> format_str, TArgs&&... args)
{
m_logger->info(fmt::format(format_str, std::forward<TArgs...>(args...)));
}

static inline rl::Console<TContext>* get()
{
return m_static_inst;
m_logger->info(format_str, std::forward<TArgs>(args)...);
}

protected:
Expand Down
79 changes: 62 additions & 17 deletions src/util/io.hpp
Original file line number Diff line number Diff line change
@@ -1,33 +1,78 @@
#pragma once

#include <algorithm>
#include <concepts>
#include "util/concepts.hpp"

#include <fmt/compile.h>
#include <fmt/core.h>
#include <fmt/format-inl.h>
#include <fmt/format.h>
#include <functional>
#include <locale>
#include <memory>
#include <ranges>
#include <span>
#include <spdlog/spdlog.h>
#include <string>
#include <string_view>
#include <tuple>
#include <type_traits>
#include <utility>

namespace rl::inline utils
{
namespace io
{
// inline constexpr auto to_lowercase(std::string_view str) noexcept
//{
// std::span<char> sp{ str.begin(), str.end() };
// std::transform(sp.begin(), sp.end(), sp.begin(), [](char c) -> char {
// if (c >= 'A' && c <= 'Z')
// return c - 32;
// return c;
// });
// return str.data();
// }

// constexpr const char* test{ std::move(to_lowercase("ASDF")) };
static inline auto white(auto&& val)
{
return fmt::format(FMT_COMPILE("[color=white]{}[/color]"),
std::forward<decltype(val)>(val));
}

static inline auto gray(auto&& val)
{
return fmt::format(FMT_COMPILE("[color=gray]{}[/color]"),
std::forward<decltype(val)>(val));
}

static inline auto black(auto&& val)
{
return fmt::format(FMT_COMPILE("[color=black]{}[/color]"),
std::forward<decltype(val)>(val));
}

static inline auto red(auto&& val)
{
return fmt::format(FMT_COMPILE("[color=red]{}[/color]"),
std::forward<decltype(val)>(val));
}

static inline auto orange(auto&& val)
{
return fmt::format(FMT_COMPILE("[color=orange]{}[/color]"),
std::forward<decltype(val)>(val));
}

static inline auto yellow(auto&& val)
{
return fmt::format(FMT_COMPILE("[color=yellow]{}[/color]"),
std::forward<decltype(val)>(val));
}

static inline auto green(auto&& val)
{
return fmt::format(FMT_COMPILE("[color=green]{}[/color]"),
std::forward<decltype(val)>(val));
}

static inline auto blue(auto&& val)
{
return fmt::format(FMT_COMPILE("[color=blue]{}[/color]"),
std::forward<decltype(val)>(val));
}

static inline auto purple(auto&& val)
{
return fmt::format(FMT_COMPILE("[color=purple]{}[/color]"),
std::forward<decltype(val)>(val));
}

}

struct log
Expand Down

0 comments on commit e7912b2

Please sign in to comment.