Cube drag and drop

This commit is contained in:
wjsjwr 2024-12-06 17:14:41 +08:00
parent e623429432
commit 693392ade9
17 changed files with 335 additions and 107 deletions

View File

@ -11,7 +11,7 @@ config_version=5
[application] [application]
config/name="导师模拟器" config/name="导师模拟器"
run/main_scene="res://scenes/game_manager.tscn" run/main_scene="res://scenes/logo_scene.tscn"
config/features=PackedStringArray("4.3", "C#", "GL Compatibility") config/features=PackedStringArray("4.3", "C#", "GL Compatibility")
config/icon="res://icon.svg" config/icon="res://icon.svg"
@ -20,7 +20,6 @@ config/icon="res://icon.svg"
GameManager="*res://scenes/game_manager.tscn" GameManager="*res://scenes/game_manager.tscn"
OneSecondTicker="*res://scenes/game_manager.tscn" OneSecondTicker="*res://scenes/game_manager.tscn"
Dialogic="*res://addons/dialogic/Core/DialogicGameHandler.gd" Dialogic="*res://addons/dialogic/Core/DialogicGameHandler.gd"
SceneTransit="*res://scenes/scene_transit.tscn"
[dialogic] [dialogic]
@ -57,6 +56,11 @@ dialogic_default_action={
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":0,"pressure":0.0,"pressed":false,"script":null) , Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":0,"pressure":0.0,"pressed":false,"script":null)
] ]
} }
mouse_left_press={
"deadzone": 0.5,
"events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":0,"position":Vector2(0, 0),"global_position":Vector2(0, 0),"factor":1.0,"button_index":1,"canceled":false,"pressed":false,"double_click":false,"script":null)
]
}
[rendering] [rendering]

View File

@ -3,9 +3,15 @@
[ext_resource type="Script" path="res://scripts/Cube.cs" id="1_3u72b"] [ext_resource type="Script" path="res://scripts/Cube.cs" id="1_3u72b"]
[ext_resource type="TileSet" uid="uid://doadcdgjrsxxu" path="res://resources/TileSet/Office.tres" id="2_ya2a5"] [ext_resource type="TileSet" uid="uid://doadcdgjrsxxu" path="res://resources/TileSet/Office.tres" id="2_ya2a5"]
[node name="Cube" type="Node2D"] [node name="Cube" type="StaticBody2D"]
collision_mask = 3
input_pickable = true
script = ExtResource("1_3u72b") script = ExtResource("1_3u72b")
[node name="RedBG" type="Polygon2D" parent="."]
color = Color(1, 0.0470588, 0.0666667, 0.466667)
polygon = PackedVector2Array(-48, -96, -48, 144, 144, 144, 144, -96)
[node name="Chair2" type="TileMapLayer" parent="."] [node name="Chair2" type="TileMapLayer" parent="."]
tile_map_data = PackedByteArray("AAAAAAAAAAANABwAAAAAAAEAAAANAB0AAAABAAAAAAAOABwAAAACAAAAAAAPABwAAAACAAEAAAAPAB0AAAABAP7/AAADAAoAAAABAP//AAADAAsAAAA=") tile_map_data = PackedByteArray("AAAAAAAAAAANABwAAAAAAAEAAAANAB0AAAABAAAAAAAOABwAAAACAAAAAAAPABwAAAACAAEAAAAPAB0AAAABAP7/AAADAAoAAAABAP//AAADAAsAAAA=")
tile_set = ExtResource("2_ya2a5") tile_set = ExtResource("2_ya2a5")
@ -33,3 +39,6 @@ tile_set = ExtResource("2_ya2a5")
[node name="Chair" type="TileMapLayer" parent="."] [node name="Chair" type="TileMapLayer" parent="."]
tile_map_data = PackedByteArray("AAABAAEAAAABAAgAAAABAAIAAAABAAkAAAA=") tile_map_data = PackedByteArray("AAABAAEAAAABAAgAAAABAAIAAAABAAkAAAA=")
tile_set = ExtResource("2_ya2a5") tile_set = ExtResource("2_ya2a5")
[node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="."]
polygon = PackedVector2Array(48, 138, 97, 138, 97, 97, 144, 97, 144, -33, 97, -33, 97, -76, 48, -76, 48, -33, -10, -33, -10, 97, 48, 97)

