-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add template to basic_engine, add camera move script
- Loading branch information
Showing
9 changed files
with
170 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
extends Camera2D | ||
|
||
# Speed of camera movement | ||
@export var speed = 200 | ||
# Zoom speed | ||
@export var zoom_speed = 0.1 | ||
# Minimum and maximum zoom levels | ||
@export var min_zoom = 0.5 | ||
@export var max_zoom = 2.0 | ||
|
||
func _process(delta): | ||
var motion = Vector2.ZERO | ||
|
||
# Check for movement keys | ||
if Input.is_action_pressed("ui_up"): | ||
motion.y -= 1 | ||
if Input.is_action_pressed("ui_down"): | ||
motion.y += 1 | ||
if Input.is_action_pressed("ui_left"): | ||
motion.x -= 1 | ||
if Input.is_action_pressed("ui_right"): | ||
motion.x += 1 | ||
|
||
# Normalize to prevent faster diagonal movement | ||
motion = motion.normalized() * speed | ||
|
||
# Apply the motion to the camera's position | ||
position += motion * delta | ||
|
||
# Zoom in | ||
if Input.is_action_pressed("ui_zoom_in"): | ||
zoom = Vector2(zoom.x - zoom_speed * delta, zoom.y - zoom_speed * delta) | ||
zoom.x = max(min_zoom, zoom.x) | ||
zoom.y = max(min_zoom, zoom.y) | ||
|
||
# Zoom out | ||
if Input.is_action_pressed("ui_zoom_out"): | ||
zoom = Vector2(zoom.x + zoom_speed * delta, zoom.y + zoom_speed * delta) | ||
zoom.x = min(max_zoom, zoom.x) | ||
zoom.y = min(max_zoom, zoom.y) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,24 @@ | ||
[gd_scene load_steps=3 format=3 uid="uid://dss4vqph6ixgk"] | ||
[gd_scene load_steps=4 format=3 uid="uid://dss4vqph6ixgk"] | ||
|
||
[ext_resource type="PackedScene" uid="uid://djahqxlcbinjp" path="res://board.tscn" id="1_i8xh8"] | ||
[ext_resource type="Script" path="res://FPSCounter.gd" id="2_avnxl"] | ||
[ext_resource type="Script" path="res://Camera2D.gd" id="2_wwmnb"] | ||
|
||
[node name="Main" type="Node"] | ||
|
||
[node name="Board" parent="." instance=ExtResource("1_i8xh8")] | ||
columns = 100 | ||
|
||
[node name="Camera2D" type="Camera2D" parent="."] | ||
offset = Vector2(500, 300) | ||
script = ExtResource("2_wwmnb") | ||
speed = 500 | ||
zoom_speed = 0.5 | ||
min_zoom = 1.0 | ||
|
||
[node name="FPSCounter" type="Label" parent="Camera2D"] | ||
offset_left = -41.0 | ||
offset_top = -24.0 | ||
offset_right = -1.0 | ||
offset_bottom = -1.0 | ||
script = ExtResource("2_avnxl") | ||
|
||
[node name="Board" parent="." instance=ExtResource("1_i8xh8")] | ||
columns = 100 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,111 @@ | ||
#ifndef LIFEPVP_BASIC_ENGINE_H | ||
#define LIFEPVP_BASIC_ENGINE_H | ||
|
||
#include <concepts> | ||
#include <vector> | ||
|
||
#include "engine_base.h" | ||
|
||
namespace lifepvp::engine { | ||
|
||
template<class T> | ||
concept BoardConstructibleBySize = requires (T&& board) { | ||
T(board.size()); | ||
}; | ||
|
||
template<class T> | ||
concept BoardResizable = std::is_default_constructible_v<T> && | ||
requires (T board, size_t x) { | ||
board.resize(x); | ||
}; | ||
|
||
template<class T> | ||
concept BasicEngineContainer = | ||
std::move_constructible<T> && | ||
(BoardResizable<T> || BoardConstructibleBySize<T>) && | ||
requires (T& board, const T& cboard, size_t i) { | ||
{ board[i] } -> std::convertible_to<EngineBase::state_t&>; | ||
{ cboard[i] } -> std::convertible_to<EngineBase::state_t const&>; | ||
}; | ||
|
||
template<BasicEngineContainer T = std::vector<EngineBase::state_t>> | ||
class BasicEngine : public EngineBase { | ||
public: | ||
using board_t = std::vector<state_t>; | ||
using board_t = std::remove_reference_t<T>; | ||
|
||
BasicEngine(board_t&& board, | ||
const size_t w, | ||
const size_t h, | ||
const update_cb_t update_cb, | ||
const done_cb_t done_cb) | ||
: EngineBase(w, h, update_cb, done_cb), | ||
m_board_pair{board, board_t(board.size())} {} | ||
: EngineBase(w, h, update_cb, done_cb) { | ||
if constexpr (BoardConstructibleBySize<board_t>) { | ||
m_board_pair = {board, board_t(board.size())}; | ||
} else { | ||
static_assert(BoardResizable<board_t>); | ||
m_board_pair = {board, board_t()}; | ||
m_board_pair[1].resize(w * h); | ||
} | ||
} | ||
|
||
virtual ~BasicEngine() = default; | ||
void next_iteration() override; | ||
state_t get_state(size_t i, size_t j) override; | ||
|
||
void next_iteration() override { | ||
const auto& curr_board = m_board_pair[m_board]; | ||
auto& next_board = m_board_pair[m_board ^ 1]; | ||
|
||
for (size_t i = 0; i < W; ++i) { | ||
for (size_t j = 0; j < H; ++j) { | ||
static const std::pair<int64_t, int64_t> neighbor_offsets[] | ||
{ | ||
{-1, -1}, {-1, 0}, {-1, 1}, | ||
{ 0, -1}, { 0, 1}, | ||
{ 1, -1}, { 1, 0}, { 1, 1} | ||
}; | ||
size_t count = 0; | ||
for (const auto [x, y] : neighbor_offsets) { | ||
if (at(curr_board, (x+W+i)%W, (y+H+j)%H)) { | ||
++count; | ||
} | ||
} | ||
|
||
const state_t curr_cell = at(curr_board, i, j); | ||
state_t& next_cell = at(next_board, i, j); | ||
if (count < 2 || count > 3) { | ||
next_cell = 0; // die | ||
} else if (count == 3) { | ||
next_cell = 1; // survive or create | ||
} else { // count == 2 | ||
next_cell = curr_cell; // survive | ||
} | ||
|
||
if (next_cell != curr_cell) { | ||
m_update_cb(i, j, next_cell); | ||
} | ||
} | ||
} | ||
|
||
m_board ^= 1; | ||
m_done_cb(); | ||
} | ||
|
||
state_t get_state(size_t i, size_t j) override { | ||
return at(m_board_pair[m_board], i, j); | ||
}; | ||
|
||
protected: | ||
size_t m_board = 0; | ||
std::array<board_t, 2> m_board_pair; | ||
|
||
state_t& at(board_t& v, size_t i, size_t j); | ||
const state_t& at(const board_t& v, size_t i, size_t j) const; | ||
state_t& at(board_t& v, size_t i, size_t j) { | ||
return v[i * W + j]; | ||
}; | ||
|
||
const state_t& at(const board_t& v, size_t i, size_t j) const { | ||
return v[i * W + j]; | ||
}; | ||
}; | ||
|
||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters