//protected override void Update(GameTime gameTime)
        //{
        //    _world.Update(gameTime);
        //    base.Update(gameTime);
        //}

        public override void Destroy(Entity entity)
        {
            base.Destroy(entity);

            //When you're inside an EntitySystem there are helper methods for creating destroying entities so that you don't need to access the World instance each time.

            var enemy = (Enemy)entity.Get(Types.Enemy);
            var body  = (Body)entity.Get(Types.Body);

            var rnd = new Random();

            foreach (var item in enemy.Drop_Items)
            {
                int dx = -5 + rnd.Next() % 10;
                int dy = -5 + rnd.Next() % 10;

                var ent = World_Ref.Create_Entity(
                    Assets.It.Get <LuaTable>(item),
                    body.X + body.Width / 2 + dx,
                    body.Y + body.Height / 2 + dy
                    );

                var physics = (Physics)ent.Get(Types.Physics);
                if (physics != null)
                {
                    float angle = (float)rnd.Next() % 360;

                    physics.Apply_Force(rnd.Next() % 150, -Physics.Deg_To_Rad(angle));
                }
            }
        }
예제 #2
0
        public override void Destroy(Entity entity)
        {
            base.Destroy(entity);

            var enemy = (Enemy)entity.Get(Types.Enemy);
            var body  = (Body)entity.Get(Types.Body);

            var rnd = new Random();

            foreach (var item in enemy.Drop_Items)
            {
                int dx = -5 + rnd.Next() % 10;
                int dy = -5 + rnd.Next() % 10;

                var ent = World_Ref.Create_Entity(
                    Assets.It.Get <LuaTable>(item),
                    body.X + body.Width / 2 + dx,
                    body.Y + body.Height / 2 + dy
                    );

                var physics = (Physics)ent.Get(Types.Physics);
                if (physics != null)
                {
                    float angle = (float)rnd.Next() % 360;

                    physics.Apply_Force(rnd.Next() % 150, -Physics.Deg_To_Rad(angle));
                }
            }
        }
        public Entity Get_Player()
        {
            var entities = World_Ref.Get_All_With_Component(Types.Player);

            if (entities.Count > 0)
            {
                return(entities.Last());
            }
            return(null);
        }
        public bool Entity_Within(string tag, float x, float y, float dist)
        {
            var other = World_Ref.Find_With_Tag(tag);

            if (other != null)
            {
                var o_body = (Body)other.Get(Types.Body);
                var adist  = Vector2.Distance(o_body.Position, new Vector2(x, y));
                if (adist < dist)
                {
                    return(true);
                }
            }
            return(false);
        }
예제 #5
0
        public void Draw_Solids(SpriteBatch batch, GameCamera camera)
        {
            var gui = (Texture2D)Assets.It.Get <Texture2D>("gui");

            solids.ForEach(s => {
                //batch.Draw(gui, new Rectangle((int)s.X, (int)s.Y, (int)s.Width, (int)s.Height), new Rectangle(24, 0, 24, 24), new Color(0, 0, 0, 10));
                var proj = camera.World_To_Screen(new Vector2(s.X, s.Y));
                Debug_Drawing.Draw_Line_Rect(batch, proj.X, proj.Y, s.Width * camera.Zoom, s.Height * camera.Zoom, Color.Red);
            });

            polygons.ForEach(p =>
            {
                for (int i = 0; i < p.Points.Count; i += 2)
                {
                    var start       = camera.World_To_Screen(p.Points[i] + p.Position);
                    var other_index = i + 1;
                    if (other_index > p.Points.Count - 1)
                    {
                        other_index = 0;
                    }

                    var end = camera.World_To_Screen(p.Points[other_index] + p.Position);
                    Debug_Drawing.Draw_Line(batch, start, end, Color.Red);
                }
            });

            var bodies = World_Ref.Get_All_With_Component(Types.Physics);

            foreach (var entity in bodies)
            {
                var physics = (Physics)entity.Get(Types.Physics);
                var body    = (Body)entity.Get(Types.Body);
                //batch.Draw(gui, new Rectangle((int) body.X, (int) body.Y, (int) body.Width, (int) body.Height), new Rectangle(24, 0, 24, 24), new Color(0, 0, 0, 10));

                // TODO: make it so that it turns red if it is collideing
                var proj = camera.World_To_Screen(new Vector2(body.X, body.Y));

                var color = Color.LimeGreen;
                if (physics.Other != null)
                {
                    color = Color.Red;
                }

                Debug_Drawing.Draw_Line_Rect(batch, proj.X, proj.Y, body.Width * camera.Zoom, body.Height * camera.Zoom, color);
            }
        }