File diff suppressed because one or more lines are too long

67
scenes/logo_scene.tscn Normal file
View File

@ -0,0 +1,67 @@
[gd_scene load_steps=6 format=3 uid="uid://dv0sy1voqlq14"]
[ext_resource type="Texture2D" uid="uid://dxqtqlu4rkew7" path="res://resources/logo/bbj.png" id="1_al3ug"]
[ext_resource type="Script" path="res://scripts/LogoScene.cs" id="1_dx6qu"]
[sub_resource type="Animation" id="Animation_6umjc"]
resource_name = "logo_show"
length = 3.0
step = 1.0
tracks/0/type = "value"
tracks/0/imported = false
tracks/0/enabled = true
tracks/0/path = NodePath("BbjLogo/Sprite2D:modulate")
tracks/0/interp = 1
tracks/0/loop_wrap = true
tracks/0/keys = {
"times": PackedFloat32Array(0, 1, 2, 3),
"transitions": PackedFloat32Array(1, 1, 1, 1),
"update": 0,
"values": [Color(1, 1, 1, 0), Color(1, 1, 1, 1), Color(1, 1, 1, 1), Color(1, 1, 1, 0)]
}
[sub_resource type="Animation" id="Animation_2c4eo"]
length = 0.001
tracks/0/type = "value"
tracks/0/imported = false
tracks/0/enabled = true
tracks/0/path = NodePath("BbjLogo/Sprite2D:modulate")
tracks/0/interp = 1
tracks/0/loop_wrap = true
tracks/0/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 0,
"values": [Color(1, 1, 1, 0)]
}
[sub_resource type="AnimationLibrary" id="AnimationLibrary_sun07"]
_data = {
"RESET": SubResource("Animation_2c4eo"),
"logo_show": SubResource("Animation_6umjc")
}
[node name="LogoScene" type="Node2D"]
script = ExtResource("1_dx6qu")
[node name="BbjLogo" type="CanvasLayer" parent="."]
[node name="ColorRect" type="ColorRect" parent="BbjLogo"]
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
[node name="Sprite2D" type="Sprite2D" parent="BbjLogo"]
modulate = Color(1, 1, 1, 0)
texture_filter = 6
position = Vector2(960, 540)
scale = Vector2(1.5, 1.5)
texture = ExtResource("1_al3ug")
[node name="AnimationPlayer" type="AnimationPlayer" parent="."]
libraries = {
"": SubResource("AnimationLibrary_sun07")
}
autoplay = "logo_show"

View File

