From b236a198bb9152b11a31ea6f3f940a4027061bed Mon Sep 17 00:00:00 2001 From: Santeri Huotari Date: Fri, 23 Feb 2024 02:02:01 +0200 Subject: [PATCH] Add box sprite and scene --- Tlob.csproj | 8 ++++++++ Tlob.sln | 19 +++++++++++++++++++ scenes/box.tscn | 17 +++++++++++++++++ scenes/main.tscn | 11 ++++++++++- scenes/tlob.cs | 39 +++++++++++++++++++++++++++++++++++++++ scenes/tlob.tscn | 8 +++++++- sprites/box.png | Bin 0 -> 803 bytes sprites/box.png.import | 34 ++++++++++++++++++++++++++++++++++ 8 files changed, 134 insertions(+), 2 deletions(-) create mode 100644 Tlob.csproj create mode 100644 Tlob.sln create mode 100644 scenes/box.tscn create mode 100644 scenes/tlob.cs create mode 100644 sprites/box.png create mode 100644 sprites/box.png.import diff --git a/Tlob.csproj b/Tlob.csproj new file mode 100644 index 0000000..198256e --- /dev/null +++ b/Tlob.csproj @@ -0,0 +1,8 @@ + + + net6.0 + net7.0 + net8.0 + true + + \ No newline at end of file diff --git a/Tlob.sln b/Tlob.sln new file mode 100644 index 0000000..416432b --- /dev/null +++ b/Tlob.sln @@ -0,0 +1,19 @@ +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 2012 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tlob", "Tlob.csproj", "{DAF2229D-5187-4A27-A81D-0555D20817E2}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + ExportDebug|Any CPU = ExportDebug|Any CPU + ExportRelease|Any CPU = ExportRelease|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {DAF2229D-5187-4A27-A81D-0555D20817E2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {DAF2229D-5187-4A27-A81D-0555D20817E2}.Debug|Any CPU.Build.0 = Debug|Any CPU + {DAF2229D-5187-4A27-A81D-0555D20817E2}.ExportDebug|Any CPU.ActiveCfg = ExportDebug|Any CPU + {DAF2229D-5187-4A27-A81D-0555D20817E2}.ExportDebug|Any CPU.Build.0 = ExportDebug|Any CPU + {DAF2229D-5187-4A27-A81D-0555D20817E2}.ExportRelease|Any CPU.ActiveCfg = ExportRelease|Any CPU + {DAF2229D-5187-4A27-A81D-0555D20817E2}.ExportRelease|Any CPU.Build.0 = ExportRelease|Any CPU + EndGlobalSection +EndGlobal diff --git a/scenes/box.tscn b/scenes/box.tscn new file mode 100644 index 0000000..b34e703 --- /dev/null +++ b/scenes/box.tscn @@ -0,0 +1,17 @@ +[gd_scene load_steps=3 format=3 uid="uid://dur0i50oao18u"] + +[ext_resource type="Texture2D" uid="uid://be6qojhnjml47" path="res://sprites/box.png" id="1_uib42"] + +[sub_resource type="RectangleShape2D" id="RectangleShape2D_pnqxx"] +size = Vector2(42, 42) + +[node name="Node2D" type="Node2D"] + +[node name="Sprite2D" type="Sprite2D" parent="."] +scale = Vector2(0.328125, 0.328125) +texture = ExtResource("1_uib42") + +[node name="StaticBody2D" type="StaticBody2D" parent="."] + +[node name="CollisionShape2D" type="CollisionShape2D" parent="StaticBody2D"] +shape = SubResource("RectangleShape2D_pnqxx") diff --git a/scenes/main.tscn b/scenes/main.tscn index 9c6d1d9..db8e123 100644 --- a/scenes/main.tscn +++ b/scenes/main.tscn @@ -1,3 +1,12 @@ -[gd_scene format=3 uid="uid://byuw363qi2nwu"] +[gd_scene load_steps=3 format=3 uid="uid://byuw363qi2nwu"] + +[ext_resource type="PackedScene" uid="uid://cpqrufo1gdajy" path="res://scenes/tlob.tscn" id="1_f4jm3"] +[ext_resource type="PackedScene" uid="uid://dur0i50oao18u" path="res://scenes/box.tscn" id="2_n63uh"] [node name="Main" type="Node2D"] + +[node name="Tlob" parent="." instance=ExtResource("1_f4jm3")] +position = Vector2(0, -3) + +[node name="Node2D" parent="." instance=ExtResource("2_n63uh")] +position = Vector2(635, -362) diff --git a/scenes/tlob.cs b/scenes/tlob.cs new file mode 100644 index 0000000..496c357 --- /dev/null +++ b/scenes/tlob.cs @@ -0,0 +1,39 @@ +using Godot; +using System; + +public partial class tlob : CharacterBody2D +{ + public const float Speed = 300.0f; + public const float JumpVelocity = -400.0f; + + // Get the gravity from the project settings to be synced with RigidBody nodes. + public float gravity = ProjectSettings.GetSetting("physics/2d/default_gravity").AsSingle(); + + public override void _PhysicsProcess(double delta) + { + Vector2 velocity = Velocity; + + // Add the gravity. + if (!IsOnFloor()) + velocity.Y += gravity * (float)delta; + + // Handle Jump. + if (Input.IsActionJustPressed("ui_accept") && IsOnFloor()) + velocity.Y = JumpVelocity; + + // Get the input direction and handle the movement/deceleration. + // As good practice, you should replace UI actions with custom gameplay actions. + Vector2 direction = Input.GetVector("ui_left", "ui_right", "ui_up", "ui_down"); + if (direction != Vector2.Zero) + { + velocity.X = direction.X * Speed; + } + else + { + velocity.X = Mathf.MoveToward(Velocity.X, 0, Speed); + } + + Velocity = velocity; + MoveAndSlide(); + } +} diff --git a/scenes/tlob.tscn b/scenes/tlob.tscn index 220c160..3978d38 100644 --- a/scenes/tlob.tscn +++ b/scenes/tlob.tscn @@ -1,14 +1,20 @@ -[gd_scene load_steps=3 format=3 uid="uid://cpqrufo1gdajy"] +[gd_scene load_steps=4 format=3 uid="uid://cpqrufo1gdajy"] +[ext_resource type="Script" path="res://scenes/tlob.cs" id="1_i865j"] [ext_resource type="Texture2D" uid="uid://dfjrr8hcd3ol" path="res://sprites/tlob.png" id="1_tg6nh"] [sub_resource type="CircleShape2D" id="CircleShape2D_5pvct"] radius = 64.0 [node name="Tlob" type="CharacterBody2D"] +motion_mode = 1 +script = ExtResource("1_i865j") [node name="CollisionShape2D" type="CollisionShape2D" parent="."] shape = SubResource("CircleShape2D_5pvct") [node name="Sprite2D" type="Sprite2D" parent="."] texture = ExtResource("1_tg6nh") + +[node name="Camera2D" type="Camera2D" parent="."] +zoom = Vector2(0.5, 0.5) diff --git a/sprites/box.png b/sprites/box.png new file mode 100644 index 0000000000000000000000000000000000000000..a132a517abd44c5a28d243b50a92f1115919afb3 GIT binary patch literal 803 zcmeAS@N?(olHy`uVBq!ia0vp^4Is?H1|$#LC7xzrU~I{Bb`J1#c2+1T%1_J8No8Qr zm{>c}*5j~)%+dJZrAndf-CY(bEBwj=1q)Y&YB;q9Xm5FyQ1nvVB-0}(MuzozQ?q<` zD7$!itZWRc;|Jy=HxGJVboKfZACw{Z`e32`y}Q+S_A<9bb!LXSi(DwV?Q<+S$!fC} zTbyD7L-UKHYtHB-`%Mzcu-^CX;p5k_E$LhBzdwHwxLJ(3MyYU$v;a0&wAW8#wp7teLkX?WO)CO7x(6-m4g0)?V-NQYZTvZ zJXP7fIc(L2W41rJn1Tm4%<^6#$ejN#hZ zmGC6OK~gH^I{!cR{kyjoO%{#|GGHs&XL0VhhQJNhSE0v$+Lg`u#rdF~ZQYk;`(v2X z>^#?-ZT%p4VLNY3jJatl|BgG1ZS#0;PKiC~AAI6z!*}Cf%Xlu-bC;~I+-v%+@E609 z6|0Z%78G#wJk_k;dv^EksOsO}eHQPRKX$3;goniUr@&}oOY(MiVfYV%3-&Ib3>4uk z@Q5sCV9-+rVaAH3_GLgp_7YEDSN0c7V&dkSpVmG-2o#bmag8W(&d<$F%`0JWE=o-- zNlj5G&n(GMaQE~L2yf&Q2P*pQ>EaktaqI0RM_vX+0hWUg=SesUg`MYe`_aTZY3-y7 zFTb6i6L3lHq`Gr#t>gk5W(fx71O}c4Mgs=60}KPpq0A8