/// <summary> /// Pull an entity toward this portal. /// </summary> void Pull(IEntityRecord entity) { Transformable2D transform = entity.GetComponent<Transformable2D>(); HasVelocity velocity = entity.GetComponent<HasVelocity>(); float distance = _transform.DistanceTo(transform); float strength = Strength * (InfluenceRadius / distance); Vector2 directionTowardsPortal = Vector2.Normalize(_transform.Position - transform.Position); Vector2 movement = directionTowardsPortal * strength; velocity.Velocity += movement; }
public void Breathe(IEntityRecord target) { if (target.HasComponent <Combustible>()) { Console.WriteLine("Firebreathe !!!"); target.GetComponent <Combustible>().Combust(); } }
public override Decision Decide(IEntityRecord opponent) { Decision decision = Decision.Undecided; History opponentHistory = opponent.GetComponent<History>(); if (opponentHistory != null) { decision = opponentHistory.MostRecentDecision; } return Decision.Distribute( decision.MostInfluencedHand, Influence); }
public override Decision React(IEntityRecord opponent, Decision decision) { Decision reaction = Decision.Undecided; History opponentHistory = opponent.GetComponent<History>(); if (opponentHistory != null) { reaction = opponentHistory.PreviousDecision; } return Decision.Distribute( reaction.MostInfluencedHand, Influence); }
/// <summary> /// Consume an entity and then do something. /// </summary> void Consume(IEntityRecord entity, Vector2 original, Vector2 target, Action then) { Transformable2D transform = entity.GetComponent<Transformable2D>(); TimeSpan duration = TimeSpan.FromSeconds(0.2); TaskManager.Main .WaitUntil( elapsed => { var t = elapsed / duration.TotalSeconds; var step = Easing.EaseIn(t, EasingType.Cubic); transform.Scale = Vector2.Lerp(original, target, step); return t >= 1; }) .Then(then); }
static void Main(string[] args) { Entity.Define("Dragon", typeof(Health), typeof(FireBreathing)); Entity.Define("Player", typeof(Health), typeof(Combustible)); IEntityRecord player = Entity.CreateFromDefinition("Player", "You!"); IEntityRecord dragon = Entity.CreateFromDefinition("Dragon", "A firebreathing dragon!"); List <Combustible> combustibles = new List <Combustible>() { new Combustible(), new Combustible(), new Combustible(), new Combustible(), new Combustible(), new Combustible(), new Combustible(), new Combustible(), new Combustible(), }; foreach (Combustible item in combustibles) { player.Add(item); } EntityRegistry.Current.SetTrigger( component => component is Combustible, (sender, eventArgs) => { foreach (IComponent component in eventArgs.Components) { Combustible combustible = component as Combustible; if (combustible != null) { if (combustible.IsOutOfSync) { combustibles.Remove(combustible); } else { combustibles.Add(combustible); } } } }); EntityRegistry.Current.Synchronize(); dragon.GetComponent <FireBreathing>().Breathe(player); Random r = new Random(); for (int turn = 0; turn < 10; turn++) { foreach (Combustible combustible in combustibles) { if (combustible.IsBurning) { combustible.Burn(); } } Combustible fire = player.GetComponent <Combustible>(); Health condition = player.GetComponent <Health>(); if (fire.IsBurning) { // There's a 10% chance that the player figures out how to extinguish himself! bool playerStoppedDroppedAndRolled = r.Next(0, 100) <= 10; if (playerStoppedDroppedAndRolled) { Console.WriteLine("Player extinguished himself !!!"); fire.Extinguish(); } } if (condition.IsDead) { // Unfortunately for the player, he did not figure it out in time. Console.WriteLine("Player is dead ://"); break; } } }
/// <summary> /// Make entity appear at destination after being consumed by portal. /// </summary> void Eject(IEntityRecord entity, Vector2 original, Vector2 target) { if (!_entitiesBeingTeleported.Contains(entity)) { return; } Transformable2D transform = entity.GetComponent<Transformable2D>(); TimeSpan duration = TimeSpan.FromSeconds(0.2); transform.Position = Destination.Position; TaskManager.Main .WaitUntil( elapsed => { var t = elapsed / duration.TotalSeconds; var step = Easing.EaseOut(t, EasingType.Cubic); transform.Scale = Vector2.Lerp(original, target, step); return t >= 1; }) .Then( () => { transform.Scale = target; _entitiesBeingTeleported.Remove(entity); }); }
/// <summary> /// Teleport entity to portal destination. /// </summary> void Teleport(IEntityRecord entity) { if (_entitiesBeingTeleported.Contains(entity)) { return; } _entitiesBeingTeleported.Add(entity); Transformable2D transform = entity.GetComponent<Transformable2D>(); Vector2 originalScale = new Vector2(transform.Scale.X); Vector2 targetScale = Vector2.Zero; Consume(entity, originalScale, targetScale, () => { Eject(entity, targetScale, originalScale); }); }
void CreateTheDungeon(SpriteBatch spriteBatch) { _dungeon = Entity.Create(Entities.Game.Dungeon, new Transformable2D() { Position = new Vector2( -(GameContext.Bounds.Width / 2), -(GameContext.Bounds.Height / 2)) }); SpriteGridSettings gridSettings = new SpriteGridSettings() { SpriteBatch = spriteBatch, Columns = DungeonColumns, Rows = DungeonRows }; gridSettings.Layer = 0; Entity.Create(Entities.Game.DungeonFloor, new Transformable2D() { Parent = _dungeon.GetComponent<Transformable2D>() }, new SpriteGrid(gridSettings) { Texture = Texture.FromFile("Content/Graphics/floor.png") } ); string dungeonWallsMap = "11111011111" + "10000000001" + "10000000001" + "10000000001" + "10001000001" + "10001000001" + "10111110001" + "10001100001" + "10002000001" + "10000000001" + "10000000011" + "10000021111" + "10000000011" + "10000000001" + "10000000021" + "11000000011"; gridSettings.Layer = 1; Entity.Create(Entities.Game.DungeonWalls, new Transformable2D() { Parent = _dungeon.GetComponent<Transformable2D>() }, new MappedSpriteGrid(gridSettings, dungeonWallsMap) { Textures = new Texture[] { Texture.FromFile("Content/Graphics/wall.png"), // #1 Texture.FromFile("Content/Graphics/wall-broken.png") // #2 } } ); string dungeonEnemiesMap = "00000000000" + "00001111000" + "00000100000" + "00000000000" + "00010000000" + "00000010000" + "01000000000" + "01100010000" + "00000100000" + "00011100000" + "00000000000" + "00000000000" + "00000001100" + "00000000000" + "00000000000" + "00000000000"; Entity.Create(Entities.Game.DungeonEnemies, new Transformable2D() { Parent = _dungeon.GetComponent<Transformable2D>() }, new MappedSpriteGrid(gridSettings, dungeonEnemiesMap) { Texture = Texture.FromFile("Content/Graphics/enemy.png") } ); }
void CreateSquad(SpriteBatch spriteBatch) { Entity.Define("squad-unit", typeof(Health)); Texture unitTexture = Texture.FromFile("Content/Graphics/unit.png"); _squadLeader = CreateSquadUnit("leader", _dungeon, new Vector2(unitTexture.Width * (DungeonColumns / 2), 0)); _squadLeader.Add( new SquadLeader() { MovementInPixels = unitTexture.Width }); IEntityRecord squadUnitLeft = CreateSquadUnit("left", _squadLeader, new Vector2(-unitTexture.Width, -unitTexture.Width)); IEntityRecord squadUnitRight = CreateSquadUnit("right", _squadLeader, new Vector2(unitTexture.Width, -unitTexture.Width)); IEntityRecord squadUnitMiddle = CreateSquadUnit("middle", _squadLeader, new Vector2(0, -unitTexture.Width)); spriteBatch.Add(_squadLeader.GetComponent<Sprite>()); spriteBatch.Add(squadUnitLeft.GetComponent<Sprite>()); spriteBatch.Add(squadUnitRight.GetComponent<Sprite>()); spriteBatch.Add(squadUnitMiddle.GetComponent<Sprite>()); }
IEntityRecord CreateSquadUnit(string name, IEntityRecord parent, Vector2 formationPosition) { IEntityRecord squadUnit = Entity.CreateFromDefinition("squad-unit", String.Format("{0}~{1}", Entities.Game.Squad, name), new Transformable2D() { Parent = parent.GetComponent<Transformable2D>(), Position = formationPosition }, new Sprite() { Layer = 2, Texture = Texture.FromFile("Content/Graphics/unit.png") }); return squadUnit; }