public Entity(SerializationInfo info, StreamingContext cntxt) : base(info, cntxt) { Name = (string)info.GetValue("Entity_Name", typeof(string)); Equipment = (Equipment)info.GetValue("Entity_Equipment", typeof(Equipment)); Stats = (EntityStats)info.GetValue("Entity_Stats", typeof(EntityStats)); XPValue = (int)info.GetValue("Entity_XPValue", typeof(int)); facing = (Direction)info.GetValue("Entity_Facing", typeof(Direction)); MAX_SPEED = (float)info.GetValue("Entity_MaxSpeed", typeof(float)); msVel = (Vector2)info.GetValue("Entity_MsVel", typeof(Vector2)); bounds = (EntityBounds)info.GetValue("Entity_EBounds", typeof(EntityBounds)); State = (EntityState)info.GetValue("Entity_State", typeof(EntityState)); // Init entity in loaded classes Stats.setEntity(this); EBounds.setEntity(this); Equipment.setEntity(this); // Un-saved values GScreen = (GameScreen)ScreenManager.getScreen(ScreenId.Game); lastHitPart = EntityPart.Body; jumpDelay = attackDelay = showHpTicks = 0; speedMultiplier = MAX_SPEED; pDrops = new PossibleDrop[0]; sprite.setFrame(250, 3); }
// return True if move successful private Boolean moveXWithVelocity(int vel) { int oldX = EBounds.X; EBounds.moveX(vel); if (Bounds.Right > Map.getPixelWidth()) { EBounds.moveX((Map.getPixelWidth() - Bounds.Width) - Bounds.X); } else if (Bounds.Left <= 0) { EBounds.moveX(-Bounds.X); } else if (vel > 0) { int newX = Map.checkBoundsXRight(Bounds, facing); if (!freeMoveForward) { updateBoundsX(newX); } } else if (vel < 0) { int newX = Map.checkBoundsXLeft(Bounds, facing); if (!freeMoveForward) { updateBoundsX(newX); } } return(oldX != EBounds.X); }
protected void setState(EntityState State) { if (Alive || State == EntityState.Dying || State == EntityState.Dead) { EBounds.resetPositions(); // Resets position Stats.resetReducers(); this.State = State; } }
public void duck() { if (isOnFloor) { setState(EntityState.Crouching); EBounds.duck(); // Resets position setXSpeedPerMs(0); Stats.headMultiplier -= 0.1f; Stats.legsMultiplier += 0.2f; } }
public void block() { // Must wait a while after attacking to block again if (isOnFloor && attackDelay < ATTACK_DELAY_MS * 0.8) { setState(EntityState.Blocking); EBounds.block(facing); // Resets position setXSpeedPerMs(0); Stats.bodyMultiplier += 0.5f; Stats.headMultiplier += 0.2f; } }
private void updateBoundsX(int newX) { if (shouldFall()) { fallWithGravity(); } else if (newX != Bounds.X) { EBounds.moveX(newX - Bounds.X); // msVel.X = 0; } }
private void updateBoundsX(TileMap map, int newX) { if (!isOnFloor && State != EntityState.Jumping) { fallWithGravity(); } else if (newX != bounds.X) { EBounds.moveX(newX - bounds.X); // msVel.X = 0; } }
public override void draw(SpriteBatch spriteBatch, Point offset, TimeSpan elapsed) { if (SlatedToRemove) { return; } Rectangle pRect = EBounds.Rect; pRect.Offset(offset); Texture2D sprite = getSprite(elapsed.Milliseconds); if (isFacingForward()) { spriteBatch.Draw(sprite, pRect, Color.White); } else { spriteBatch.Draw(sprite, pRect, sprite.Bounds, Color.White, 0, Vector2.Zero, SpriteEffects.FlipHorizontally, 0); } // Was hit resently, show hp bar if (Alive && showHpTicks != 0) { Rectangle hpRect = EBounds.Rect; hpRect.Offset(offset); hpRect.X += (int)(hpRect.Width * 0.125); // Offset and a bit over from the absolute left side hpRect.Y -= 7; hpRect.Height = 3; hpRect.Width = (int)Math.Round(hpRect.Width * 0.75 * Stats.HpPercent) + 1; spriteBatch.Draw(ScreenManager.WhiteRect, hpRect, Color.Red); Vector2 vect = ScreenManager.Small_Font.MeasureString(Name); spriteBatch.DrawString(ScreenManager.Small_Font, Name, new Vector2(pRect.Center.X - vect.X / 2, hpRect.Y - 19), Color.White); Rectangle lastRect = EBounds.getRectFromPart(lastHitPart); if (showHpTicks > HP_BAR_SHOW_MS / 2 && lastRect.Width != 0) { lastRect.Offset(offset); lastRect.X += (int)(lastRect.Height * 0.1); lastRect.Width = (int)(pRect.Width * 0.8); spriteBatch.Draw(ScreenManager.WhiteRect, lastRect, HIT_BOX_COLOR); } } }
public Entity(SerializationInfo info, StreamingContext cntxt) : base(info, cntxt) { Name = (string)info.GetValue("Name", typeof(string)); Equipment = (Equipment)info.GetValue("Equipment", typeof(Equipment)); Stats = (EntityStats)info.GetValue("Stats", typeof(EntityStats)); facing = (Direction)info.GetValue("Facing", typeof(Direction)); Bounds = (EntityBounds)info.GetValue("EBounds", typeof(EntityBounds)); State = (EntityState)info.GetValue("State", typeof(EntityState)); Properties = (Dictionary <string, bool>)info.GetValue("Props", typeof(Dictionary <string, bool>)); // Init entity in loaded classes Stats.setEntity(this); EBounds.setEntity(this); Equipment.setEntity(this); // Un-saved values commonEntityInit(); }
protected void die() { EBounds.die(); msVel.X = msVel.Y = 0; State = EntityState.Dying; }
public override void update(TimeSpan elapsed) { if (SlatedToRemove) { return; } // ### Tick updates Moved = false; tElapsed = elapsed; freeMoveForward = false; if (attackDelay > 0) { attackDelay -= elapsed.Milliseconds; } if (showHpTicks > 0) { showHpTicks -= elapsed.Milliseconds; } // ### Update entity State isOnFloor = Map.isRectOnFloor(EBounds, facing, this); if (!Alive) { if (State == EntityState.Dead) { return; } else if (State != EntityState.Dying) { die(); } else if (!isOnFloor) { EBounds.moveY(2); } else { setState(EntityState.Dead); dropItems(); } return; } // ### Update movement State based on movement if (msVel.X != 0 && State != EntityState.Jumping) { setState(EntityState.Moving); if (msVel.X < 0) { facing = Direction.Left; } else { facing = Direction.Right; } } else if (State == EntityState.Moving) // If State still 'Moving' but not moving, change State { setState(EntityState.Standing); } // ### Update X position int currXSpeed = (int)(getRealXSpeed() * speedMultiplier); if (attackDelay > ATTACK_DELAY_MS * 0.625f && speedMultiplier > 0.25f) // If attacked recently while jumping, move slower { speedMultiplier *= 0.93f; } else if (speedMultiplier < MAX_SPEED) { speedMultiplier += 0.033f; } else { speedMultiplier = MAX_SPEED; // Don't overshoot } Moved = moveXWithVelocity(currXSpeed); // ### Update Y Position if (State == EntityState.Jumping) // Gravity { msVel.Y -= GRAVITY_PER_MS; } else if (jumpDelay > 0) // Tick jump delay { jumpDelay -= elapsed.Milliseconds; } // Subtract so everything else doesn't have to be switched (0 is top) EBounds.moveY((int)-getRealYSpeed()); if (Bounds.Top >= Map.getPixelHeight() - Bounds.Height / 2) { EBounds.moveY((int)getRealYSpeed()); // Undo the move fallWithGravity(); } else if (Bounds.Bottom <= 0) { EBounds.moveY((int)getRealYSpeed()); // Undo the move hitGround(); } else if (getRealYSpeed() > 0) { int newY = Map.checkBoundsYUp(Bounds, facing); if (newY != Bounds.Y) // Hit something { EBounds.moveY(newY - Bounds.Y); // Move down correct amount (+) fallWithGravity(); } } else if (getRealYSpeed() < 0) { int newY = Map.checkBoundsYDown(Bounds, facing); if (newY != Bounds.Y) // Hit something { EBounds.moveY(newY - Bounds.Y); // Move up correct amount (-) hitGround(); } } // ### Run the entities customizable AI if (aiEnabled()) { AI.run(); } }
public override void draw(SpriteBatch spriteBatch, Point offset, TimeSpan elapsed) { // Draw hp bar if (Stats.Hp > 0) { Rectangle hpRect = new Rectangle(2, 2, 100, 16); hpRect.Width = (int)Math.Round(hpRect.Width * Stats.HpPercent) + 1; spriteBatch.Draw(ScreenManager.WhiteRect, hpRect, Color.Green); } else { spriteBatch.DrawString(ScreenManager.Small_Font, "Dead", new Vector2(12, 0), Color.White); } // Draw level spriteBatch.DrawString(ScreenManager.Small_Font, "Lvl " + Stats.Level + " " + Name, new Vector2(105, 0), Color.White); // Draw entity Rectangle pRect = EBounds.Rect; pRect.Offset(offset); Texture2D sprite = getSprite(elapsed.Milliseconds); if (isFacingForward()) { spriteBatch.Draw(sprite, pRect, Color.White); } else { spriteBatch.Draw(sprite, pRect, sprite.Bounds, Color.White, 0, Vector2.Zero, SpriteEffects.FlipHorizontally, 0); } // Draw armour stats if (Alive) { // Draw armour Equipment.draw(spriteBatch, offset); // Draw xp bar Rectangle xpBar = new Rectangle(0, spriteBatch.GraphicsDevice.Viewport.Height - 3, (int)(xp / (float)nextLevelXP * spriteBatch.GraphicsDevice.Viewport.Width), 2); spriteBatch.Draw(ScreenManager.WhiteRect, xpBar, Color.LightBlue); Viewport vp = spriteBatch.GraphicsDevice.Viewport; Rectangle armourImg = new Rectangle(vp.Width - 45, 2, 32, 32); Texture2D img; switch (State) { case EntityState.Blocking: img = GameScreen.sprGUI[GUISpriteId.Blocking]; break; case EntityState.Crouching: img = GameScreen.sprGUI[GUISpriteId.Ducking]; break; default: img = GameScreen.sprGUI[GUISpriteId.Standing]; break; } if (isFacingForward()) { spriteBatch.Draw(img, armourImg, Color.Black); } else { spriteBatch.Draw(img, armourImg, img.Bounds, Color.Black, 0, Vector2.Zero, SpriteEffects.FlipHorizontally, 0); } spriteBatch.DrawString(ScreenManager.Small_Font, Stats.THeadMultiplier.ToString("0.0"), new Vector2(vp.Width - 38, 0), Color.White); spriteBatch.DrawString(ScreenManager.Small_Font, Stats.TBodyMultiplier.ToString("0.0"), new Vector2(vp.Width - 38, 11), Color.White); spriteBatch.DrawString(ScreenManager.Small_Font, Stats.TLegsMultiplier.ToString("0.0"), new Vector2(vp.Width - 38, 22), Color.White); // Draw hit box if (showHpTicks > HP_BAR_SHOW_MS / 2) { Rectangle lastRect = EBounds.getRectFromPart(lastHitPart); if (lastRect.Width != 0) { lastRect.Offset(offset); lastRect.X += (int)(lastRect.Height * 0.1); lastRect.Width = (int)(lastRect.Width * 0.8); spriteBatch.Draw(ScreenManager.WhiteRect, lastRect, HIT_BOX_COLOR); } } } }
public void moveTo(Vector2 targ) { EBounds.moveX((int)targ.X - Bounds.X); EBounds.moveY((int)targ.Y - Bounds.Y); }
public override void update(TileMap map, TimeSpan elapsed) { if (SlatedToRemove) { return; } tElapsed = elapsed; isOnFloor = map.isRectOnFloor(EBounds.StandRect); if (attackDelay > 0) { attackDelay -= elapsed.Milliseconds; } if (showHpTicks > 0) { showHpTicks -= elapsed.Milliseconds; } // ### Update entity State if (!Alive) { DidMove = false; if (State == EntityState.Dead) { return; } else if (State != EntityState.Dying) { die(); } else if (!isOnFloor) { EBounds.moveY(2); } else { setState(EntityState.Dead); dropItems(); } return; } isOnFloor = map.isRectOnFloor(EBounds.StandRect); // ### Run the entities customizable AI if (ai != null) { ai(this, map); } else { runAI(map); } // ### Update movement State based on movement if (msVel.X != 0 && State != EntityState.Jumping) { setState(EntityState.Moving); if (msVel.X < 0) { facing = Direction.Left; } else { facing = Direction.Right; } } else if (State == EntityState.Moving) // If State still 'Moving' but not moving, change State { setState(EntityState.Standing); } // ### Update X position int currXSpeed = (int)(getRealXSpeed() * speedMultiplier); if (attackDelay > ATTACK_DELAY_MS * 0.625f && speedMultiplier > 0.25f) // If attacked recently while jumping, move slower { speedMultiplier *= 0.93f; } else if (speedMultiplier < MAX_SPEED) { speedMultiplier += 0.033f; } else { speedMultiplier = MAX_SPEED; // Don't overshoot } int oldX = EBounds.X; EBounds.moveX(currXSpeed); if (bounds.Right > map.getPixelWidth()) { EBounds.moveX((map.getPixelWidth() - bounds.Width) - bounds.X); // msVel.X = 0; } else if (bounds.Left <= 0) { EBounds.moveX(-bounds.X); // msVel.X = 0; } else if (msVel.X > 0) { int newX = map.checkBoundsXRight(bounds.Rect); updateBoundsX(map, newX); } else if (msVel.X < 0) { int newX = map.checkBoundsXLeft(bounds.Rect); updateBoundsX(map, newX); } if (oldX != EBounds.X) { DidMove = true; } else { DidMove = false; } // ### Update Y Position if (State == EntityState.Jumping) // Gravity { msVel.Y -= GRAVITY_PER_MS; } else if (jumpDelay > 0) // Tick jump delay { jumpDelay -= elapsed.Milliseconds; } // Subtract so everything else doesn't have to be switched (0 is top) EBounds.moveY((int)-getRealYSpeed()); if (bounds.Top >= map.getPixelHeight() - bounds.Height / 2) { EBounds.moveY((int)getRealYSpeed()); // Undo the move fallWithGravity(); } else if (bounds.Bottom <= 0) { EBounds.moveY((int)getRealYSpeed()); // Undo the move hitGround(); } else if (getRealYSpeed() > 0) { int newY = map.checkBoundsYUp(bounds.Rect); if (newY != bounds.Y) // Hit something { EBounds.moveY(newY - bounds.Y); // Move down correct amount (+) fallWithGravity(); } } else if (getRealYSpeed() < 0) { int newY = map.checkBoundsYDown(bounds.Rect); if (newY != bounds.Y) // Hit something { EBounds.moveY(newY - bounds.Y); // Move up correct amount (-) hitGround(); } } }