public override void _Ready() { GD.Print("NOTIFY: SpawnOwnPlayer Ready started"); _globals = GetNode <Globals>("/root/Globals"); if (_globals.PlayingAsHost) { GD.Print("NOTIFY: SpawnOwnPlayer as Host"); PackedScene playerScene = ResourceLoader.Load <PackedScene>(_playerPath); KinematicBody player = playerScene?.Instance() as KinematicBody; if (player == null) { return; } CallDeferred("add_child", player); _globals.OwnPlayerBody = player; player.Name = _globals.HostId.ToString(); Transform playerTransform = player.Transform; playerTransform.origin = DefaultSpawnPos; player.Transform = playerTransform; } else { GD.Print("NOTIFY: SpawnOwnPlayer as Client, sending"); var packet = Utils.GetSpawnBytes(true, _globals.OwnId.ToString(), ActionType.PlayerSpawn, DefaultSpawnPos); SteamNetworking.SendP2PPacket(_globals.HostId, packet, (uint)packet.Length, EP2PSend.k_EP2PSendReliable); } }
private void SpawnProp(string name, Vector3?pos) { PackedScene propScene = ResourceLoader.Load <PackedScene>(NetworkBoxPath); KinematicBody prop = propScene?.Instance() as KinematicBody; if (prop == null) { return; } AddChild(prop); _props.Add(prop.Name, prop); prop.Name = name; if (pos != null) { Transform propTransform = prop.Transform; propTransform.origin.x = pos.Value.x; propTransform.origin.y = pos.Value.y; propTransform.origin.z = pos.Value.z; prop.Transform = propTransform; } else { Transform propTransform = prop.Transform; propTransform.origin.x = DefaultSpawnPos.x; propTransform.origin.y = DefaultSpawnPos.y; propTransform.origin.z = DefaultSpawnPos.z; prop.Transform = propTransform; } }
public override void _Process(float delta) { if (!Input.IsActionJustReleased("spawn_player")) { return; } GD.Print("DEV HERE"); var arthur = _arthur?.Instance() as KinematicBody2D; var ySort = GetNode <YSort>("YSort"); ySort.AddChild(arthur); if (arthur != null) { arthur.GlobalPosition = new Vector2(64, 32); arthur.ZIndex = 1; } }
private void Shoot(InputEventMouse @event) { if (!(_timeSinceLastShot > _projectileCooldown)) { return; } if (_playerProjectile?.Instance() is Projectile projectile) { AddChild(projectile); projectile.Setup(_projectileCollateral, false); projectile.Damage = _projectileDamage; projectile.Lifetime = _projectileLifetime; projectile.SetVelocity(this, @event.Position, _projectileSpeed); projectile.LookAt(@event.Position); } _timeSinceLastShot = 0; }
public EquippableItem CreateItem() => (EquippableItem)_equippableItem?.Instance();
private void ProcessInput(float delta) { // ------------------------------------------------------------------- // Walking _dir = new Vector3(); Transform camXform = Camera.GlobalTransform; Vector2 inputMovementVector = new Vector2(); if (Input.IsActionPressed("movement_forward")) { inputMovementVector.y += 1; } if (Input.IsActionPressed("movement_backward")) { inputMovementVector.y -= 1; } if (Input.IsActionPressed("movement_left")) { inputMovementVector.x -= 1; } if (Input.IsActionPressed("movement_right")) { inputMovementVector.x += 1; } inputMovementVector = inputMovementVector.Normalized(); // Basis vectors are already normalized. _dir += -camXform.basis.z * inputMovementVector.y; _dir += camXform.basis.x * inputMovementVector.x; // ------------------------------------------------------------------- // ------------------------------------------------------------------- // Jumping if (IsOnFloor() && Input.IsActionJustPressed("movement_jump")) { _vel.y = JumpSpeed; } // ------------------------------------------------------------------- // ------------------------------------------------------------------- // Sprinting if (Input.IsActionPressed("movement_sprint")) { _isSprinting = true; } else { _isSprinting = false; } // ------------------------------------------------------------------- // ------------------------------------------------------------------- // Turning the flashlight on/off if (Input.IsActionJustPressed("flashlight")) { if (_flashlight.IsVisibleInTree()) { _flashlight.Hide(); } else { _flashlight.Show(); } } // ------------------------------------------------------------------- // Changing _weapons int _weaponChangeNumber = _weaponNameToNumber[_currentWeaponName]; if (Input.IsActionJustPressed("Weapon1")) { _weaponChangeNumber = 0; } if (Input.IsActionJustPressed("Weapon2")) { _weaponChangeNumber = 1; } if (Input.IsActionJustPressed("Weapon3")) { _weaponChangeNumber = 2; } if (Input.IsActionJustPressed("Weapon4")) { _weaponChangeNumber = 3; } if (Input.IsActionJustPressed("shift_weapon_positive")) { _weaponChangeNumber++; } if (Input.IsActionJustPressed("shift_weapon_negative")) { _weaponChangeNumber--; } _weaponChangeNumber = Mathf.Clamp(_weaponChangeNumber, 0, _weaponNumberToName.Count); if (_weaponNumberToName[_weaponChangeNumber] != _currentWeaponName) { if (!_reloadingWeapon && !_changingWeapon) { _changingWeaponName = _weaponNumberToName[_weaponChangeNumber]; _changingWeapon = true; _mouseScrollValue = _weaponChangeNumber; } } // ------------------------------------------------------------------- // ------------------------------------------------------------------- // Firing Weapon if (Input.IsActionPressed("fire") && !_changingWeapon && !_reloadingWeapon) { Weapon _currentWeapon = _weapons[_currentWeaponName]; if (_currentWeapon != null && _currentWeapon.AmmoInWeapon > 0) { if (AnimationPlayer.CurrentState == _currentWeapon.IdleAnimName) { AnimationPlayer.CallbackFunction = GD.FuncRef(this, nameof(FireBullet)); AnimationPlayer.SetAnimation(_currentWeapon.FireAnimName); } } else { _reloadingWeapon = true; } } // ------------------------------------------------------------------- // ------------------------------------------------------------------- // Reloading if (!_reloadingWeapon && !_changingWeapon && Input.IsActionJustPressed("reload")) { Weapon _currentWeapon = _weapons[_currentWeaponName]; if (_currentWeapon != null && _currentWeapon.CanReload) { string _currentAnimState = AnimationPlayer.CurrentState; bool _isReloading = false; foreach (string _weapon in _weapons.Keys) { Weapon _weaponNode = _weapons[_weapon]; if (_weaponNode != null && _currentAnimState == _weaponNode.ReloadingAnimName) { _isReloading = true; } } if (!_isReloading) { _reloadingWeapon = true; } } } // ------------------------------------------------------------------- // ------------------------------------------------------------------- // Changing and throwing grenades if (Input.IsActionJustPressed("change_grenade")) { if (_currentGrenade == 0) { _currentGrenade = 1; } else if (_currentGrenade == 1) { _currentGrenade = 0; } } if (Input.IsActionJustPressed("fire_grenade") && _grenadeAmmounts[_currentGrenade] > 0) { _grenadeAmmounts[_currentGrenade]--; Grenade _grenadeClone; if (_currentGrenade == 0) { _grenadeClone = (FragGrenade)_fragGrenadeScene.Instance(); } else { _grenadeClone = (StickyGrenade)_stickyGrenadeScene.Instance(); // Sticky grenades will stick to the player if we do not pass ourselves _grenadeClone.PlayerBody = this; } GetTree().Root.AddChild(_grenadeClone); _grenadeClone.GlobalTransform = GetNode <Spatial>("Rotation_Helper/FragGrenade_Toss_Pos").GlobalTransform; _grenadeClone.ApplyImpulse(new Vector3(0, 0, 0), _grenadeClone.GlobalTransform.basis.z * FragGrenadeThrowForce); } // ------------------------------------------------------------------- // ------------------------------------------------------------------- // Grabbing and throwing objects if (Input.IsActionJustPressed("fire") && _currentWeaponName == "UNARMED") { if (_grabbedObject == null) { PhysicsDirectSpaceState _state = GetWorld().DirectSpaceState; Vector2 _centerPosition = GetViewport().Size / 2; Vector3 _rayFrom = Camera.ProjectRayOrigin(_centerPosition); Vector3 _rayTo = _rayFrom + Camera.ProjectRayNormal(_centerPosition) * ObjectGrabRayDistance; Godot.Collections.Array _exclude = new Godot.Collections.Array(); _exclude.Add(this); _exclude.Add(GetNode <Area>("Rotation_Helper/Gun_Fire_Points/Knife/Area")); Godot.Collections.Dictionary _rayResult = _state.IntersectRay(_rayFrom, _rayTo, _exclude); if (_rayResult.Count != 0 && _rayResult["collider"] is RigidBody) { _grabbedObject = _rayResult["collider"]; RigidBody _grabbedRigid = (RigidBody)_grabbedObject; _grabbedRigid.Mode = RigidBody.ModeEnum.Static; _grabbedRigid.CollisionLayer = 0; _grabbedRigid.CollisionMask = 0; } } else { _grabbedRigid = (RigidBody)_grabbedObject; _grabbedRigid.Mode = RigidBody.ModeEnum.Rigid; _grabbedRigid.ApplyImpulse(new Vector3(0, 0, 0), -Camera.GlobalTransform.basis.z.Normalized() * ObjectThrowForce); _grabbedRigid.CollisionLayer = 1; _grabbedRigid.CollisionMask = 1; _grabbedRigid = null; _grabbedObject = null; } } if (_grabbedObject != null) { _grabbedRigid = (RigidBody)_grabbedObject; Transform _transform = new Transform(_grabbedRigid.GlobalTransform.basis, Camera.GlobalTransform.origin + (-Camera.GlobalTransform.basis.z.Normalized() * ObjectGrabDistance)); _grabbedRigid.GlobalTransform = _transform; } // ------------------------------------------------------------------- // ------------------------------------------------------------------- // Pause Popup if (Input.IsActionJustPressed("ui_cancel")) { if (_pausePopup == null) { _pausePopup = (PausePopup)_pausePopupScene.Instance(); _pausePopup.Player = this; _globals.CanvasLayer.AddChild(_pausePopup); _pausePopup.PopupCentered(); Input.SetMouseMode(Input.MouseMode.Visible); GetTree().Paused = true; } else { Input.SetMouseMode(Input.MouseMode.Captured); _pausePopup = null; } } // ------------------------------------------------------------------- }
public Equipment CreateEquipment() => (Equipment)_equipment?.Instance();
public (Spatial, Vector3, Vector3, int) generate_maze_1() { // 0 = vide , 1=floor , 2=fake floor , 3=wall Vector3 tc = new Vector3(1.5F, 3F, 1.5F); Vector3 dc = tc * 2; if (globale.difficulty == 0) { dc = new Vector3(5F, 3F, 5F); dc = tc * 2; } else if (globale.difficulty == 1) { tc = new Vector3(3F, 3F, 3F); dc = tc * 2; } else if (globale.difficulty == 3) { dc = new Vector3(1.1F, 3F, 1.1F); dc = tc * 2; } int mtx = globale.mtx; int mty = 2; int mtz = globale.mtz; int nb_plats = globale.nb_plats; int[, ,] mape = new int[mtx, mty, mtz]; Spatial level = new Spatial(); //creation PackedScene packedSceneFloor = (PackedScene)ResourceLoader.Load("res://assets/floor.tscn"); MeshInstance floor = (MeshInstance)packedSceneFloor.Instance(); floor.Name = "tile-" + 0 + "-" + 0 + "-" + 0; level.AddChild(floor); floor.Translation = new Vector3(0, 0, 0); floor.Scale = new Vector3(mtx * dc.x, 1, mtz * dc.z); for (int x = 0; x < mtx; x++) { for (int z = 0; z < mtz; z++) { // PackedScene packedSceneWall = (PackedScene)ResourceLoader.Load("res://assets/mur.tscn"); mur wall = (mur)packedSceneWall.Instance(); wall.Name = "tile-" + x + "-" + 1 + "-" + z; level.AddChild(wall); wall.Translation = new Vector3(x, 1, z) * dc; wall.Scale = tc; mape[x, 1, z] = 3; } } Random rand = new Random(); int apx = rand.Next(1, mtx - 2); int apz = rand.Next(1, mtz - 2); Vector3 player_spawn = new Vector3(apx, 2, apz) * dc; mur walle = (mur)level.GetNode("tile-" + apx + "-" + 1 + "-" + apz); if (walle != null) { walle.QueueFree(); } // for (int w = 0; w < nb_plats; w++) { if (rand.Next(0, 2) == 0) { if (rand.Next(0, 2) == 0) { apx -= 1; } else { apx += 1; } } else { if (rand.Next(0, 2) == 0) { apz -= 1; } else { apz += 1; } } if (apx < 1) { apx = 1; } else if (apx > mtx - 2) { apx = mtx - 2; } if (apz < 1) { apz = 1; } else if (apz > mtz - 2) { apz = mtz - 2; } walle = (mur)level.GetNode("tile-" + apx + "-" + 1 + "-" + apz); if (walle != null) { walle.QueueFree(); } } Vector3 pos_finniv = new Vector3(apx, 1, apz) * dc; return(level, player_spawn, pos_finniv, nb_plats); }
private void Init() { menu = (Control)menuScene.Instance(); soundManager = soundManagerScene.Instance(); inputHandler = inputHandlerScene.Instance(); }
public static T Instance <T>(this PackedScene src) where T : Node { return((T)src.Instance()); }
public (Spatial, Vector3, Vector3, int) generate_platforms_1() { // 0 = vide , 1=floor , 2=fake floor , 3=wall Vector3 dc = new Vector3(1F, 1F, 1F); Vector3 tc = new Vector3(1F, 1F, 1F); if (globale.difficulty == 0) { dc = new Vector3(2.5F, 1F, 2.5F); tc = new Vector3(2.5F, 1F, 2.5F); } else if (globale.difficulty == 1) { dc = new Vector3(1.5F, 1F, 1.5F); tc = new Vector3(1.5F, 1F, 1.5F); } else if (globale.difficulty == 3) { dc = new Vector3(1.5F, 1.5F, 1.5F); tc = new Vector3(0.8F, 0.8F, 0.8F); } int mtx = globale.mtx; int mty = globale.mty; int mtz = globale.mtz; int nb_plats = globale.nb_plats; int[, ,] mape = new int[mtx, mty, mtz]; Vector3 player_spawn = new Vector3(mtx / 2, mty / 2 + 2, mtz / 2) * dc; Vector3 pos_finniv; Vector3 act_platform_pos = new Vector3(mtx / 2, mty / 2, mtz / 2); Spatial level = new Spatial(); int max_security = 100; int security = 0; int final_nb_plats = 0; int dx = 0, dy = 0, dz = 0; //création for (int w = 0; w < nb_plats; w++) { //initialisation des variables security = 0; Vector3 dec = new Vector3(dx, dy, dz); //création de la platforme final_nb_plats = w; act_platform_pos = act_platform_pos + dec; PackedScene packedScene = (PackedScene)ResourceLoader.Load(globale.themes_objects[globale.arcalcenv()]["floor"]); MeshInstance floor = (MeshInstance)packedScene.Instance(); level.AddChild(floor); floor.Translation = act_platform_pos * dc; floor.Scale = tc; mape[(int)act_platform_pos.x, (int)act_platform_pos.y, (int)act_platform_pos.z] = 1; //preparation prochaine platforme dx = 0; dy = 0; dz = 0; do { security++; (dx, dy, dz) = decalage_platforms(tc); if (act_platform_pos.x + dx < 0) { dx = Convert.ToInt32(-act_platform_pos.x); } if (act_platform_pos.z + dz < 0) { dz = Convert.ToInt32(-act_platform_pos.z); } if (act_platform_pos.y + dy < 0) { dy = Convert.ToInt32(-act_platform_pos.y); } if (act_platform_pos.x + dx >= mtx) { dx = Convert.ToInt32(mtx - 1 - act_platform_pos.x); } if (act_platform_pos.z + dz >= mtz) { dz = Convert.ToInt32(mty - 1 - act_platform_pos.z); } if (act_platform_pos.y + dy >= mty) { dy = Convert.ToInt32(mtz - 1 - act_platform_pos.y); } dec = new Vector3(dx, dy, dz); }while(test_derange(act_platform_pos + dec, mape, mtx, mty, mtz) && security < max_security); //on aimerait que le niveau soit possible //test de sécurité if (security == max_security) { break; } } // pos_finniv = new Vector3(act_platform_pos.x, act_platform_pos.y + 2, act_platform_pos.z) * dc; // return(level, player_spawn, pos_finniv, final_nb_plats); }
public void Fire() { //Instance bullet, set the rotation and start position bullet = bulletScene.Instance(); this.GetParent().AddChild(bullet); }
private void _on_MobTimer_timeout() { ///////////////MONEDA/////////////////////// if (_score % 10 == 0 && _score > 0) { float x = ran.Next(1, 480); float y = ran.Next(1, 720); // Create a Mob instance and add it to the scene. var coinInstance = (RigidBody2D)Coin.Instance(); AddChild(coinInstance); // Set the mob's position to a random location. coinInstance.Position = new Vector2(x, y); } ////////////////////UFO///////////////////// if (ran.Next(1, 9) <= 2) { _score++; // Choose a random location on Path2D. var mobSpawnLocation2 = GetNode <PathFollow2D>("MobPath/MobSpawnLocation"); mobSpawnLocation2.SetOffset(_random.Next()); // Create a Mob instance and add it to the scene. var ufoInstance = (RigidBody2D)UFO.Instance(); AddChild(ufoInstance); // Set the mob's direction perpendicular to the path direction. float direction2 = mobSpawnLocation2.Rotation + Mathf.Pi / 2; // Set the mob's position to a random location. ufoInstance.Position = mobSpawnLocation2.Position; // Add some randomness to the direction. direction2 += RandRange(-Mathf.Pi / 4, Mathf.Pi / 4); ufoInstance.Rotation = direction2; // Choose the velocity. ufoInstance.SetLinearVelocity(new Vector2(RandRange(150f, 250f), 0).Rotated(direction2)); _on_ScoreTimer_timeout(); GetNode("HUD").Connect("StartGame", ufoInstance, "OnStartGame"); } else { //////////MOB///////////////////////////////// // Choose a random location on Path2D. var mobSpawnLocation = GetNode <PathFollow2D>("MobPath/MobSpawnLocation"); mobSpawnLocation.SetOffset(_random.Next()); // Create a Mob instance and add it to the scene. var mobInstance = (RigidBody2D)Mob.Instance(); AddChild(mobInstance); // Set the mob's direction perpendicular to the path direction. float direction = mobSpawnLocation.Rotation + Mathf.Pi / 2; // Set the mob's position to a random location. mobInstance.Position = mobSpawnLocation.Position; // Add some randomness to the direction. direction += RandRange(-Mathf.Pi / 4, Mathf.Pi / 4); mobInstance.Rotation = direction; // Choose the velocity. mobInstance.SetLinearVelocity(new Vector2(RandRange(150f, 250f), 0).Rotated(direction)); _on_ScoreTimer_timeout(); GetNode("HUD").Connect("StartGame", mobInstance, "OnStartGame"); } }
public void TimerOnTimeout() { var babyNode = _rng.RandiRange(1, 100) > 50 ? _maleBabyScene.Instance() : _femaleBabyScene.Instance(); EmitSignal(nameof(Birth), babyNode); }
public override void _Ready() { _viewRect = GetViewportRect(); _camera2D = GetParent().GetNode <Camera2D>("Game/Camera2D"); if (_playerScene.Instance() is Player player) { player.Position = new Vector2(100, _viewRect.Size.y / 2); GetTree().CurrentScene.AddChild(player); _player = player; } if (_levelScene.Instance() is Level level) { GetTree().CurrentScene.AddChild(level); _level = level; } if (_fleetScene.Instance() is Fleet f1) { f1.Position = new Vector2(-200, 250); GetTree().CurrentScene.AddChild(f1); fleet1 = f1; } if (_fleetScene.Instance() is Fleet f2) { f2.Position = new Vector2(-200, 450); GetTree().CurrentScene.AddChild(f2); fleet2 = f2; } if (_fleetScene.Instance() is Fleet f3) { f3.Position = new Vector2(-500, 150); GetTree().CurrentScene.AddChild(f3); fleet3 = f3; } if (_fleetScene.Instance() is Fleet f4) { f4.Position = new Vector2(-500, 350); GetTree().CurrentScene.AddChild(f4); fleet4 = f4; } if (_fleetScene.Instance() is Fleet f5) { f5.Position = new Vector2(-500, 550); GetTree().CurrentScene.AddChild(f5); fleet5 = f5; } if (_fleetScene2.Instance() is Fleet2 f6) { f6.Position = new Vector2(-800, 250); GetTree().CurrentScene.AddChild(f6); fleet6 = f6; } if (_fleetScene2.Instance() is Fleet2 f7) { f7.Position = new Vector2(-800, 450); GetTree().CurrentScene.AddChild(f7); fleet7 = f7; } if (_fleetScene2.Instance() is Fleet2 f8) { f8.Position = new Vector2(-1100, 150); GetTree().CurrentScene.AddChild(f8); fleet8 = f8; } if (_fleetScene2.Instance() is Fleet2 f9) { f9.Position = new Vector2(-1100, 350); GetTree().CurrentScene.AddChild(f9); fleet9 = f9; } if (_fleetScene2.Instance() is Fleet2 f10) { f10.Position = new Vector2(-1100, 550); GetTree().CurrentScene.AddChild(f10); fleet10 = f10; } _gameState = GameState.Intro; _player.Connect("tree_exited", this, nameof(_on_Player_tree_exited)); }
public override void _Process(float delta) { switch (_gameState) { case GameState.Game: if (!_level.IsRunning()) { if (MoveCameraBack(delta)) { _gameState = GameState.UpgradeMenu; } else { if (_hud != null) { _hud.QueueFree(); _hud = null; _player.Deactivate(); UpdateFleet(); } } } break; case GameState.PrepareGame: if (LevelIntroduction(delta)) { return; } if (_levelNumber != 1) { _level.IncreaseDifficulty(); } _levelNumber++; _level.Start(); CreateHud(); _player.SetHud(_hud); _player.Activate(); _gameState = GameState.Game; break; case GameState.GameOver: if (_level != null) { _level.QueueFree(); _level = null; } ShowGameOver(); if (MoveCameraBack(delta)) { if (_menuButton == null) { if (_menuButtonScene.Instance() is MenuButton menuButton) { menuButton.RectPosition = new Vector2(-325, 600); GetTree().CurrentScene.AddChild(menuButton); _menuButton = menuButton; } } } break; case GameState.UpgradeMenu: if (_upgradeScreen == null) { if (_upgradeScene.Instance() is UpgradeScreen upgradeScreen) { GetTree().CurrentScene.AddChild(upgradeScreen); _upgradeScreen = upgradeScreen; } } if (_flightButton == null) { if (_flightButtonScene.Instance() is FlightButton flightButton) { flightButton.RectPosition = new Vector2(-250, 600); GetTree().CurrentScene.AddChild(flightButton); _flightButton = flightButton; } } break; case GameState.Start: if (_upgradeScreen != null) { _upgradeScreen.QueueFree(); _upgradeScreen = null; } if (MoveCameraForward(delta)) { _levelWaitTime = 0; _flightButton = null; _upgradeScreen = null; _gameState = GameState.PrepareGame; } break; case GameState.Intro: if (_flightButton == null) { if (_flightButtonScene.Instance() is FlightButton flightButton) { flightButton.RectPosition = new Vector2(-250, 600); GetTree().CurrentScene.AddChild(flightButton); _flightButton = flightButton; } } break; default: throw new ArgumentOutOfRangeException(); } }