private void BeingAttacked(UnitHitEvent unitHit) { if (this.Name == unitHit.target.Name) { GD.Print("AttackerName = " + unitHit.attacker.Name); target = (Node2D)unitHit.attacker; } }
private void BodyEntered(Node node) { UnitHitEvent uhei = new UnitHitEvent(); uhei.attacker = (Node2D)this; uhei.target = (Node2D)node; uhei.damage = 200; uhei.Description = uhei.attacker.Name + " attacked " + uhei.target.Name; uhei.FireEvent(); }
public void TargetHit(Node node) { if (node.IsInGroup("Enemies")) { UnitHitEvent uhei = new UnitHitEvent(); uhei.target = (Node2D)node; uhei.attacker = (Node2D)GetParent(); uhei.damage = 70; uhei.Description = "Missile hit enemy"; } Die(); }
private void Attack() { //Check if the target is still in the desired distane if (Position.DistanceTo(target.Position) > 300) { //Change the state to the attack state myState = AIStates.MoveTo; } else { //Get the direction to move in Vector2 dir = target.Position - Position; //Normalizing direction for movement dir = dir.Normalized(); //gunSprite.Rotation = Mathf.LerpAngle(gunSprite.Rotation, dir.Angle(), 0.2f); gunSprite.LookAt(target.Position); } //If the attack timer has not run out yet we just return out of the method if (!canAttack) { return; } //Reset the can attack bool canAttack = false; //Start the attack timer attackTimer.Start(); //so I am brute forcing the heck out of this mthod sorry ShowFlash(); Physics2DDirectSpaceState worldState = GetWorld2d().DirectSpaceState; //Get the raycast hits and store them in a dictionary Godot.Collections.Dictionary hits = worldState.IntersectRay(GlobalPosition, target.GlobalPosition, new Godot.Collections.Array { this }, this.CollisionMask); //Check if there was a hit if (hits.Count > 0) { if (hits.Contains("collider")) { UnitHitEvent uhei = new UnitHitEvent(); uhei.attacker = (Node2D)GetParent(); uhei.target = (Node2D)hits["collider"]; uhei.damage = 5; uhei.Description = uhei.attacker.Name + " attacked " + uhei.target.Name; uhei.FireEvent(); } } }
private void Fire() { /* * if (missileUpgrade) * { * missile = missileScene.Instance(); * * if (((Missile)missile).HasMethod("Start")) * { * //((Missile)missile).Start(this.Transform, null); * //((Missile)missile).Start(GetNode<Node2D>("../../../../../Main").Transform, null); * } * Node2D tempNode = GetNode<Node2D>("../../../../../Main/MissileContainer"); * tempNode.AddChild(missile); * //AddChild(missile); * * } * else * {*/ //Get a snapshot of the physics state of he world at this moment Physics2DDirectSpaceState worldState = GetWorld2d().DirectSpaceState; //Get the raycast hits and store them in a dictionary Godot.Collections.Dictionary hits = worldState.IntersectRay(GlobalPosition, GetGlobalMousePosition(), new Godot.Collections.Array { tankBody }, tankBody.CollisionMask); //Check if there was a hit if (hits.Count > 0) { //Change the line2d end position if there was a hit //hitPos = (Vector2)hits["position"]; if (hits.Contains("collider")) { if (((Node)hits["collider"]).IsInGroup("Enemies")) { UnitHitEvent uhei = new UnitHitEvent(); uhei.attacker = (Node2D)Owner; uhei.target = (Node2D)hits["collider"]; uhei.damage = 50; uhei.Description = uhei.attacker.Name + " attacked " + uhei.target.Name; uhei.FireEvent(); } } } //Set the start and end of the line2D and then make it visible //traceLine.Points = new Vector2[] {Vector2.Zero, hitPos }; //traceLine.Visible = true; //traceTimer.Start(); //} }
// Called when the node enters the scene tree for the first time. public override void _Ready() { attackTimer = new Timer(); //Set up the timer for the muzzle flash attackTimer.WaitTime = 2f; attackTimer.OneShot = true; attackTimer.Name = "attacktTimer"; //Create the new timer AddChild(attackTimer, true); GetNode <Timer>(attackTimer.Name).Connect("timeout", this, nameof(attackReset)); flashTimer = new Timer(); //Set up the timer for the muzzle flash flashTimer.WaitTime = .05f; flashTimer.OneShot = true; flashTimer.Name = "flashTimer"; AddChild(flashTimer, true); //Grab the timer node for the muzzle flash GetNode <Timer>(flashTimer.Name).Connect("timeout", this, nameof(HideFlash)); //Get this nodes health scripgt and init the conevtion //GetNode<Health>("Health").Connect("BeingAttacked", this, nameof(BeingAttacked)); UnitHitEvent.RegisterListener(BeingAttacked); gunSprite = (Sprite)FindNode("Gun"); muzzleFlash = (Sprite)FindNode("MuzzleFlash"); myState = AIStates.MoveTo; //Set the defualt target to the crystal target = GetNode <Node2D>("../../Crystal"); //Set the target to the dome by defualt so it goes and attacks it by defualt }
public override void _ExitTree() { UnitHitEvent.UnregisterListener(BeingAttacked); }