virtual public void Spawn(Player p, string nodeName) { PackedScene PackedScene = (PackedScene)ResourceLoader.Load(_weaponResource); _weaponMesh = (MeshInstance)PackedScene.Instance(); p.Head.AddChild(_weaponMesh); _playerOwner = p; _weaponMesh.Translation = _weaponPosition; _weaponMesh.Name = nodeName; _weaponMesh.Visible = false; _shootSound = (AudioStreamPlayer3D)_weaponMesh.GetNode("ShootSound"); if (_weaponMesh.HasNode("MuzzleFlash")) { _muzzleFlash = (Sprite3D)_weaponMesh.GetNode("MuzzleFlash"); } if (_weaponMesh.HasNode("ReloadSound")) { _reloadSound = (AudioStreamPlayer3D)_weaponMesh.GetNode("ReloadSound"); } // projectile mesh if (_weaponShotType == WEAPONSHOTTYPE.PROJECTILE || _weaponShotType == WEAPONSHOTTYPE.GRENADE) { _projectileScene = (PackedScene)ResourceLoader.Load(_projectileResource); } }
public void Spawn(Node camera, string Name) { PackedScene PackedScene = (PackedScene)ResourceLoader.Load(_weaponResource); _weaponMesh = (MeshInstance)PackedScene.Instance(); camera.AddChild(_weaponMesh); _weaponMesh.Translation = this.SpawnTranslation; _weaponMesh.SetName(Name); _weaponMesh.SetVisible(false); shootSound = (AudioStreamPlayer3D)_weaponMesh.GetNode("ShootSound"); if (_weaponMesh.HasNode("MuzzleFlash")) { muzzleFlash = (Sprite3D)_weaponMesh.GetNode("MuzzleFlash"); } if (_weaponMesh.HasNode("ReloadSound")) { reloadSound = (AudioStreamPlayer3D)_weaponMesh.GetNode("ReloadSound"); } // projectile mesh if (_weaponType == WeaponType.Projectile || _weaponType == WeaponType.Grenade) { _projectileScene = (PackedScene)ResourceLoader.Load(_projectileResource); } GD.Print("Loaded " + Name); }
public override void _Input(InputEvent @event) { if (@event is InputEventMouseMotion eventMouseMotion) { var camera = (Camera)GetNode("../CameraGimbal/InnerGimbal/Camera"); var from = camera.ProjectRayOrigin(eventMouseMotion.Position); var to = from + camera.ProjectRayNormal(eventMouseMotion.Position) * rayLength; var spaceState = GetWorld().DirectSpaceState; var res = spaceState.IntersectRay(from, to, null, 0b10000000000000000000, true, true); if (res.Contains("position")) { Vector3 mouse_pos = (Vector3)res["position"] / CellSize; Vector3 tranlated_mouse_pos = new Vector3((int)mouse_pos.x, (int)mouse_pos.y, (int)mouse_pos.z); if (tranlated_mouse_pos != lastMouseSnap) { lastMouseSnap = tranlated_mouse_pos; outline.Translation = MapToWorld((int)lastMouseSnap.x, (int)lastMouseSnap.y, (int)lastMouseSnap.z); if (base.ValidBuildingLocation(lastMouseSnap)) { outline_mesh.SetSurfaceMaterial(0, green); ((MeshInstance)outline_mesh.GetNode("Colour")).Mesh.SurfaceSetMaterial(0, green); } else { outline_mesh.SetSurfaceMaterial(0, red); ((MeshInstance)outline_mesh.GetNode("Colour")).Mesh.SurfaceSetMaterial(0, red); } } } } }
virtual public bool Shoot(Camera camera, Vector2 aimAt, Player shooter) { bool shot = false; // if enough ammunition in clip if (ClipLeft >= _minAmmoRequired) { // if weapon has hit cooldown if (!shooter.Tranquilised && TimeSinceLastShot >= _coolDown || shooter.Tranquilised && TimeSinceLastShot >= _coolDown * 2) { this.TimeSinceLastShot = 0f; ClipLeft -= _minAmmoRequired; GD.Print("ClipSize: " + _clipSize); GD.Print("ClipLeft: " + ClipLeft); // fire either hitscan or projectile if (muzzleFlash != null) { muzzleFlash.Show(); } shootSound.Play(); Vector3 shootOrigin = camera.ProjectRayOrigin(new Vector2(aimAt.x, aimAt.y)); Vector3 shootTo = camera.ProjectRayNormal(new Vector2(aimAt.x, aimAt.y)) * _shootRange; Vector3 newTo = shootTo + shootOrigin; GD.Print("orig: " + shootOrigin); GD.Print("dest: " + shootTo); GD.Print("newTo: " + newTo); switch (_weaponType) { case WeaponType.Hitscan: case WeaponType.Melee: case WeaponType.Spread: List <Tuple <Vector3, PuffType, Node> > puffList = new List <Tuple <Vector3, PuffType, Node> >(); Dictionary <KinematicBody, float> hitList = new Dictionary <KinematicBody, float>(); PhysicsDirectSpaceState spaceState = shooter.GetWorld().DirectSpaceState; if (_weaponType == WeaponType.Spread) { float pc = _pelletCount; Random ran = new Random(); float random = 0f; while (pc > 0) { random = (float)ran.Next(0, 100); newTo = new Vector3((shootOrigin.x + shootTo.x) + random * _spread.x, (shootOrigin.y + shootTo.y) + random * _spread.y, shootOrigin.z + shootTo.z); GD.Print("random: " + random); GD.Print("dir: " + newTo); Tuple <Vector3, PuffType, Node, KinematicBody, float> data = this.DoHit(spaceState, shootOrigin, newTo, shooter); if (data != null) { if (data.Item1 != null) { puffList.Add(new Tuple <Vector3, PuffType, Node>(data.Item1, data.Item2, data.Item3)); } if (data.Item4 != null) { hitList.Add(data.Item4, data.Item5); } } pc -= 1; } } else { Tuple <Vector3, PuffType, Node, KinematicBody, float> data = this.DoHit(spaceState, shootOrigin, newTo, shooter); if (data != null) { if (data.Item1 != null) { puffList.Add(new Tuple <Vector3, PuffType, Node>(data.Item1, data.Item2, data.Item3)); } if (data.Item4 != null) { hitList.Add(data.Item4, data.Item5); } } } // apply damage foreach (KinematicBody kb in hitList.Keys) { Player hit = (Player)kb; if (hit.TeamID == shooter.TeamID) { if (this.GetType().ToString().ToLower() == "syringe" || this.GetType().ToString().ToLower() == "spanner") { hit.Heal(shooter, this); } } else { hit.TakeDamage(shooter.Transform, this.GetType().ToString().ToLower(), this.InflictLength, shooter, hitList[kb]); } } // do puff particles and blood particles foreach (Tuple <Vector3, PuffType, Node> puff in puffList) { Particles puffPart = null; switch (puff.Item2) { case PuffType.Blood: puffPart = (Particles)bloodScene.Instance(); break; case PuffType.Puff: puffPart = (Particles)puffScene.Instance(); break; } puffPart.SetTranslation(puff.Item1); if (puff.Item3 != null) { puff.Item3.AddChild(puffPart); } else { _weaponMesh.GetNode("/root/OpenFortress/Main").AddChild(puffPart); } puffPart.Emitting = true; } break; case WeaponType.Projectile: case WeaponType.Grenade: // spawn projectile, set it moving _projectileMesh = (Projectile)_projectileScene.Instance(); // add to scene _weaponMesh.GetNode("/root/OpenFortress/Main").AddChild(_projectileMesh); Transform t = camera.GetGlobalTransform(); _projectileMesh.Init(t, newTo, shooter, this, this.GetType().ToString().ToLower(), 0, _projectileSpeed, _damage); if (_projectileMesh is Pipebomb p) { // track against player shooter.AddActivePipebomb(p); } break; } shot = true; } else { shot = false; } } else { // force a reload this.Reload(false); shot = false; } return(shot); }