-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBall.cs
84 lines (71 loc) · 2.3 KB
/
Ball.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
using Godot;
using System;
public class Ball : KinematicBody2D
{
private Vector2 _velocity = new Vector2(125, 200);
[Export]
float Scalar = 1;
[Export]
float MAX_SCALAR = 1.35f;
Vector2 _screenSize;
Vector2 _startPos = new Vector2(360, 540);
[Signal]
public delegate void OnBrickBroken();
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
_screenSize = GetViewport().Size;
Hide();
Start();
}
public override void _PhysicsProcess(float delta)
{
var collisionInfo = MoveAndCollide(_velocity * delta);
if (collisionInfo != null)
{
string colliderType = collisionInfo.Collider.GetType().ToString();
if (colliderType == "Player")
{
float playerVelocity = (int)collisionInfo.Collider.Get("prevVelocity");
GD.Print("Player Velocity: " + playerVelocity);
if (playerVelocity != 0)
_velocity.x = _velocity.x + (playerVelocity * 0.25f);
else if (Math.Abs(_velocity.x) > 125 || Math.Abs(_velocity.y) > 200)
{
_velocity.x *= 0.85f;
_velocity.y *= 0.85f;
Scalar -= (Scalar >= 1.015f) ? 0.015f : 0f;
}
GetNode<AudioStreamPlayer>("Pop").Play();
if (Scalar < MAX_SCALAR)
{
Scalar += 0.02f;
_velocity *= Scalar;
}
}
_velocity = _velocity.Bounce((collisionInfo.Normal));
if (colliderType == "Brick")
{
GetNode<AudioStreamPlayer>("Break").Play();
collisionInfo.Collider.Call("Hit");
EmitSignal("OnBrickBroken");
}
else if (colliderType == "Player")
{
}
}
}
public void Start()
{
Position = _startPos;
_velocity = new Vector2(125, 200);
Scalar = 1;
Show();
GetNode<CollisionShape2D>("CollisionShape2D").SetDeferred("disabled", false);
}
public void ClearBall()
{
Hide();
GetNode<CollisionShape2D>("CollisionShape2D").SetDeferred("disabled", true);
}
}