// Raycasting Variables

    private void Awake()
    {
        rb       = GetComponent <Rigidbody2D>();
        state    = EntityMotionState.AIR;
        rcModule = new RaycastModule(this);
        ChangeForm(formIndex);
    }
Esempio n. 2
0
        internal static EngineCommandResult InitEntity(Entity entity)
        {
            try
            {
                var physicsComponent = entity.Get <PhysicsComponent>(ComponentType.Physics);
                if (physicsComponent == null)
                {
                    return(EngineCommandResult.Success);
                }

                var isStatic = physicsComponent.Physics == PhysicsType.Static;

                var collisionShape = isStatic
                    ? (CollisionShape)  new BvhTriangleMeshShape(physicsComponent.Mesh.Convert(), false)
                    : (CollisionShape)  new ConvexTriangleMeshShape(physicsComponent.Mesh.Convert());

                Engine.I.CollisionShapes.Add(collisionShape);

                Vector3 inertia;
                collisionShape.CalculateLocalInertia(1.0f, out inertia);

                var motionState   = new EntityMotionState(entity, physicsComponent.Motion == MotionType.Kinematic);
                var rigidBodyInfo = new RigidBodyConstructionInfo(isStatic ? 0.0f : 1.0f, motionState, collisionShape, inertia);
                var rigidBody     = new RigidBody(rigidBodyInfo)
                {
                    Friction = 1.0f,
                    Gravity  = isStatic
                        ?               Vector3.Zero
                        :               new Vector3(0.0f, -9.8f, 0.0f),
                    CollisionFlags = isStatic
                        ?               CollisionFlags.StaticObject
                        :               physicsComponent.Motion == MotionType.Dynamic
                            ?               CollisionFlags.None
                            :               CollisionFlags.KinematicObject,
                    ActivationState = physicsComponent.Motion == MotionType.Dynamic
                        ?               ActivationState.ActiveTag
                        :               ActivationState.DisableDeactivation
                };

                Engine.I.World.AddRigidBody(rigidBody);

                if (!isStatic && !Engine.I.Objects.ContainsKey(entity))
                {
                    Engine.I.Objects.Add(entity, rigidBody);
                }
            }
            catch (Exception e)
            {
                return(EngineCommandResult.Failed);
            }

            return(EngineCommandResult.Success);
        }
 private void CheckForLanding(Vector2 incomingVelocity)
 {
     // If we had negative velocity before colliding with said slope.
     if (incomingVelocity.y < 0f)
     {
         externalVelocity.y = 0f;
         if (state != EntityMotionState.GROUNDED)
         {
             state = EntityMotionState.GROUNDED;
             OnLanding();
         }
     }
 }
    /*
     * incomingVelocity may or not be equal to currentVelocity.
     * When a surface is hit, it could be redirected along that surface.
     */
    public void OnContactWithSurface(RaycastHit2D surfaceHit, Vector2 incomingVelocity, float surfaceNormSide)
    {
        float slopeNormal2OurNormal = Vector2.Angle(surfaceHit.normal, Vector2.up);

        // If the angle of the surface hit is less than our climbing angle (we can stand on it), enter GroundedState.
        if (Mathf.Abs(slopeNormal2OurNormal) < maxClimbAngle)
        {
            CheckForLanding(incomingVelocity);
        }
        // If the slope is too great and we are trying to go up it.
        else
        {
            if (surfaceNormSide != 0f)
            {
                state = EntityMotionState.AIR;
            }
        }
    }
    public void Update()
    {
        // Translate the entity in the direction calculated last frame.
        transform.Translate(forNextFrame);

        // First check which way we are facing based on DI.
        if (directionalInfluence.x > 0)
        {
            facingRight = true;
        }
        else if (directionalInfluence.x < 0)
        {
            facingRight = false;
        }

        switch (state)
        {
        case EntityMotionState.AIR:
            WhileInAir();
            break;

        case EntityMotionState.GROUNDED:
            if (!rcModule.CheckGround())
            {
                state = EntityMotionState.AIR;
            }
            break;

        default:
            break;
        }

        // First calculate the final velocity of the entity.
        currentVelocity = CompoundVelocities(vOverride, vAdd, vMult);

        // Do our Raycast Movement logic.
        forNextFrame = rcModule.RaycastMovement(currentVelocity * Time.deltaTime);

        Physics2D.SyncTransforms();

        DecayExternalVelocity();
    }
 private void WhileInAir()
 {
     state       = EntityMotionState.AIR;
     subAirTime += Time.deltaTime;
     sbaWhileInAir?.Invoke();
 }
 private void OnLanding()
 {
     state = EntityMotionState.GROUNDED;
     sbaOnLanding?.Invoke();
     ResetAirTime();
 }