Example #1
0
        internal protected override void Draw(SpriteBatch spriteBatch)
        {
            foreach (Entity e in Entities)
            {
                AABBComponent aabb = e.GetComponent <AABBComponent> ();
                if (!aabb.DrawDebug)
                {
                    continue;
                }
                TransformComponent transform = e.GetComponent <TransformComponent> ();

                spriteBatch.Draw
                (
                    MGame.GraphicUtilities.Rectangle,
                    new Rectangle((int)transform.Position.X + aabb.OffsetX, (int)transform.Position.Y + aabb.OffsetY, 1, aabb.Height),
                    null,
                    aabb.DebugColor,
                    0f,
                    Vector2.Zero,
                    SpriteEffects.None,
                    0.011f
                );

                spriteBatch.Draw
                (
                    MGame.GraphicUtilities.Rectangle,
                    new Rectangle((int)transform.Position.X + aabb.OffsetX, (int)transform.Position.Y + aabb.OffsetY, aabb.Width, 1),
                    null,
                    aabb.DebugColor,
                    0f,
                    Vector2.Zero,
                    SpriteEffects.None,
                    0.011f
                );

                spriteBatch.Draw
                (
                    MGame.GraphicUtilities.Rectangle,
                    new Rectangle((int)transform.Position.X + aabb.Width + aabb.OffsetX, (int)transform.Position.Y + aabb.OffsetY, 1, aabb.Height),
                    null,
                    aabb.DebugColor,
                    0f,
                    Vector2.Zero,
                    SpriteEffects.None,
                    0.011f
                );

                spriteBatch.Draw
                (
                    MGame.GraphicUtilities.Rectangle,
                    new Rectangle((int)transform.Position.X + aabb.OffsetX, (int)transform.Position.Y + aabb.Height + aabb.OffsetY, aabb.Width, 1),
                    null,
                    aabb.DebugColor,
                    0f,
                    Vector2.Zero,
                    SpriteEffects.None,
                    0.011f
                );
            }
        }
Example #2
0
    protected override void OnUpdate()
    {
        for (int index = 0; index < m_Data.Length; ++index)
        {
            m_Data.Collision[index] = new CollisionComponent
            {
                direction = float3.zero
            };

            for (int id = 0; id < m_Data.Length; ++id)
            {
                if (index == id)
                {
                    continue;
                }
                AABBComponent a = m_Data.AABB[index];
                AABBComponent b = m_Data.AABB[id];

                if (checkAABB(a, b))
                {
                    m_Data.Collision[index] = new CollisionComponent
                    {
                        direction = getCollisionDirection(a, b)
                    };
                }
            }
        }
    }
Example #3
0
        internal protected override void Update(float dt)
        {
            base.Update(dt);

            foreach (Entity e in Entities)
            {
                PhysicsBodyComponent pbody = e.GetComponent <PhysicsBodyComponent> ();
                if (pbody == null)
                {
                    continue;
                }

                TransformComponent transform = e.GetComponent <TransformComponent> ();
                AABBComponent      collider  = e.GetComponent <AABBComponent> ();

                if (pbody.Velocity.X != 0)
                {
                    transform.Position = new Vector2(transform.Position.X + pbody.Velocity.X * dt, transform.Position.Y);
                    HandleHorizontalCollision(e, transform, collider, pbody);
                }

                if (pbody.Velocity.Y != 0)
                {
                    transform.Position = new Vector2(transform.Position.X, transform.Position.Y + pbody.Velocity.Y * dt);
                    HandleVerticalCollision(e, transform, collider, pbody);
                }
            }
        }
Example #4
0
        protected override void Update(float dt)
        {
            base.Update(dt);

            PhysicsBodyComponent p = e.GetComponent <PhysicsBodyComponent> ();
            AABBComponent        c = e.GetComponent <AABBComponent> ();

            if (Input.IsKeyHeld(Keys.D))
            {
                p.Velocity.X = 100;
            }
            else if (Input.IsKeyHeld(Keys.A))
            {
                p.Velocity.X = -100;
            }
            else
            {
                p.Velocity.X = 0;
            }

            if (Input.IsKeyHeld(Keys.S))
            {
                p.Velocity.Y = 100;
            }
            else if (Input.IsKeyHeld(Keys.W))
            {
                p.Velocity.Y = -100;
            }
            else
            {
                p.Velocity.Y = 0;
            }
        }
