-
Notifications
You must be signed in to change notification settings - Fork 0
/
Barrel.cs
78 lines (71 loc) · 2.51 KB
/
Barrel.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
using Godot;
using System;
public class Barrel : KinematicBody
{
// Declare member variables here. Examples:
// private int a = 2;
// private string b = "text";
float z;
bool direction = true;
float reloadTime = 1;
float timePassed = 0;
int max_ammo = 600;
int ammo = 0;
KinematicBody ktargetPos;
RigidBody targetPos;
PackedScene scene = (PackedScene)ResourceLoader.Load("res://RBullet.tscn");
RayCast ray;
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
z = Translation.z;
ray = GetNode<RayCast>("RayCast");
}
void ShootAnim(float delta){
if(ammo != 0){
if(Translation.z < -3){
direction = true;
var bullet = (RBullet)scene.Instance();
bullet.Parent = GetParent();
//GD.Print(Scale);
Vector3 tDir = Transform.origin.DirectionTo(targetPos.GlobalTransform.origin);
//GD.Print(targetPos.LinearVelocity.Normalized());
//GD.Print(targetPos.GlobalTransform.origin);
Vector3 bulletDirection = tDir*100;//GlobalTransform.origin; //+ Transform.origin;
//GD.Print(GlobalTransform.origin);
//GD.Print(Transform.origin);
//bulletDirection.y = 0;
bullet.Launch(bulletDirection);
GetNode("/root/World").AddChild(bullet);
//GetParent().AddChild(bullet);
}else if(Translation.z > -2.4f){
direction = false;
}
if(direction){
Translate(new Vector3(0,0,0.1f));
}else{
Translate(new Vector3(0,0,-0.1f));
}
ammo--;
}else if(timePassed > reloadTime){
ammo = max_ammo;
timePassed = 0;
}else{
timePassed += delta;
}
}
// // Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(float delta)
{
var upDir = new Vector3(1, 0, 0);
targetPos = GetNode<RigidBody>("/root/World/Target");
ktargetPos = GetNode<KinematicBody>("/root/World/KTarget");
//LookAt(ktargetPosition.GlobalTransform.origin,upDir);
//LookAt(targetPos.GlobalTransform.origin,upDir);
//ray.LookAt(targetPos.GlobalTransform.origin,upDir);
//GD.Print(delta);
if(ray.IsColliding()){
ShootAnim(delta);
}
}
}