public Npc(Game game, string name, Vector2 startPos) { _game = game; _name = name; Position = new Position { Current = startPos, Destination = startPos }; startPosition = Position.FromPosition(Position); Sprite = SpriteLoader.LoadTexture("alienBlue_front.png"); SpriteDuck = SpriteLoader.LoadTexture("alienBlue_duck.png"); _size = new Vector2(Sprite.Width / 2f, Sprite.Height / 2f); _sprite = Sprite; _movement = new TweenMovement(this); _movement.SetPosition(startPosition.Current); }
public KeyTest(Game game) { _game = game; _game.KeyDown += (sender, args) => { if (args.Key == Key.X) { var toRemove = _game.Collisionables.Where(w => w.Key is INpc npc && npc.Name() == "Alien"); foreach (var pair in toRemove) { _game.Collisionables.TryRemove(pair.Key, out _); } } if (args.Key == Key.Z) { _game.Collisionables[new Npc(_game, "Alien", new Vector2(50, 50))] = 0; _game.Collisionables[new Npc(_game, "Alien", new Vector2(150, 50))] = 0; _game.Collisionables[new Npc(_game, "Alien", new Vector2(250, 50))] = 0; } if (args.Key == Key.Space) { var aliens = _game.Collisionables.Keys .Where(w => w is INameable nameable && nameable.Name() == "Alien").ToList(); var alien = aliens[rnd.Next(aliens.Count - 1)]; var copy = Position.FromPosition(_game.LocalPlayer.Position); var options = new ProjectileOptions { Collision = new ProjectileCollisionOptions { Type = ProjectileCollisionType.SpecificTarget, SpecificTarget = (ICollisionableLocatable)alien, OnCollision = collisionable => { Console.WriteLine("Collided."); } }, MovementSpeed = 50, Sprite = SpriteLoader.LoadTexture("onfire_0001.png"), StartPosition = copy.Current }; _game.Collisionables[new Projectile(_game, _game.LocalPlayer, options)] = 0; } }; }
public void Move() { if (!_game.Focused) { return; } var state = Keyboard.GetState(0); if (!_movementKeys.Any(w => state.IsKeyDown(w))) { return; } var copy = Position.FromPosition(Position); if (state.IsKeyDown(Key.W)) { copy.SubtractY(10); } if (state.IsKeyDown(Key.S)) { copy.AddY(10); } if (state.IsKeyDown(Key.A)) { copy.SubtractX(10); } if (state.IsKeyDown(Key.D)) { copy.AddX(10); } if (CheckCollision(new RectangleF(copy.Current.X - _size.X / 2f, copy.Current.Y - _size.Y / 2f, _size.X, _size.Y))) { return; } Position = copy; }