@ -1,7 +1,6 @@
[gd_scene load_steps=7 format=3 uid="uid://xnssep8q3gm5"] [gd_scene load_steps=5 format=3 uid="uid://xnssep8q3gm5"]
[ext_resource type="Script" path="res://scripts/SceneTransit.cs" id="1_2kqcs"] [ext_resource type="Script" path="res://scripts/SceneTransit.cs" id="1_2kqcs"]
[ext_resource type="Texture2D" uid="uid://dxqtqlu4rkew7" path="res://resources/logo/bbj.png" id="2_3sfim"]
[sub_resource type="Animation" id="Animation_6aqtw"] [sub_resource type="Animation" id="Animation_6aqtw"]
length = 0.001 length = 0.001
@ -17,58 +16,6 @@ tracks/0/keys = {
"update": 0, "update": 0,
"values": [Color(0, 0, 0, 0)] "values": [Color(0, 0, 0, 0)]
} }
tracks/1/type = "value"
tracks/1/imported = false
tracks/1/enabled = true
tracks/1/path = NodePath("BbjLogo/ColorRect:color")
tracks/1/interp = 1
tracks/1/loop_wrap = true
tracks/1/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 0,
"values": [Color(1, 1, 1, 1)]
}
tracks/2/type = "value"
tracks/2/imported = false
tracks/2/enabled = true
tracks/2/path = NodePath("BbjLogo/Sprite2D:modulate")
tracks/2/interp = 1
tracks/2/loop_wrap = true
tracks/2/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 0,
"values": [Color(1, 1, 1, 0)]
}
[sub_resource type="Animation" id="Animation_ro2x5"]
resource_name = "logo"
step = 0.3
tracks/0/type = "value"
tracks/0/imported = false
tracks/0/enabled = true
tracks/0/path = NodePath("BbjLogo/ColorRect:color")
tracks/0/interp = 1
tracks/0/loop_wrap = true
tracks/0/keys = {
"times": PackedFloat32Array(0, 1),
"transitions": PackedFloat32Array(1, 1),
"update": 0,
"values": [Color(1, 1, 1, 0), Color(1, 1, 1, 1)]
}
tracks/1/type = "value"
tracks/1/imported = false
tracks/1/enabled = true
tracks/1/path = NodePath("BbjLogo/Sprite2D:modulate")
tracks/1/interp = 1
tracks/1/loop_wrap = true
tracks/1/keys = {
"times": PackedFloat32Array(0, 1),
"transitions": PackedFloat32Array(1, 1),
"update": 0,
"values": [Color(1, 1, 1, 0), Color(1, 1, 1, 1)]
}
[sub_resource type="Animation" id="Animation_n35b4"] [sub_resource type="Animation" id="Animation_n35b4"]
resource_name = "transit" resource_name = "transit"
@ -90,7 +37,6 @@ tracks/0/keys = {
[sub_resource type="AnimationLibrary" id="AnimationLibrary_g0ecv"] [sub_resource type="AnimationLibrary" id="AnimationLibrary_g0ecv"]
_data = { _data = {
"RESET": SubResource("Animation_6aqtw"), "RESET": SubResource("Animation_6aqtw"),
"logo": SubResource("Animation_ro2x5"),
"transit": SubResource("Animation_n35b4") "transit": SubResource("Animation_n35b4")
} }
@ -106,19 +52,3 @@ color = Color(0, 0, 0, 0)
libraries = { libraries = {
"": SubResource("AnimationLibrary_g0ecv") "": SubResource("AnimationLibrary_g0ecv")
} }
[node name="BbjLogo" type="CanvasLayer" parent="."]
[node name="ColorRect" type="ColorRect" parent="BbjLogo"]
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
[node name="Sprite2D" type="Sprite2D" parent="BbjLogo"]
modulate = Color(1, 1, 1, 0)
texture_filter = 6
position = Vector2(960, 540)
scale = Vector2(1.5, 1.5)
texture = ExtResource("2_3sfim")

View File

@ -1,4 +1,4 @@
[gd_scene load_steps=14 format=3 uid="uid://c413oatj0eqhu"] [gd_scene load_steps=13 format=3 uid="uid://c413oatj0eqhu"]
[ext_resource type="Script" path="res://scripts/Student.cs" id="1_oesea"] [ext_resource type="Script" path="res://scripts/Student.cs" id="1_oesea"]
[ext_resource type="Texture2D" uid="uid://bw2007qd8eedh" path="res://resources/characters/bodies/Body_48x48_01.png" id="3_jhea4"] [ext_resource type="Texture2D" uid="uid://bw2007qd8eedh" path="res://resources/characters/bodies/Body_48x48_01.png" id="3_jhea4"]
@ -12,8 +12,6 @@
[sub_resource type="RectangleShape2D" id="RectangleShape2D_opr6h"] [sub_resource type="RectangleShape2D" id="RectangleShape2D_opr6h"]
size = Vector2(48, 96) size = Vector2(48, 96)
[sub_resource type="BlackboardPlan" id="BlackboardPlan_np5gt"]
[sub_resource type="Animation" id="Animation_f0f87"] [sub_resource type="Animation" id="Animation_f0f87"]
length = 0.001 length = 0.001
@ -108,9 +106,6 @@ script = ExtResource("1_oesea")
position = Vector2(0, -48) position = Vector2(0, -48)
shape = SubResource("RectangleShape2D_opr6h") shape = SubResource("RectangleShape2D_opr6h")
[node name="BTPlayer" type="BTPlayer" parent="."]
blackboard_plan = SubResource("BlackboardPlan_np5gt")
[node name="parts" type="Node2D" parent="."] [node name="parts" type="Node2D" parent="."]
position = Vector2(0, -24) position = Vector2(0, -24)

View File

@ -2,7 +2,7 @@ using Godot;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
public partial class Cube : Node2D public partial class Cube : StaticBody2D, ITileDraggable
{ {
private static readonly Rect2I tableRect = new(0, 0, 3, 2); private static readonly Rect2I tableRect = new(0, 0, 3, 2);
private static readonly Rect2I table2Rect = new(0, -1, 3, 1); private static readonly Rect2I table2Rect = new(0, -1, 3, 1);
@ -74,15 +74,67 @@ public partial class Cube : Node2D
new(new Vector2I(13, 33), equip2Rect), new(new Vector2I(13, 33), equip2Rect),
}; };
private bool _draggable = false;
public bool Draggable {
get => _draggable;
set {
if (value == _draggable) return;
_draggable = value;
}
}
public Vector2I TilePosition { get; set; } = new Vector2I(5,5);
private bool _isCollided = true;
public bool IsCollided {
get => _isCollided;
set {
if (_isCollided == value) return;
_isCollided = value;
if (value) {
GetNode<Polygon2D>("RedBG").Show();
} else {
GetNode<Polygon2D>("RedBG").Hide();
}
}
}
private static readonly Rect2I _tileRect = new(-1, -2, 4, 5);
public Rect2I TileRect => _tileRect;
private Vector2I _mouseOffset;
public Vector2I MouseOffset => _mouseOffset;
// Called when the node enters the scene tree for the first time. // Called when the node enters the scene tree for the first time.
public override void _Ready() public override void _Ready()
{ {
RandomChangeTheme(); RandomChangeTheme();
IsCollided = false;
}
public override void _InputEvent(Viewport viewport, InputEvent @event, int shapeIdx)
{
if (@event.IsActionPressed("mouse_left_press")) {
if (Draggable) {
var parent = GetParent<Lab>();
parent.IsDragging = true;
parent.DraggingTarget = this;
var desk = GetNode<TileMapLayer>("Desk");
_mouseOffset = desk.LocalToMap(GetLocalMousePosition());
GD.Print($"_mouseOffset = {_mouseOffset}");
}
} else if (@event.IsActionReleased("mouse_left_press")) {
var parent = GetParent<Lab>();
parent.IsDragging = false;
parent.DraggingTarget = null;
}
base._InputEvent(viewport, @event, shapeIdx);
} }
// Called every frame. 'delta' is the elapsed time since the previous frame. // Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta) public override void _Process(double delta)
{ {
GlobalPosition = TilePosition * 48;
} }
public void RandomChangeTheme() { public void RandomChangeTheme() {
@ -95,5 +147,4 @@ public partial class Cube : Node2D
GD.Print(GD.RandRange(0, equip2Themes.Length-1)); GD.Print(GD.RandRange(0, equip2Themes.Length-1));
equip2Themes[x].Apply(GetNode<TileMapLayer>("Equip2")); equip2Themes[x].Apply(GetNode<TileMapLayer>("Equip2"));
} }
} }