Example #5
0
    private bool checkAABB(AABBComponent a, AABBComponent b)
    {
        bool x = Mathf.Abs(a.center[0] - b.center[0]) <= (a.halfwidths[0] + b.halfwidths[0]);
        bool y = Mathf.Abs(a.center[1] - b.center[1]) <= (a.halfwidths[1] + b.halfwidths[1]);
        bool z = Mathf.Abs(a.center[2] - b.center[2]) <= (a.halfwidths[2] + b.halfwidths[2]);

        return(x && y && z);
    }
Example #6
0
        public void Execute(int index)
        {
            var   position = data.Position[index].Value;
            float size     = data.Collider[index].size;

            AABBComponent aabb = new AABBComponent
            {
                center     = position,
                halfwidths = new float3(size, size, size)
            };

            data.AABB[index] = aabb;
        }
Example #7
0
        private void OnLoded(object sender, EventMsg msg)
        {
            _hasInited = true;
            EventGroup.DelEvent(SceneEvent.OnLoaded.ToInt(), OnLoded);
            var so        = CreateSceneObject();
            var modelComp = new ModelComponent();

            modelComp.RePath = "kachujin.prefab";
            so.AddComponent(modelComp);
            so.AddComponent <MainPlayerComponent>();
            so.AddComponent <GravityComponent>();
            so.AddComponent <StateMachine>();
            so.AddComponent <AttributeManager>();
            so.AttributeManager.SetBase(AttributeType.MaxSpeed, FixedMath.One * 2);
            so.Position = new Vector3d(FixedMath.Create(16.5f), FixedMath.One * 12, 0);
            var aabb = new AABBComponent();

            aabb.AABB = new AABB(new Vector2d(FixedMath.One * 15, FixedMath.One * 15 + FixedMath.Half), FixedMath.One, FixedMath.One);
            so.AddComponent(aabb);
        }
        public override void Process(Entity entity)
        {
            PositionComponent position = entity.GetComponent <PositionComponent>();
            SpeedComponent    speed    = entity.GetComponent <SpeedComponent>();
            AABBComponent     aabb     = entity.GetComponent <AABBComponent>();

            Vector2          new_position = speed.PreviousPosition;
            MapCollisionType type         = MoveContactMap(
                ref new_position,
                position.Position - speed.PreviousPosition,
                aabb.Rectangle,
                EntitySystem.BlackBoard.GetEntry <Map>("Map"));

            position.Position = new_position;

            EventSenderComponent eventSender = entity.GetComponent <EventSenderComponent>();

            if (eventSender != null)
            {
                eventSender.Trigger <MapCollisionEventType>(entity, new MapCollisionEventType(type));
            }
        }
        public void Execute(Entity entity, int index, [ReadOnly] ref UnitComponent unit, [ReadOnly] ref LocalToWorld c1)
        {
            var inst = CommandBuffer.Instantiate(index, unit.entity);

            CommandBuffer.SetComponent(index, inst, new Translation
            {
                Value = new float3(0, 0, 0)
            });

            var aabb = new AABBComponent
            {
                aabb = new Unity.Physics.Aabb
                {
                    Max = unit.boundsMax,
                    Min = unit.boundsMin
                }
            };

            CommandBuffer.AddComponent(index, inst, aabb);

            CommandBuffer.DestroyEntity(index, entity);
        }
Example #10
0
    private float3 getCollisionDirection(AABBComponent a, AABBComponent b)
    {
        float3 direction = float3.zero;

        if (a.center.y > b.center.y)
        {
            return(new float3(0, -1, 0));
        }
        else if (a.center.y < b.center.y)
        {
            return(new float3(0, 1, 0));
        }
        else if (a.center.x > b.center.x)
        {
            return(new float3(-1, 0, 0));
        }
        else if (a.center.x < b.center.x)
        {
            return(new float3(1, 0, 0));
        }

        return(direction);
    }
Example #11
0
        private void HandleVerticalCollision(Entity e, TransformComponent t, AABBComponent c, PhysicsBodyComponent p)
        {
            foreach (Entity o in Entities)
            {
                if (e == o)
                {
                    continue;
                }

                TransformComponent ot = o.GetComponent <TransformComponent> ();
                AABBComponent      oc = o.GetComponent <AABBComponent> ();

                Rectangle r  = new Rectangle((int)t.Position.X + c.OffsetX, (int)t.Position.Y + c.OffsetY, c.Width, c.Height);
                Rectangle or = new Rectangle((int)ot.Position.X + oc.OffsetX, (int)ot.Position.Y + oc.OffsetY, oc.Width, oc.Height);

                if (r.Intersects(or))
                {
                    float d = r.GetIntersectionDepth(or).Y * -1;

                    t.Position = new Vector2(t.Position.X, (float)Math.Floor(t.Position.Y - d));
                }
            }
        }