예제 #6
0
        public override void Draw(SpriteBatch batch, Entity entity)
        {
            base.Draw(batch, entity);
            var body   = (Body)(entity.Get(Component.Types.Body));
            var sprite = (Animated_Sprite)(entity.Get(Component.Types.Animation));
            var player = World_Ref.Find_With_Tag("Player");

            if (player != null)
            {
                var pbody = (Body)(player.Get(Component.Types.Body));
                if (Vector2.Distance(body.Position, pbody.Position) < 25)
                {
                    var entities = Assets.It.Get <Texture2D>("entities");
                    batch.Draw(entities, body.Position - new Vector2(-body.Width / 2, 32), new Rectangle(458, 0, 24, 24), Color.White, 0, Vector2.Zero, 0.7f, SpriteEffects.None, 1);
                }
            }
        }
예제 #7
0
        public void Create_Sword_Hitbox(World world, Physics physics, Body body)
        {
            var side = (int)physics.FacingSide;

            var hit_size = 16;
            var hit      = World_Ref.Create_Entity();

            hit.Tags.Add("Player-Hit");

            hit.Add(new Body(new Vector2(
                                 body.Center.X + (side < 0 ? hit_size * side * 2 : 0),
                                 body.Position.Y - body.Height / 2 - hit_size),

                             new Vector2(
                                 hit_size * 2.3f,
                                 hit_size * 2
                                 )));

            hit.Add(new Timed_Destroy(0.3f));

            var phy = (Physics)hit.Add(new Physics(Vector2.Zero, Physics.PType.DYNAMIC));

            phy.Blacklisted_Collision_Tags.Add("Player");

            hit.Update = (self) => {
                var h_body = (Body)self.Get(Types.Body);
                var _side  = (int)physics.FacingSide;

                h_body.Position = new Vector2(
                    body.Center.X + (side < 0 ? hit_size * side * 2 : 0),
                    body.Position.Y - body.Height / 2 - hit_size
                    );

                return(true);
            };
        }
예제 #8
0
        public override void Constant_Update(GameTime time, Entity entity)
        {
            base.Constant_Update(time, entity);

            if (timer >= 0)
            {
                timer -= (float)time.ElapsedGameTime.TotalSeconds;
            }

            var body    = (Body)(entity.Get(Component.Types.Body));
            var anim    = (Animated_Sprite)(entity.Get(Component.Types.Animation));
            var physics = (Physics)entity.Get(Component.Types.Physics);
            var npc     = (Npc)entity.Get(Types.Npc);
            var player  = World_Ref.Find_With_Tag("Player");

            if (player == null)
            {
                return;
            }

            var player_body = (Body)player.Get(Types.Body);

            if (Vector2.Distance(player_body.Center, body.Center) < Constants.NPC_TALKING_DISTANCE && dialog_box.IsOpen == false)
            {
                if (Input.It.Is_Key_Pressed(Keys.Z))
                {
                    Input.It.Reset_Key(Keys.Z);

                    // set the speaker and the target for interactions with lua functions
                    npc.Dialog.Speaker = entity;
                    npc.Dialog.Target  = player;

                    dialog_box.TryOpen(npc.Dialog);
                }
            }
        }
 public Entity Spawn(LuaTable e, float x, float y)
 {
     return(World_Ref.Create_Entity(e, x, y));
 }
 public Entity Get_With_Tag(string tag)
 {
     return(World_Ref.Find_With_Tag(tag));
 }