View File

@ -13,8 +13,6 @@ public partial class GameManager : Node
public override void _Ready() public override void _Ready()
{ {
Input.SetCustomMouseCursor(Arrow2x); Input.SetCustomMouseCursor(Arrow2x);
var transit = GetNode<SceneTransit>("/root/SceneTransit");
transit.Transit("res://scenes/lab.tscn", true, "logo", 1.5f);
} }
// Called every frame. 'delta' is the elapsed time since the previous frame. // Called every frame. 'delta' is the elapsed time since the previous frame.

View File

@ -5,6 +5,13 @@ using System.Linq;
public partial class Lab : Node2D public partial class Lab : Node2D
{ {
private static readonly Rect2I[] wallRectangles = {
new(0,0,40,2),
new(0,5,1, 15),
new(0,20,40,1),
new(39,2,1,18),
};
// Called when the node enters the scene tree for the first time. // Called when the node enters the scene tree for the first time.
public override void _Ready() public override void _Ready()
{ {
@ -19,10 +26,10 @@ public partial class Lab : Node2D
Player.Timeline.OnDayChanged += d => label.Text = d.ToLongDateString(); Player.Timeline.OnDayChanged += d => label.Text = d.ToLongDateString();
var table = GetNode<Cube>("Cube"); var table = GetNode<Cube>("Cube");
Player.Timeline.OnDayChanged += d => table.RandomChangeTheme(); Player.Timeline.OnDayChanged += d => table.RandomChangeTheme();
table.Draggable = true;
MoneyLabel = GetNode<Label>("BottomBar/HBoxContainer/Money"); MoneyLabel = GetNode<Label>("BottomBar/HBoxContainer/Money");
tileMap = GetNode<TileMapLayer>("TestMap"); tileMap = GetNode<TileMapLayer>("OutGround");
UpdateMap(); UpdateMap();
} }
@ -42,8 +49,18 @@ public partial class Lab : Node2D
break; break;
} }
MoneyLabel.Text = Player.Budget.Total.ToString("N0"); MoneyLabel.Text = Player.Budget.Total.ToString("N0");
if (IsDragging && DraggingTarget != null) {
Vector2 mousePos = GetLocalMousePosition();
Vector2I cell = Point2Coord(mousePos) - DraggingTarget.MouseOffset;
var targetRect = DraggingTarget.TileRect;
targetRect.Position += cell;
DraggingTarget.IsCollided = wallRectangles.Any(r => r.Intersects(targetRect));
DraggingTarget.TilePosition = cell;
}
} }
public bool IsDragging { get; set; } = false;
public ITileDraggable DraggingTarget { get; set; } = null;
private static readonly Vector2I tableTileCoord = new(0, 6); private static readonly Vector2I tableTileCoord = new(0, 6);
private static readonly Vector2I waterTileCoord = new(6, 9); private static readonly Vector2I waterTileCoord = new(6, 9);
@ -93,6 +110,7 @@ public partial class Lab : Node2D
} }
private static bool IsValidPosition(Vector2I pos) private static bool IsValidPosition(Vector2I pos)
{ {
int x = pos.X; int x = pos.X;

View File

@ -19,12 +19,12 @@ public partial class Loader : Control
if (nextScene != null) { if (nextScene != null) {
GameManager.NextScene = "res://scenes/lab.tscn"; GameManager.NextScene = "res://scenes/lab.tscn";
} }
resToLoad.AddRange(Res.Accessory); // resToLoad.AddRange(Res.Accessory);
resToLoad.AddRange(Res.Body); // resToLoad.AddRange(Res.Body);
resToLoad.AddRange(Res.Eye); // resToLoad.AddRange(Res.Eye);
resToLoad.AddRange(Res.Hair); // resToLoad.AddRange(Res.Hair);
resToLoad.AddRange(Res.Outfit); // resToLoad.AddRange(Res.Outfit);
resToLoad.AddRange(Res.Smartphone); // resToLoad.AddRange(Res.Smartphone);
ResourceLoader.LoadThreadedRequest(resToLoad[sceneLoaded]); ResourceLoader.LoadThreadedRequest(resToLoad[sceneLoaded]);
progressBar.MaxValue = 100 * resToLoad.Count; progressBar.MaxValue = 100 * resToLoad.Count;
SetProcess(true); SetProcess(true);
@ -44,9 +44,8 @@ public partial class Loader : Control
if(sceneLoaded < resToLoad.Count) { if(sceneLoaded < resToLoad.Count) {
ResourceLoader.LoadThreadedRequest(resToLoad[sceneLoaded]); ResourceLoader.LoadThreadedRequest(resToLoad[sceneLoaded]);
} else { } else {
var transit = GetNode<SceneTransit>("/root/SceneTransit");
transit.Transit("res://scenes/lab.tscn", false);
SetProcess(false); SetProcess(false);
GetTree().ChangeSceneToFile("res://scenes/lab.tscn");
} }
break; break;
default: default:

15
scripts/LogoScene.cs Normal file
View File

@ -0,0 +1,15 @@
using Godot;
using System;
public partial class LogoScene : Node2D
{
public override void _Ready() {
GetNode<AnimationPlayer>("AnimationPlayer").AnimationFinished += OnAnimationPlayerAnimationFinished;
}
public void OnAnimationPlayerAnimationFinished(StringName animationName)
{
GD.Print("FFF");
GetTree().ChangeSceneToFile("res://scenes/loader.tscn");
}
}

View File

@ -17,6 +17,7 @@ public partial class SceneTransit : CanvasLayer
public async void Transit(string nextScene, bool needLoadResources = true, string animation = "transit", float waitSecond = 0) public async void Transit(string nextScene, bool needLoadResources = true, string animation = "transit", float waitSecond = 0)
{ {
SetProcess(true);
GameManager.NextScene = nextScene; GameManager.NextScene = nextScene;
var ap = GetNode<AnimationPlayer>("AnimationPlayer"); var ap = GetNode<AnimationPlayer>("AnimationPlayer");
Layer = 9999; Layer = 9999;
@ -35,6 +36,7 @@ public partial class SceneTransit : CanvasLayer
await ToSignal(ap, AnimationMixer.SignalName.AnimationFinished); await ToSignal(ap, AnimationMixer.SignalName.AnimationFinished);
Layer = -1; Layer = -1;
Hide(); Hide();
SetProcess(false);
} }
} }

