public static void DrawBackedText(this SpriteBatch graphics, string text, BfbVector position, BFBContentManager content, float scale = 1f) { SpriteFont font = content.GetFont("default"); Texture2D texture = content.GetTexture("default"); (float width, float height) = font.MeasureString(text); width *= scale; height *= scale; //Background graphics.Draw( texture, new Rectangle((int)position.X - 2, (int)position.Y - 2, (int)width + 4, (int)height + 2), new Color(0, 0, 0, 0.5f)); graphics.DrawString( font, text, position.ToVector2(), Color.White, 0f, Vector2.Zero, scale, SpriteEffects.None, 1); }
public PhysicsItem() : base(false) { Acceleration = new BfbVector(5, 5); MaxSpeed = new BfbVector(20, 29); Gravity = 4f; Friction = 0.8f; }
/// <summary> /// Constructs a Walking Physics Component /// </summary> public PhysicsWalking(int maxSpeed = 20) : base(false) { Acceleration = new BfbVector(5, 25); MaxSpeed = new BfbVector(maxSpeed, 29); Gravity = 4f; Friction = 0.8f; }
/// <summary> /// Creates a new entity for the game simulations /// </summary> /// <param name="entityId">Unique ID for this entity</param> /// <param name="options">Sets the initial properties of this entity</param> /// <param name="socket">The socket object if the entity is a player</param> private SimulationEntity(string entityId, EntityOptions options, ClientSocket socket = null) : base(entityId, options) { _currentTick = -1; TicksSinceCreation = -1; ParentEntityId = null; SteeringVector = new BfbVector(); OldPosition = new BfbVector(); //Default collision info CollideFilter = "entity"; CollideWithFilters = new List <string> { "tile" }; //Components EntityComponents = new List <EntityComponent>(); if (socket != null) { VisibleChunks = new List <string>(); ChunkVersions = new Dictionary <string, int>(); InventorySlotHistory = new List <byte>(); Socket = socket; } }
public ControlState() { Left = false; Right = false; Jump = false; LeftClick = false; RightClick = false; DropItem = false; HotBarPosition = 0; Mouse = new BfbVector(0, 0); }
public PhysicsProjectile(int maxSpeed = 40, bool useGravity = false) : base(false) { Acceleration = new BfbVector(maxSpeed, maxSpeed); MaxSpeed = new BfbVector(maxSpeed, maxSpeed); Gravity = 2f; Friction = 0.99f; _useGravity = useGravity; _targetsHit = new List <SimulationEntity>(); _hitTarget = false; }
/// <summary> /// Creates a new Entity /// </summary> /// <param name="entityId">Unique ID for this entity</param> /// <param name="options">Options object to be applied to this entity</param> protected Entity(string entityId, EntityOptions options) { EntityId = entityId; TextureKey = options.TextureKey; AnimationState = AnimationState.IdleRight; Position = options.Position ?? new BfbVector(); Dimensions = options.Dimensions; Origin = options.Origin; Rotation = options.Rotation; EntityType = options.EntityType; Grounded = false; Velocity = new BfbVector(); Facing = DirectionFacing.Left; }
public BfbVector ViewPointToMapPoint(BfbVector point) { if (!_init) { return(null); } BfbVector translatedPoint = new BfbVector(point.ToVector2()); translatedPoint.X /= Camera.GetScale().X; translatedPoint.Y /= Camera.GetScale().Y; translatedPoint.X += Camera.Position.X - Camera.Origin.X; translatedPoint.Y += Camera.Position.Y - Camera.Origin.Y; return(translatedPoint); }
public void Use(Simulation simulation, SimulationEntity entity, IItem item) { ItemConfiguration itemConfig = item.Configuration; if (entity.ControlState == null || entity.Meta == null || entity.Meta.Mana < itemConfig.ManaCost) { return; } entity.Meta.Health += itemConfig.HealthGain; entity.Meta.Health -= itemConfig.HealthCost; entity.Meta.Mana += itemConfig.ManaGain; entity.Meta.Mana -= itemConfig.ManaCost; if (entity.Meta.Health > entity.EntityConfiguration.Health) { entity.Meta.Health = entity.EntityConfiguration.Health; } if (entity.Meta.Mana > entity.EntityConfiguration.Mana) { entity.Meta.Health = entity.EntityConfiguration.Mana; } BfbVector directionVector = BfbVector.Sub(entity.ControlState.Mouse, entity.OriginPosition); float direction = (float)System.Math.Atan2(directionVector.Y, directionVector.X) + (float)(System.Math.PI / 2); //place a copy of the spell config inside the projectile InventoryManager inventory = new InventoryManager(); Item newItem = new Item(item.ItemConfigKey); inventory.Insert(newItem); SimulationEntity spellEntity = SimulationEntity.SimulationEntityFactory(itemConfig.EntitySpawnKey); spellEntity.SteeringVector = directionVector; spellEntity.Rotation = direction; spellEntity.ParentEntityId = entity.EntityId; spellEntity.Position = entity.OriginPosition.Clone(); spellEntity.Inventory = inventory; simulation.AddEntity(spellEntity); }
private void DebugPanel(SpriteBatch graphics, GameTime time, WorldManager world, ClientEntity entity, List <ClientEntity> entities, ClientSocketManager connection, ControlState input) { const int xPos = 5; const int offset = 24; int yPos = 120; graphics.DrawBackedText($"Client FPS: {System.Math.Round(1f/(float)time.ElapsedGameTime.TotalSeconds)}/60", new BfbVector(xPos, yPos), _content, 0.5f); graphics.DrawBackedText($"Server TPS: {connection.Tps}/20", new BfbVector(xPos, yPos += offset), _content, 0.5f); graphics.DrawBackedText($"X: {entity.Left}, Y: {entity.Top}", new BfbVector(xPos, yPos += offset), _content, 0.5f); Chunk chunk = world.ChunkFromPixelLocation(entity.Left, entity.Top); graphics.DrawBackedText($"Chunk-X: {chunk?.ChunkX ?? 0}, Chunk-Y: {chunk?.ChunkY ?? 0}", new BfbVector(xPos, yPos += offset), _content, 0.5f); graphics.DrawBackedText($"Velocity-X: {entity.Velocity.X}, Velocity-Y: {entity.Velocity.Y}", new BfbVector(xPos, yPos += offset), _content, 0.5f); graphics.DrawBackedText($"Facing: {entity.Facing}", new BfbVector(xPos, yPos += offset), _content, 0.5f); BfbVector mouse = ViewPointToMapPoint(input.Mouse); (int blockX, int blockY) = world.BlockLocationFromPixel((int)mouse.X, (int)mouse.Y) ?? new Tuple <int, int>(0, 0); graphics.DrawBackedText($"Mouse-X: {(int)mouse.X}, Mouse-Y: {(int)mouse.Y}", new BfbVector(xPos, yPos += offset), _content, 0.5f); graphics.DrawBackedText($"Block-X: {blockX}, Block-Y: {blockY}, Block: {world.GetBlock(blockX,blockY)}, Wall: {(WorldTile)world.GetWall(blockX,blockY)}", new BfbVector(xPos, yPos += offset), _content, 0.5f); graphics.DrawBackedText($"Entities: {entities.Count}, Players: {entities.Count(x => x.EntityType == EntityType.Player)}, Items: {entities.Count(x => x.EntityType == EntityType.Item)}, Mobs: {entities.Count(x => x.EntityType == EntityType.Mob)}, Projectiles: {entities.Count(x => x.EntityType == EntityType.Projectile)}, Particles: {entities.Count(x => x.EntityType == EntityType.Particle)}", new BfbVector(xPos, yPos += offset), _content, 0.5f); graphics.DrawBackedText("Press F3 to exit Debug", new BfbVector(xPos, yPos + offset * 2), _content, 0.5f); }
protected override void Init() { Inventory = ClientDataRegistry.GetInstance().Inventory; AddInputListener("keypress", e => { switch (e.Keyboard.KeyEnum) { case Keys.Escape: case Keys.E: ParentScene.Client.Emit("/inventory/throwHolding"); UIManager.StopLayer(Key); break; } e.StopPropagation(); }); AddInputListener("mouseclick", e => { if (e.Mouse.RightButton == ButtonState.Pressed) { ParentScene.Client.Emit("/inventory/throwHolding", new DataMessage { Message = "one" }); } else { ParentScene.Client.Emit("/inventory/throwHolding"); } }); AddInputListener("mousemove", e => { Mouse = new BfbVector(e.Mouse.X, e.Mouse.Y); }); }
/// <summary> /// Updates this entity as part of the chunks in the simulation of this entity /// </summary> /// <param name="simulation"></param> public void Tick(Simulation.Simulation simulation) { TicksSinceCreation++; //Only tick entity once per frame if (simulation.Tick == _currentTick) { return; } //Record last tick _currentTick = simulation.Tick; //Record current position OldPosition = new BfbVector(Position.X, Position.Y); //the future foreach (EntityComponent gameComponent in EntityComponents) { gameComponent?.Update(this, simulation); } MoveEntity(simulation); }
public void Draw(SpriteBatch graphics, GameTime time, WorldManager world, List <ClientEntity> entities, ClientEntity playerEntity, ControlState input, ClientSocketManager clientSocket = null) { if (!_init) { return; } float worldScale = _tileScale / GraphicsScale; #region graphics.Begin() //Start different graphics layer graphics.End(); graphics.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, DepthStencilState.None, RasterizerState.CullCounterClockwise, null, Camera.Transform); #endregion #region DrawRanges int xStart = Camera.Left / _tileScale - 1; int xEnd = Camera.Right / _tileScale + 2; int yStart = Camera.Top / _tileScale - 1; int yEnd = Camera.Bottom / _tileScale + 2; if (xStart < 0) { xStart = 0; } if (xEnd > _blockWidth) { xEnd = _blockWidth; } if (yStart < 0) { yStart = 0; } if (yEnd > _blockHeight) { yEnd = _blockHeight; } #endregion #region Render Walls + Blocks for (int y = yStart; y < yEnd; y++) { for (int x = xStart; x < xEnd; x++) { int xPosition = x * _tileScale; int yPosition = y * _tileScale; if (world.GetBlock(x, y) == WorldTile.Air) { WorldTile tile = (WorldTile)world.GetWall(x, y); if (tile == WorldTile.Air) { continue; } graphics.DrawAtlas( _content.GetAtlasTexture("Tiles:" + tile), new Rectangle(xPosition, yPosition, _tileScale, _tileScale) , Color.White); graphics.DrawAtlas( _content.GetAtlasTexture("Tiles:" + tile), new Rectangle(xPosition, yPosition, _tileScale, _tileScale) , new Color(0, 0, 0, 0.4f)); } else { WorldTile tile = world.GetBlock(x, y); if (tile == WorldTile.Leaves) { WorldTile wallTile = (WorldTile)world.GetWall(x, y); if (wallTile != WorldTile.Air) { graphics.DrawAtlas( _content.GetAtlasTexture("Tiles:" + wallTile), new Rectangle(xPosition, yPosition, _tileScale, _tileScale) , Color.White); graphics.DrawAtlas( _content.GetAtlasTexture("Tiles:" + wallTile), new Rectangle(xPosition, yPosition, _tileScale, _tileScale) , new Color(0, 0, 0, 0.4f)); } } if (tile != WorldTile.Air) { graphics.DrawAtlas( _content.GetAtlasTexture("Tiles:" + tile), new Rectangle(xPosition, yPosition, _tileScale, _tileScale), Color.White); } } } } #endregion #region Mouse ToolTip if (playerEntity.Meta != null && playerEntity.Meta.Holding.ItemType != ItemType.Unknown) { //Get proper mouse coordinates BfbVector mouse = ViewPointToMapPoint(input.Mouse); Tuple <int, int> block = world.BlockLocationFromPixel((int)mouse.X, (int)mouse.Y); //if the mouse is inside the map if (block != null) { int playerX = (int)(playerEntity.Position.X + playerEntity.Width / 2f); int playerY = (int)(playerEntity.Position.Y + playerEntity.Height / 2f); int blockPixelX = block.Item1 * _tileScale; //x position of block mouse is over int blockPixelY = block.Item2 * _tileScale; //y position of block mouse is over int distance = (int)System.Math.Sqrt(System.Math.Pow(playerX - blockPixelX, 2) + System.Math.Pow(playerY - blockPixelY, 2)) / _tileScale; int reach = playerEntity.Meta.Holding.Reach; switch (playerEntity.Meta.Holding.ItemType) { case ItemType.Block: if (world.GetBlock(block.Item1, block.Item2) == WorldTile.Air) { graphics.Draw(_content.GetTexture("default"), new Rectangle(blockPixelX, blockPixelY, _tileScale, _tileScale), distance >= reach ? new Color(255, 0, 0, 0.2f) : new Color(0, 0, 0, 0.2f)); } else { float progress = playerEntity.Meta.Holding.Progress; if (progress < 0.05f) { graphics.DrawBorder( new Rectangle(blockPixelX, blockPixelY, _tileScale, _tileScale), 2, new Color(0, 0, 0, 0.6f), _content.GetTexture("default")); } else { graphics.Draw(_content.GetTexture("default"), new Rectangle(blockPixelX + (int)(_tileScale * (1 - progress)) / 2, blockPixelY + (int)(_tileScale * (1 - progress)) / 2, (int)(_tileScale * progress), (int)(_tileScale * progress)), new Color(0, 0, 0, 0.4f)); } } break; case ItemType.Wall: if (world.GetBlock(block.Item1, block.Item2) == WorldTile.Air && (WorldTile)world.GetWall(block.Item1, block.Item2) == WorldTile.Air) { graphics.Draw(_content.GetTexture("default"), new Rectangle(blockPixelX, blockPixelY, _tileScale, _tileScale), distance >= reach ? new Color(255, 0, 0, 0.2f) : new Color(0, 0, 0, 0.2f)); } else { float progress = playerEntity.Meta.Holding.Progress; if (progress < 0.05f) { graphics.DrawBorder( new Rectangle(blockPixelX, blockPixelY, _tileScale, _tileScale), 2, new Color(0, 0, 0, 0.6f), _content.GetTexture("default")); } else { graphics.Draw(_content.GetTexture("default"), new Rectangle(blockPixelX + (int)(_tileScale * (1 - progress)) / 2, blockPixelY + (int)(_tileScale * (1 - progress)) / 2, (int)(_tileScale * progress), (int)(_tileScale * progress)), new Color(0, 0, 0, 0.4f)); } } break; case ItemType.Tool: var atlas = _content.GetAtlasTexture(playerEntity.Meta.Holding.AtlasKey); graphics.DrawAtlas(atlas, new Rectangle( (int)(mouse.X - atlas.Width / 2f), (int)(mouse.Y - atlas.Height / 2f), (int)(atlas.Width * worldScale), (int)(atlas.Height * worldScale)), distance > reach ? new Color(255, 0, 0, 0.1f) : new Color(255, 255, 255, 0.1f)); break; } } } #endregion #region Map Debug if (Debug) { MapDebug(graphics, world, xStart, xEnd, yStart, yEnd); } #endregion #region Render Entities foreach (ClientEntity entity in entities.OrderBy(x => x.Position.Y).ThenBy(x => x.EntityType)) { if (!Debug) { entity.Draw(graphics, _content, worldScale); } else { entity.DebugDraw(graphics, _content, worldScale, _tileScale); } } #endregion #region graphics.End() graphics.End(); graphics.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, DepthStencilState.None, RasterizerState.CullCounterClockwise); #endregion #region Debug Panel if (Debug) { DebugPanel(graphics, time, world, playerEntity, entities, clientSocket, input); } #endregion }
public InventoryUI() : base(nameof(InventoryUI)) { BlockInput = true; Mouse = new BfbVector(); }