-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.cs
164 lines (131 loc) · 5.19 KB
/
player.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using Godot;
using System;
public class player : Node2D {
[Export] public float PhysicalStart = 0.0f;
[Export] public float GhostStart = 0.0f;
[Export] public float MaxSpeed = 0.0f;
[Signal] public delegate void SwapRealms();
private vertical physical;
private vertical ghost;
private Camera2D camera;
private bool swapped = false;
private bool swapButton = true;
private void SetFlip(bool flip) {
physical.sprite.FlipH = flip;
var climb = physical.GetNode<RayCast2D>("Climb");
climb.Rotation = flip ? (float)Math.PI : 0.0f;
var foot = physical.GetNode<RayCast2D>("CornerCheck");
foot.Rotation = flip ? (float)Math.PI : 0.0f;
ghost.sprite.FlipH = flip;
}
private void SetAnimationIfGrounded(string anim) {
if (physical.IsGrounded()) {
physical.sprite.Animation = anim;
}
if (ghost.IsGrounded()) {
ghost.sprite.Animation = anim;
}
}
// Called when the node enters the scene tree for the first time.
public override void _Ready() {
physical = GetNode<vertical>("Physical");
physical.Position = new Vector2(0.0f, -PhysicalStart);
ghost = GetNode<vertical>("Ghost");
ghost.Position = new Vector2(0.0f, GhostStart);
camera = GetNodeOrNull<Camera2D>("Camera2D");
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _PhysicsProcess(float delta) {
float originalX = physical.Position.x;
float horiz = MoveHorizontal();
if (!swapped) {
MoveVertical(physical, delta, horiz, -1.0f);
MoveVertical(ghost, delta, horiz, 1.0f);
}
else {
MoveVertical(physical, delta, horiz, 1.0f);
MoveVertical(ghost, delta, horiz, -1.0f);
}
// POSITION SET USED AFTER THIS POINT
// NO MORE PHYSICS CALLS ALLOWED
// Make sure positions are together
SyncPositions(originalX);
SwapCheck();
// Move camera to appropriate position
if (camera != null) {
camera.Position = new Vector2(physical.Position.x, 0.0f);
}
}
private float MoveHorizontal() {
bool left = Input.IsActionPressed("ui_left");
bool right = Input.IsActionPressed("ui_right");
// Either none or both are pressed
if (left == right) {
SetAnimationIfGrounded("idle");
return 0.0f;
}
// Flip sprites based on facing (flipped if left)
SetFlip(left);
SetAnimationIfGrounded("walk");
// Direction is positive if right, negative otherwise
// We know left ^ right, so checking for right is enough
float direction = right ? 1 : -1;
return direction * MaxSpeed;
}
private void MoveVertical(vertical v, float delta, float horiz, float up) {
float vert = v.MoveVertical(delta);
Vector2 upDir = new Vector2(0.0f, 1.0f) * up;
v.MoveAndSlide(new Vector2(horiz, vert * up), upDir);
}
private void SyncPositions(float originalX) {
float physicalChange = Math.Abs(physical.Position.x - originalX);
float ghostChange = Math.Abs(ghost.Position.x - originalX);
if (physicalChange > ghostChange) {
Vector2 newPosition = new Vector2(ghost.Position.x, physical.Position.y);
physical.MoveAndCollide(newPosition - physical.Position);
}
else {
Vector2 newPosition = new Vector2(physical.Position.x, ghost.Position.y);
ghost.MoveAndCollide(newPosition - ghost.Position);
}
}
private void SwapCheck() {
if (Input.IsActionPressed("swap")) {
if (!swapButton) {
GetNode<AudioStreamPlayer2D>("Physical/Audio").Play();
if (!swapped) {
Swap(physical, ghost);
}
else {
Swap(ghost, physical);
}
swapped = !swapped;
physical.PhysFlip(swapped);
swapButton = true;
}
}
else {
swapButton = false;
}
}
private void Swap(vertical top, vertical bottom) {
AnimatedSprite topSprite = top.GetNode<AnimatedSprite>("AnimatedSprite");
AnimatedSprite bottomSprite = bottom.GetNode<AnimatedSprite>("AnimatedSprite");
topSprite.FlipV = true;
bottomSprite.FlipV = false;
// Swap top and bottom sprite offsets
float topYPosition = topSprite.Position.y;
topSprite.Position = new Vector2(topSprite.Position.x, bottomSprite.Position.y);
bottomSprite.Position = new Vector2(bottomSprite.Position.x, topYPosition);
float topSpeed = top.verticalSpeed;
top.verticalSpeed = bottom.verticalSpeed;
bottom.verticalSpeed = topSpeed;
Vector2 topPosition = top.Position;
top.Position = bottom.Position;
bottom.Position = topPosition;
Vector2 topRayPosition = top.groundRay.Position;
top.groundRay.Position = bottom.groundRay.Position;
bottom.groundRay.Position = topRayPosition;
EmitSignal(nameof(SwapRealms));
}
}