View File

@ -73,8 +73,8 @@ public partial class Student : CharacterBody2D
public override void _Ready() public override void _Ready()
{ {
base._Ready(); base._Ready();
var bt = GetNode<BTPlayer>("BTPlayer"); // var bt = GetNode<BTPlayer>("BTPlayer");
var bb = bt.Blackboard; // var bb = bt.Blackboard;
// bb.BindVarToProperty("Stayed", this, "Speed"); // bb.BindVarToProperty("Stayed", this, "Speed");
// GD.Print(bb.GetVar("Stayed")); // GD.Print(bb.GetVar("Stayed"));
// GD.Print($"Speed: {Speed}"); // GD.Print($"Speed: {Speed}");

View File

@ -34,7 +34,6 @@ public partial class TestMap : TileMapLayer
} }
lastHighlightCell = cell; lastHighlightCell = cell;
SetCell(cell, 0, highlightTileCoord); SetCell(cell, 0, highlightTileCoord);
GD.Print("Mouse is over cell: ", cell);
} }
} }

10
scripts/TileDraggable.cs Normal file
View File

@ -0,0 +1,10 @@
using Godot;
public interface ITileDraggable {
Vector2I TilePosition { get; set; }
bool Draggable { get; set; }
bool IsCollided { get; set; }
Rect2I TileRect { get; }
Vector2I MouseOffset { get; }
}

View File

@ -26,6 +26,8 @@ public partial class TileMapping : Node
tileRect = rect; tileRect = rect;
} }
public TileMapping() {}
public void Apply(TileMapLayer layer) { public void Apply(TileMapLayer layer) {
// if pixelMap is not null, use all the pairs in pixelMap to set the tiles. // if pixelMap is not null, use all the pairs in pixelMap to set the tiles.
if (pixelMap != null) { if (pixelMap != null) {

14
shaders/cube.gdshader Normal file
View File

@ -0,0 +1,14 @@
shader_type canvas_item;
void vertex() {
// Called for every vertex the material is visible on.
}
void fragment() {
// Called for every pixel the material is visible on.
}
//void light() {
// Called for every pixel for every light affecting the CanvasItem.
// Uncomment to replace the default light processing function with this one.
//}