/// <summary> /// /// </summary> public override void onAddedToScene() { base.onAddedToScene(); //Add a static sprite to the player for standing Subtexture tex = new Subtexture(this.scene.content.Load <Texture2D>("Sprites/StandingCharacter")); playerSprite.setRenderLayer(-99); //Make sure that the player sprite is always on top playerSprite.setSubtexture(tex); addComponent(playerSprite); //Set animation sprite from spritesheet Texture2D spriteSheet = this.scene.content.Load <Texture2D>("Sprites/WalkingSpriteSheet"); int subSpriteWidth = 18; int subSpriteHeight = 23; //Goes through 4 times because there are 4 walking animation frames //Gets the sub sprite images out of the spritesheet for (int i = 0; i < 4; i++) { ; Rectangle sourceRectangle = new Rectangle((i * subSpriteWidth), 0, subSpriteWidth, subSpriteHeight); Texture2D cropTexture = new Texture2D(graphics, sourceRectangle.Width, sourceRectangle.Height); Color[] data = new Color[sourceRectangle.Width * sourceRectangle.Height]; spriteSheet.GetData(0, sourceRectangle, data, 0, data.Length); cropTexture.SetData(data); walkingAnimation.addFrame(new Subtexture(cropTexture)); } playerSprite.addAnimation(Animations.WalkingRight, walkingAnimation); playerSprite.addAnimation(Animations.standing, new SpriteAnimation().addFrame(tex)); //Set the standing animation (right now it is just a single frame) //Add the controller and mover and add collision layers to the mover addComponent <PlayerController>(); TiledMapMover mapMover = new TiledMapMover(map.getLayer <TiledTileLayer>("CollidableLayer1")); //Add the first collision layer for (int i = 1; i < numCollisionLayers; i++) { int num = i + 1; string layerName = "CollidableLayer" + num.ToString(); mapMover.addCollisionLayer(map.getLayer <TiledTileLayer>(layerName)); } addComponent(mapMover); addCollider(new BoxCollider(-tex.sourceRect.Width / 2, -tex.sourceRect.Height / 2, tex.sourceRect.Width, tex.sourceRect.Height)); //Add an ObjectInteractor addComponent(new ObjectInteractor(this.scene, map, numObjLayers)); //Add ItemHandler addComponent(new ItemHandler()); }
/// <summary> /// /// </summary> public override void onAddedToScene() { base.onAddedToScene(); //Add a static sprite to the enemy for standing Subtexture tex = new Subtexture(this.scene.content.Load <Texture2D>("Sprites/DefaultSmallEnemy")); enemySprite.setRenderLayer(-9); enemySprite.setSubtexture(tex); addComponent(enemySprite); //Add the physics collider BoxCollider mainCollider = new BoxCollider(-tex.sourceRect.Width / 2, -tex.sourceRect.Height / 2, tex.sourceRect.Width, tex.sourceRect.Height); addCollider(mainCollider); //Attack area //This is the box around the enemy that if a player is in it then they will actively attack the player int xMult = 15; int yMult = 7; BoxCollider attackCollider = new BoxCollider((-tex.sourceRect.Width * xMult) / 2, (-tex.sourceRect.Height * yMult) / 2, tex.sourceRect.Width * xMult, tex.sourceRect.Height * yMult); addCollider(attackCollider); Vector2 updatedPos = new Vector2(transform.position.X, transform.position.Y - mainCollider.height / 2); transform.setPosition(updatedPos); //Create and add the mover mapMover = new TiledMapMover(map.getLayer <TiledTileLayer>("CollidableLayer1")); //Add the first collision layer for (int i = 1; i < numCollisionLayers; i++) { int num = i + 1; string layerName = "CollidableLayer" + num.ToString(); mapMover.addCollisionLayer(map.getLayer <TiledTileLayer>(layerName)); } addComponent(mapMover); //Add enemy controller enemyController = new GroundEnemyController(mapMover, mainCollider, attackCollider); addComponent(enemyController); }
/// <summary> /// Does all the stuff needed when essentially spawning in /// </summary> public override void onAddedToScene() { base.onAddedToScene(); state = States.dropped; MasterScene mScene = this.scene as MasterScene; swordSprite.setRenderLayer(-9); addComponent(swordSprite); Rectangle sourceRectangle; Texture2D cropTexture; Color[] data; #region Set Idle Animation //Set idle animation sprite from spritesheet Texture2D spriteSheet = this.scene.content.Load <Texture2D>("Sprites/DefaultSwordSpinningSpriteSheet"); int subSpriteWidth = 11; int subSpriteHeight = 24; //Goes through 4 times because there are 4 walking animation frames //Gets the sub sprite images out of the spritesheet for (int i = 0; i < 4; i++) { sourceRectangle = new Rectangle((i * subSpriteWidth), 0, subSpriteWidth, subSpriteHeight); cropTexture = new Texture2D(graphics, sourceRectangle.Width, sourceRectangle.Height); data = new Color[sourceRectangle.Width * sourceRectangle.Height]; spriteSheet.GetData(0, sourceRectangle, data, 0, data.Length); cropTexture.SetData(data); idleAnimation.addFrame(new Subtexture(cropTexture)); } swordSprite.addAnimation(Animations.idle, idleAnimation); #endregion //Then play the idle animation because it just spawned in swordSprite.play(Animations.idle); Texture2D swingSpriteSheet = this.scene.content.Load <Texture2D>("Sprites/DefaultSwordSpriteSheet"); subSpriteWidth = 22; subSpriteHeight = 24; #region Set Held Animation //Add the sprite animation for the sword being held sourceRectangle = new Rectangle(0, 0, subSpriteWidth, subSpriteHeight); cropTexture = new Texture2D(graphics, sourceRectangle.Width, sourceRectangle.Height); data = new Color[sourceRectangle.Width * sourceRectangle.Height]; swingSpriteSheet.GetData(0, sourceRectangle, data, 0, data.Length); cropTexture.SetData(data); //Subtexture heldSprite = new Subtexture(this.scene.content.Load<Texture2D>("Sprites/DefaultSword")); swordSprite.addAnimation(Animations.held, new SpriteAnimation().addFrame(new Subtexture(cropTexture))); #endregion #region Set Swing animation //Add the swinging animation //Goes through 4 times because there are 4 walking animation frames //Gets the sub sprite images out of the spritesheet for the swinging for (int i = 0; i < 4; i++) { sourceRectangle = new Rectangle((i * subSpriteWidth), 0, subSpriteWidth, subSpriteHeight); cropTexture = new Texture2D(graphics, sourceRectangle.Width, sourceRectangle.Height); data = new Color[sourceRectangle.Width * sourceRectangle.Height]; swingSpriteSheet.GetData(0, sourceRectangle, data, 0, data.Length); cropTexture.SetData(data); swingAnimation.addFrame(new Subtexture(cropTexture)); } swingAnimation.setLoop(false); swingAnimation.setFps(20); swordSprite.addAnimation(Animations.swing, swingAnimation); #endregion #region Add the controller so the object can move with physics //Add the controller and mover and add collision layers to the mover TiledMapMover mapMover = new TiledMapMover(mScene.tileMap.getLayer <TiledTileLayer>("CollidableLayer1")); //Add the first collision layer for (int i = 1; i < mScene.numOfCollisionLayers; i++) { int num = i + 1; string layerName = "CollidableLayer" + num.ToString(); mapMover.addCollisionLayer(mScene.tileMap.getLayer <TiledTileLayer>(layerName)); } addComponent(mapMover); //Add controller controller = new ObjectMapController(1500); addComponent(controller); #endregion addCollider(new BoxCollider(-swordSprite.width / 2, -swordSprite.height / 2, swordSprite.width, swordSprite.height)); //Do little bounce when spawning in controller.bounce(20); spawnTime = 25; }