private SceneNode _parent = null; // Parent node #endregion Fields #region Constructors /// <summary> /// Constructor /// </summary> /// <param name="parent">Parent of the node</param> /// <param name="name">Name of the node</param> public SceneNode(SceneNode parent, String name) { Parent = parent; Name = name; _children = new List<SceneNode>(); }
private float _walkSpeed = 20.0f; // Walk-speed of the enemy #endregion Fields #region Constructors /// <summary> /// Constructor /// </summary> /// <param name="parent">Parent of the object</param> /// <param name="name">Name of the object</param> /// <param name="sprite">The sprite representing the enemy</param> /// <param name="walkSpeed">Walking speed of the enemy</param> public Enemy(SceneNode parent, String name, Sprite sprite, float walkSpeed) : base(parent, name, sprite) { WalkSpeed = walkSpeed; State = EnemyState.Idle; IsFixed = false; }
protected Sprite _sprite; // Sprite for the object #endregion Fields #region Constructors /// <summary> /// Constructor /// </summary> /// <param name="parent">Parent of the object</param> /// <param name="name">Name of the object</param> /// <param name="sprite">Sprite representing the object</param> public WorldObject(SceneNode parent, String name, Sprite sprite) : base(parent, name) { _sprite = sprite; _position = Vector2.Zero; _aabb = new AABB(Centre, new Vector2(sprite.Width / 2.0f, sprite.Height / 2.0f)); }
private float _walkSpeed = 20.0f; // Speed in units per second #endregion Fields #region Constructors /// <summary> /// Constructor /// </summary> /// <param name="parent">Parent of the object</param> /// <param name="name">Name of the object</param> /// <param name="sprite">The sprite representing the player</param> /// <param name="walkSpeed">Walking speed of the player</param> /// <param name="soundFX">Sound effects for the player</param> public Player(SceneNode parent, String name, Sprite sprite, float walkSpeed, Dictionary<String, SoundEffect> soundFX) : base(parent, name, sprite) { WalkSpeed = walkSpeed; _updateFunction = this.UpdateWalking; _soundFX = soundFX; IsFixed = false; }
/// <summary> /// Finds all nearby objects /// </summary> /// <param name="sourceObject">Source object</param> /// <param name="distanceThreshold">The distance from position within which an object is considered close</param> /// <param name="nearbyObjects">The list to add nearby objects to</param> public void FindNearbyObjects(SceneNode sourceObject, float distanceThreshold, ref List<WorldObject> nearbyObjects) { // Check if I'm close to the source object if (this != sourceObject) { if (distanceThreshold >= (sourceObject.Position - this.Position).Length()) { nearbyObjects.Add((WorldObject)this); } } // Check children for nearby objects if (_children.Count > 0) { for (int i = 0; i < _children.Count; ++i) { _children[i].FindNearbyObjects(sourceObject, distanceThreshold, ref nearbyObjects); } } }
/// <summary> /// LoadContent will be called once per game and is the place to load /// all of your content. /// </summary> protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); // Load the font _renderer.Font = Content.Load<SpriteFont>("Courier New"); // Load the background _background = new Background(new Sprite(Content.Load<Texture2D>("background-1"))); // Create the scenegraph _scene = new SceneNode(null, "_root_"); // Load the player // Load the player animations int fps = 6; Dictionary<String, FrameSet> playerAnimations = new Dictionary<string, FrameSet>(); List<Tuple<int, int>> idleFrames = new List<Tuple<int, int>>(); idleFrames.Add(new Tuple<int, int>(0, 0)); idleFrames.Add(new Tuple<int, int>(0, 1)); playerAnimations.Add("idle", new FrameSet(idleFrames, true, fps)); List<Tuple<int, int>> walkFrames = new List<Tuple<int, int>>(); walkFrames.Add(new Tuple<int, int>(0, 0)); playerAnimations.Add("walk", new FrameSet(walkFrames, true, fps)); List<Tuple<int, int>> crouchFrames = new List<Tuple<int, int>>(); crouchFrames.Add(new Tuple<int, int>(0, 1)); playerAnimations.Add("crouch", new FrameSet(crouchFrames, true, fps)); List<Tuple<int, int>> jumpFrames = new List<Tuple<int, int>>(); jumpFrames.Add(new Tuple<int, int>(0, 2)); playerAnimations.Add("jump", new FrameSet(jumpFrames, true, fps)); List<Tuple<int, int>> interactionFrames = new List<Tuple<int, int>>(); interactionFrames.Add(new Tuple<int, int>(0, 2)); playerAnimations.Add("interact", new FrameSet(interactionFrames, true, fps)); AnimatedSprite playerSprite = new AnimatedSprite(Content.Load<Texture2D>("player-ss"), 32, 64, playerAnimations); // Load the player sounds Dictionary<String, SoundEffect> playerSoundFX = new Dictionary<string, SoundEffect>(); playerSoundFX.Add("jump", Content.Load<SoundEffect>("jump")); playerSoundFX.Add("run", Content.Load<SoundEffect>("run")); playerSoundFX.Add("walk", Content.Load<SoundEffect>("walk")); _player = new Player(_scene, "Player", playerSprite, 100.0f, playerSoundFX); // Load the enemy Sprite enemySprite = new Sprite(Content.Load<Texture2D>("enemy")); _enemy = new Enemy(_scene, "Enemy-1", enemySprite, 20.0f); _enemy.Position = new Vector2(300.0f, 20.0f); // Load the crate WorldObject crate = new WorldObject(_scene, "Crate-1", new Sprite(Content.Load<Texture2D>("crate"))); crate.Position = new Vector2(200.0f, 200.0f); crate.IsElevated = true; WorldObject crate2 = new WorldObject(_scene, "Crate-2", new Sprite(Content.Load<Texture2D>("crate"))); crate2.Position = new Vector2(400.0f, 400.0f); }