Skip to content

Commit 275c858

Browse files
authored
Merge pull request #8 from magarich228/dev
Player states, animations.
2 parents adbc1cb + 7c84900 commit 275c858

File tree

3 files changed

+335
-35
lines changed

3 files changed

+335
-35
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
## TODO:
44
* Замена ui событий на кастомные.
55
* Отрефачить код _Process.
6-
* Фикс странной механики замедленного движения при зажатых ui_up, ui_down.
7-
* Фикс состояния Stand после прекращения передвижения в присяде.
6+
* Баг прохождения игрока через коллайдеры при принятии положения лежа.
87

98
![image](https://github.com/magarich228/TDP5/assets/78496868/e8e352f7-186e-45a4-aea9-f5b1e19b06db)

player/stickplayer.cs

Lines changed: 76 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,22 @@
77

88
public partial class stickplayer : CharacterBody2D
99
{
10-
public const float Speed = 300.0f;
10+
// Godot public properties.
11+
// ReSharper disable MemberCanBePrivate.Global
1112
public const float JumpVelocity = -500.0f;
13+
public const float Speed = 300.0f;
1214

1315
public PlayerState State { get; set; }
1416
public float Gravity = ProjectSettings.GetSetting("physics/2d/default_gravity").AsSingle();
17+
// ReSharper restore MemberCanBePrivate.Global
1518

1619
private AnimationPlayer _animationPlayer;
1720
private Dictionary<PlayerState, string> _stateAnimations;
18-
private Stopwatch _watch = new Stopwatch();
21+
private Stopwatch _watch;
1922

2023
public override void _Ready()
2124
{
25+
_watch = new Stopwatch();
2226
_animationPlayer = GetNode("AnimationPlayer") as AnimationPlayer ??
2327
throw new ApplicationException($"Не получен узел {nameof(AnimationPlayer)}");
2428

@@ -28,15 +32,7 @@ public override void _Ready()
2832

2933
foreach (var state in Enum.GetValues<PlayerState>())
3034
{
31-
var stateName = Enum.GetName(state) ??
32-
throw new ApplicationException("Не получено имя анимации состояния.");
33-
34-
var animationName = _animationPlayer.GetAnimation(Regex.Replace(
35-
stateName,
36-
"[A-Z]",
37-
"_$0")
38-
.Substring(1)
39-
.ToLower()).ResourceName;
35+
var animationName = GetAnimationName(state);
4036

4137
if (!string.IsNullOrWhiteSpace(animationName))
4238
{
@@ -54,52 +50,84 @@ public override void _PhysicsProcess(double delta)
5450

5551
if (IsOnFloor() && velocity.X.Equals(0) && !Input.IsActionPressed("ui_down"))
5652
{
57-
Console.WriteLine("stand");
58-
State = PlayerState.Stand;
59-
} else { Console.WriteLine($"not stand ({State})"); }
53+
State = State != PlayerState.Lie && State != PlayerState.Creep? PlayerState.Stand : PlayerState.Lie;
54+
}
6055

6156
if (!IsOnFloor())
6257
velocity.Y += Gravity * (float)delta;
6358

64-
if (Input.IsActionJustPressed("ui_up") && IsOnFloor())
59+
if (Input.IsActionJustPressed("ui_up"))
6560
{
66-
velocity.Y = JumpVelocity;
61+
if (State == PlayerState.Lie || State == PlayerState.Creep)
62+
{
63+
State = PlayerState.Stand;
64+
}
65+
else if (IsOnFloor())
66+
{
67+
velocity.Y = JumpVelocity;
68+
}
6769
}
6870

69-
// Get the input direction and handle the movement/deceleration.
70-
// As good practice, you should replace UI actions with custom gameplay actions.
71-
Vector2 direction = Input.GetVector("ui_left", "ui_right", "ui_up", "ui_down");
71+
var speed = State switch
72+
{
73+
PlayerState.Run or PlayerState.Stand or PlayerState.Jump => Speed,
74+
PlayerState.SitWalk or PlayerState.Sit => Speed / 1.5f,
75+
PlayerState.Creep or PlayerState.Lie => Speed / 2f,
76+
_ => 0
77+
};
7278

73-
if (direction != Vector2.Zero)
79+
if (Input.IsActionPressed("ui_right"))
7480
{
75-
velocity.X = direction.X * Speed;
81+
velocity.X = speed;
7682
}
83+
else if (Input.IsActionPressed("ui_left"))
84+
{
85+
velocity.X = -speed;
86+
}
7787
else
7888
{
79-
velocity.X = Mathf.MoveToward(Velocity.X, 0, Speed);
89+
velocity.X = 0;
8090
}
8191

82-
if (velocity.X != 0 && velocity.Y == 0)
92+
if (velocity.X != 0 && velocity.Y >= 0)
8393
{
84-
State = PlayerState.Run;
94+
State = State == PlayerState.Lie || State == PlayerState.Creep ? PlayerState.Creep : PlayerState.Run;
8595
}
8696

87-
if (velocity.Y != 0 && !Input.IsActionPressed("ui_down"))
97+
if (State != PlayerState.Lie && State != PlayerState.Creep && !IsOnFloor() && !Input.IsActionPressed("ui_down"))
8898
{
8999
State = PlayerState.Jump;
90100
}
101+
102+
if (Input.IsActionJustPressed("ui_down"))
103+
{
104+
if (_watch.IsRunning)
105+
{
106+
if (_watch.ElapsedMilliseconds > 500)
107+
{
108+
_watch.Reset();
109+
_watch.Stop();
110+
}
111+
else
112+
{
113+
State = PlayerState.Lie;
114+
}
115+
}
116+
117+
_watch.Start();
118+
}
91119

92-
if (Input.IsActionPressed("ui_down"))
120+
if (State != PlayerState.Lie && Input.IsActionPressed("ui_down"))
93121
{
94122
State = PlayerState.Sit;
95123
}
96124

97-
if (velocity.X != 0 && Input.IsActionPressed("ui_down") &&
125+
if (State != PlayerState.Lie && velocity.X != 0 && Input.IsActionPressed("ui_down") &&
98126
(Input.IsActionPressed("ui_left") || Input.IsActionPressed("ui_right")))
99127
{
100128
State = PlayerState.SitWalk;
101129
}
102-
130+
103131
var localMousePosition = GetLocalMousePosition();
104132
var globalMousePosition = GetGlobalMousePosition();
105133

@@ -124,10 +152,16 @@ public override void _PhysicsProcess(double delta)
124152
MoveAndSlide();
125153

126154
if (State == PlayerState.Sit && oldState == PlayerState.SitWalk)
127-
_animationPlayer.Play("sit", fromEnd: true);
155+
_animationPlayer.Play(GetAnimationName(State), fromEnd: true);
156+
157+
if (State == PlayerState.Lie && oldState == PlayerState.Creep)
158+
_animationPlayer.Play(GetAnimationName(State), fromEnd: true);
128159

129160
if (State != oldState)
161+
{
130162
Animate();
163+
Console.WriteLine($"State: {State}");
164+
}
131165
}
132166

133167
private void Animate()
@@ -137,4 +171,17 @@ private void Animate()
137171
_animationPlayer.Play(_stateAnimations[State]);
138172
}
139173
}
174+
175+
private string GetAnimationName(PlayerState state)
176+
{
177+
var stateName = Enum.GetName(state) ??
178+
throw new ApplicationException("Не получено имя анимации состояния.");
179+
180+
return _animationPlayer.GetAnimation(Regex.Replace(
181+
stateName,
182+
"[A-Z]",
183+
"_$0")
184+
.Substring(1)
185+
.ToLower()).ResourceName;
186+
}
140187
}

0 commit comments

Comments
 (0)