Skip to content

Commit

Permalink
add map projections
Browse files Browse the repository at this point in the history
  • Loading branch information
Nemrav committed Dec 16, 2024
1 parent 8e911c8 commit 0d96d42
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 1 deletion.
61 changes: 61 additions & 0 deletions extension/src/openvic-extension/singletons/MapItemSingleton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,67 @@ TypedArray<Dictionary> MapItemSingleton::get_billboards() const {
return ret;
}


GFX::Projection const* MapItemSingleton::get_projection(std::string_view name, bool error_on_fail) const {
GameSingleton const* game_singleton = GameSingleton::get_singleton();
ERR_FAIL_NULL_V(game_singleton, nullptr);

GFX::Projection const* projection =
game_singleton->get_definition_manager().get_ui_manager().get_cast_object_by_identifier<GFX::Projection>(name);

if (error_on_fail) {
ERR_FAIL_NULL_V_MSG(
projection, nullptr, vformat("Failed to find projection \"%s\"", Utilities::std_to_godot_string(name))
);
}

return projection;
}

bool MapItemSingleton::add_projection_dict(std::string_view name, TypedArray<Dictionary>& projection_dict_array) const {

static const StringName name_key = "name";
static const StringName texture_key = "texture";
static const StringName size_key = "size";
static const StringName spin_key = "spin";
static const StringName expanding_key = "expanding";
static const StringName duration_key = "duration";
static const StringName additative_key = "additative";

GFX::Projection const* projection = get_projection(name, false);

ERR_FAIL_NULL_V_MSG(projection, false, vformat("Failed to find projection \"%s\"", Utilities::std_to_godot_string(name)));

Dictionary dict;

dict[name_key] = Utilities::std_to_godot_string(projection->get_name());
dict[texture_key] = Utilities::std_to_godot_string(projection->get_texture_file());
dict[size_key] = projection->get_size().to_float();
dict[spin_key] = projection->get_spin().to_float();
dict[expanding_key] = projection->get_expanding().to_float();
dict[duration_key] = projection->get_duration().to_float();
dict[additative_key] = projection->get_additative();

projection_dict_array.push_back(dict);

return true;
}

TypedArray<Dictionary> MapItemSingleton::get_projections() const {
GameSingleton const* game_singleton = GameSingleton::get_singleton();
ERR_FAIL_NULL_V(game_singleton, {});

TypedArray<Dictionary> ret;

for (std::unique_ptr<GFX::Object> const& obj : game_singleton->get_definition_manager().get_ui_manager().get_objects()) {
if (obj->is_type<GFX::Projection>()) {
add_projection_dict(obj->get_name(), ret);
}
}

return ret;
}

// We assume GameSingleton isn't null when this is being called
static Vector2 get_billboard_pos(ProvinceDefinition const& province) {
return Utilities::to_godot_fvec2(province.get_city_position()) / GameSingleton::get_singleton()->get_map_dims();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ namespace OpenVic {
GFX::Billboard const* get_billboard(std::string_view name, bool error_on_fail = true) const;
bool add_billboard_dict(std::string_view name, godot::TypedArray<godot::Dictionary>& billboard_dict_array) const;
godot::TypedArray<godot::Dictionary> get_billboards() const;

GFX::Projection const* get_projection(std::string_view name, bool error_on_fail = true) const;
bool add_projection_dict(std::string_view name, godot::TypedArray<godot::Dictionary>& projection_dict_array) const;
godot::TypedArray<godot::Dictionary> get_projections() const;

godot::PackedVector2Array get_province_positions() const;
int32_t get_max_capital_count() const;
godot::PackedVector2Array get_capital_positions() const;
Expand Down
24 changes: 23 additions & 1 deletion game/src/Game/GameSession/MapView.tscn
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
[gd_scene load_steps=8 format=3 uid="uid://dkehmdnuxih2r"]
[gd_scene load_steps=12 format=3 uid="uid://dkehmdnuxih2r"]

[ext_resource type="Script" path="res://src/Game/GameSession/MapView.gd" id="1_exccw"]
[ext_resource type="Shader" path="res://src/Game/GameSession/TerrainMap.gdshader" id="1_upocn"]
[ext_resource type="Script" path="res://src/Game/GameSession/MapText.gd" id="2_13bgq"]
[ext_resource type="Shader" path="res://src/Game/GameSession/Projection.gdshader" id="4_2f4io"]

[sub_resource type="ShaderMaterial" id="ShaderMaterial_tayeg"]
render_priority = 0
Expand All @@ -24,6 +25,24 @@ albedo_color = Color(0, 0, 0, 1)
material = SubResource("StandardMaterial3D_irk50")
size = Vector2(6, 2)

[sub_resource type="ShaderMaterial" id="ShaderMaterial_0txs7"]
render_priority = 0
shader = ExtResource("4_2f4io")
shader_parameter/type = 0
shader_parameter/start_time = 0.0
shader_parameter/sizes = null
shader_parameter/additative = null
shader_parameter/spin = null
shader_parameter/expanding = null
shader_parameter/projections = null

[sub_resource type="QuadMesh" id="QuadMesh_rxujm"]
material = SubResource("ShaderMaterial_0txs7")
orientation = 1

[sub_resource type="MultiMesh" id="MultiMesh_ncb3x"]
mesh = SubResource("QuadMesh_rxujm")

[node name="MapView" type="Node3D" node_paths=PackedStringArray("_camera", "_map_mesh_instance", "_map_background_instance", "_map_text")]
editor_description = "SS-73"
script = ExtResource("1_exccw")
Expand Down Expand Up @@ -56,4 +75,7 @@ light_energy = 1.5
light_bake_mode = 0
sky_mode = 1

[node name="ProjectionManager" type="MultiMeshInstance3D" parent="."]
multimesh = SubResource("MultiMesh_ncb3x")

[connection signal="detailed_view_changed" from="." to="MapText" method="set_visible"]
41 changes: 41 additions & 0 deletions game/src/Game/GameSession/Projection.gdshader
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
shader_type spatial;
render_mode unshaded;

uniform uint type = 0;
uniform float start_time = 0;
//uniform float end_time = 10.0;

uniform sampler2D projections[3] : source_color;
uniform float sizes[3];
uniform bool additative[3]; //if true, black becomes a transparency colour
uniform float spin[3];
uniform float expanding[3];

void vertex() {
// Called for every vertex the material is visible on.
float rot = TIME*spin[type];
mat3 rotation_matrix = mat3(
vec3(cos(-rot), 0.0, sin(-rot)),
vec3(0.0, 1.0, 0.0),
vec3(-sin(-rot), 0.0, cos(-rot))
);
//TIME loops after 3600s = 1hr, so we use modulus to make sure we can
// expand through the hour mark
VERTEX.xyz *= rotation_matrix *
clamp(
expanding[type] * mod(TIME-start_time,3600.0),
0.0,
sizes[type]
);
}

void fragment() {
// Called for every pixel the material is visible on.
vec4 sample = texture(projections[type],UV);
ALBEDO.rgb = sample.rgb;

//if additative, then black = transparent, otherwise use alpha
float add_tr = float(additative[type]);
float not_add_tr = float(!additative[type]);
ALPHA = not_add_tr*sample.a + add_tr*(sample.r+sample.g+sample.b);
}

0 comments on commit 0d96d42

Please sign in to comment.