예제 #11
0
        public void Do_Block(LuaTable table, Entity e, AI ai, Body body, Physics physics)
        {
            int  i       = 2;
            bool running = true;

            while (running)
            {
                if (i > table.Values.Count)
                {
                    running = false; break;
                }

                var key    = table[i] as LuaTable;
                var opcode = key[1] as string;

                switch (opcode)
                {
                case "set_z":
                {
                    var to = (float)(key[2] as Double?);
                    body.Z = to;
                    break;
                }

                case "set_layer":
                {
                    var to     = (float)(key[2] as Double?);
                    var sprite = (Animated_Sprite)e.Get(Types.Animation);
                    sprite.Layer = to;
                    break;
                }

                case "entity_within":
                {
                    var tag  = key[2] as string;
                    var dist = (float)(key[3] as double?);

                    var other = World_Ref.Find_With_Tag(tag);
                    if (other != null)
                    {
                        var o_body = (Body)other.Get(Types.Body);
                        var adist  = Vector2.Distance(o_body.Position, body.Position);
                        if (adist > dist)
                        {
                            i++;
                        }
                    }
                    break;
                }

                case "track":
                {
                    var tag   = key[2] as string;
                    var other = World_Ref.Find_With_Tag(tag);
                    if (other != null)
                    {
                        var o_body = (Body)other.Get(Types.Body);
                        var dot    = body.Angle_To_Other(o_body);
                        var force  = (float)(key[3] as double?);
                        physics.Apply_Force(force, dot);
                    }

                    i++;
                    break;
                }

                case "block":
                    Do_Block(key, e, ai, body, physics);
                    break;

                case "block_with_skip":
                    Do_Block(key, e, ai, body, physics); i++;
                    break;

                case "if_timer":
                    var time = (float)(key[2] as double?);
                    if (ai.Timer < time)
                    {
                        i++;
                    }
                    break;

                case "timer_reset": ai.Timer = 0; break;

                case "new_target_dir":
                {
                    var by  = (float)(key[2] as double?);
                    var rnd = new Random();
                    var dir = (rnd.Next() % (int)by);
                    ai.Target_Angle += (float)((dir * Math.PI) / 180);
                    break;
                }

                case "move_towards_target":
                {
                    var force = (float)(key[2] as double?);
                    physics.Apply_Force(force, ai.Target_Angle);
                    break;
                }

                case "collision_with_tag":
                {
                    var tag = key[2] as string;
                    if (physics.Other != null)
                    {
                        if (!physics.Other.Tags.Contains(tag))
                        {
                            i++;
                        }
                        continue;
                    }
                    i++;
                    break;
                }

                case "destroy":
                {
                    var obj = key[2] as string;
                    if (obj == "self")
                    {
                        e.Destroy();
                    }
                    if (obj == "other")
                    {
                        if (physics.Other != null)
                        {
                            physics.Other.Destroy();
                        }
                    }
                    break;
                }

                case "face_move_dir": {
                    var sprite = (Animated_Sprite)e.Get(Types.Animation);
                    if (sprite != null)
                    {
                        if (physics.Velocity.X > 0)
                        {
                            sprite.Scale = new Vector2(1, sprite.Scale.Y);
                        }
                        else
                        {
                            sprite.Scale = new Vector2(-1, sprite.Scale.Y);
                        }
                    }
                    break;
                }

                case "set_animation": {
                    var sprite = (Animated_Sprite)e.Get(Types.Animation);
                    if (sprite != null)
                    {
                        var anim_id = key[2] as string;
                        sprite.Current_Animation_ID = anim_id;
                    }
                    break;
                }

                case "velocity_lessthan": {
                    var value = (float)(key[2] as double?);
                    if (value < physics.Current_Speed)
                    {
                        i++;
                    }
                    break;
                }

                case "velocity_greaterthen": {
                    var value = (float)(key[2] as double?);

                    if (value < physics.Current_Speed)
                    {
                        i++;
                    }
                    break;
                }

                case "set_target": {
                    var to = (float)(key[2] as double?);
                    ai.Target_Angle = (float)((to * Math.PI) / 180);
                }
                break;

                case "print_str":
                    Console.WriteLine("AI: {}", key[2] as string);
                    break;

                case "set_flag":
                    ai.Flags[(int)(key[2] as double?)] = true;
                    break;

                case "unset_flag":
                    ai.Flags[(int)(key[2] as double?)] = false;
                    break;

                case "if_flag": {
                    var flag = ai.Flags[(int)(key[2] as double?)];
                    if (!flag)
                    {
                        i++;
                    }
                    break;
                }

                case "if_not_flag":
                {
                    var flag = ai.Flags[(int)(key[2] as double?)];
                    if (flag)
                    {
                        i++;
                    }
                    break;
                }

                default:
                    Console.WriteLine($"Unknown command: { key[1] }");
                    break;
                }
                i++;
            }
        }