public override bool CanMove(Vector3 direction) { if (Frozen) { return(false); } Vector3 pos = my.position; RaycastHit hit; if (!EatenA && Physics.Raycast(pos, direction, out hit, 1, layerMask)) { MovableEntity movable = hit.transform.GetComponent <MovableEntity>(); if (!(movable && movable.CanMove(direction))) { return(false); } movable.Push(direction); } if (!EatenB && Physics.Raycast(pos + my.forward, direction, out hit, 1, layerMask)) { MovableEntity movable = hit.transform.GetComponent <MovableEntity>(); if (!(movable && movable.CanMove(direction))) { return(false); } movable.Push(direction); } return(true); }
bool CollideWithMovableObject(MovableEntity movable, Vector3 direction) { if (movable.CanMove(direction)) { movable.Push(direction); } else if (SameDirection(direction, my.forward) && !Eating && movable.CanBeEaten(direction) && !ShouldRiseOnEars()) { StartEating(movable, direction); } else { return(false); // blocked by wall } return(true); }
public virtual bool CanMove(Vector3 direction) { Vector3 pos = my.position; RaycastHit hit; if (Physics.Raycast(pos, direction, out hit, 1, layerMask)) { MovableEntity movable = hit.transform.GetComponent <MovableEntity>(); if (movable) { movable.Push(direction); return(movable.CanMove(direction)); } else { return(false); } } return(true); }
public override bool EndMove(Vector3 direction) { Vector3 pos = my.position; Suspended = false; RaycastHit hit; bool onGround = Physics.Raycast(pos, -yAxis, out hit, 1, layerMask); if (StandingUp() && onGround) { LevelEntrance entry = hit.transform.GetComponent <LevelEntrance>(); if (entry) { entry.EnterLevel(); } } if (SameDirection(my.forward, -yAxis)) { if (Eating) { JustAte = true; eatParticles.Play(); currentlyEating.Eat(pos - yAxis); } else if (onGround) { MovableEntity movable = hit.transform.GetComponent <MovableEntity>(); if (movable) { if (CollideWithMovableObject(movable, -yAxis) && Eating) { HandleFalling(pos); } } } } if (!onGround) { // there's a hole OnEars = UpsideDown() && Physics.Raycast(pos, -yAxis, 2, layerMask); if (OnEars) { return(true); // on tient sur les oreilles tout va bien } if (Eating && !Colinear(my.forward, yAxis) && Physics.Raycast(pos + my.forward, -yAxis, 1, layerMask)) { if (Physics.Raycast(pos + my.forward, yAxis, 1, layerMask)) { Suspended = true; return(true);// a ceiling holds the carrot, wer'e suspended } currentlyEating.transform.parent = my; Vector3 axis = new Vector3(-my.forward.z, 0, my.forward.x); Quaternion initialRotation = my.rotation; Quaternion finalRotation = Quaternion.AngleAxis(90, axis) * initialRotation; finalRotation = RoundRotation(finalRotation); ChangeRotation(initialRotation, finalRotation, timeToMove, -my.forward); } HandleFalling(pos); return(false); } if (StandingUp() && Physics.Raycast(pos, yAxis, out hit, 1, layerMask)) { MovableEntity movable = hit.transform.GetComponent <MovableEntity>(); if (movable) { if (movable.CanMove(direction)) { movable.Push(direction); } else if (movable.CanMove(yAxis)) { movable.Push(yAxis); } return(false); } } if (UpsideDown() && !OnEars && !Eating) { OnHead = true; ExecuteMove(direction); } else { OnEars = OnHead = false; if (Physics.Raycast(pos, my.up, out hit, 1, layerMask)) { //mes oreilles sont dans un mur MovableEntity movable = hit.transform.GetComponent <MovableEntity>(); if (movable && movable.CanMove(my.up)) { movable.Push(my.up); } else { if (Physics.Raycast(my.position, -my.up, out hit, 1, layerMask)) { movable = hit.transform.GetComponent <MovableEntity>(); if (movable && movable.CanMove(-my.up)) { movable.Push(-my.up); // should check if not eating first (carrot could be stuck under ceiling) //carrying = movable; } else { return(true); } } else if (Eating && Physics.Raycast(my.position + my.forward, -my.up, out hit, 1, layerMask)) { movable = hit.transform.GetComponent <MovableEntity>(); if (movable && movable.CanMove(-my.up)) { movable.Push(-my.up); //carrying = movable; } else { return(true); } } ChangePosition(pos, RoundPosition(pos - my.up), timeToMove, -my.up); return(false); } } } return(true); }
void ExecuteMove(Vector3 direction) { Vector3 initialPosition = my.position; Vector3 finalPosition = RoundPosition(initialPosition + direction); JustAte = false; bool readyToEat = Eating && direction == my.forward; if (Suspended && readyToEat) { JustAte = true; eatParticles.Play(); currentlyEating.Eat(finalPosition); } else { RaycastHit hit; if (Physics.Linecast(initialPosition, readyToEat ? finalPosition + direction : finalPosition, out hit, layerMask)) { if (!HandleCollision(hit.transform, direction)) { // if stuck on your head, your ears push you up if (ShouldRiseOnEars()) { if (Physics.Raycast(my.position, yAxis, out hit, 1, layerMask)) { MovableEntity movable = hit.transform.GetComponent <MovableEntity>(); print("movable above: " + movable); if (movable && movable.CanMove(yAxis)) { movable.Push(yAxis); // should check if not eating first (carrot could be stuck under ceiling) } else { return; } } else if (Eating && Physics.Raycast(my.position + my.forward, yAxis, out hit, 1, layerMask)) { MovableEntity movable = hit.transform.GetComponent <MovableEntity>(); if (movable && movable.CanMove(yAxis)) { movable.Push(yAxis); } else { return; } } OnEars = true; OnHead = false; ChangePosition(my.position, my.position + yAxis, timeToFall, yAxis); } if (readyToEat) { JustAte = true; eatParticles.Play(); currentlyEating.Eat(finalPosition); } else { return; // there's a wall blocking us } } } else if (Eating && !StandingUp()) { if (SameDirection(direction, my.forward) && Physics.Raycast(initialPosition + my.forward, -yAxis, 1, layerMask)) { // check if movable, if movable, push it return; // tu peux pas tourner t'as une carotte dans la bouche } else if (SameDirection(yAxis, my.forward) && Physics.Linecast(initialPosition, finalPosition + direction, out hit, layerMask)) { print("bump"); return; // do rotation, bump in wall then come back } } if (OnTheSides()) // les oreilles se plient pas sur le côté { if (SameDirection(my.up, direction) && Physics.Raycast(finalPosition, -yAxis, 1, layerMask)) { return; } else if (SameDirection(-my.up, direction) && Physics.Raycast(my.position + my.up, yAxis, out hit, 1, layerMask)) { MovableEntity movable = hit.transform.GetComponent <MovableEntity>(); if (movable && movable.CanMove(direction)) { // this wall check is gonna be annoying movable.Push(direction * 3); } else { return; } } } } if (Suspended && direction == -my.forward) { currentlyEating.StopEating(); StopEating(); EndMove(0 * zAxis); } else { ChangePosition(initialPosition, finalPosition, timeToMove, direction); } if (!Eating || (!StandingUp() && !JustAte)) { if (Eating) { currentlyEating.transform.parent = my; } Vector3 axis = new Vector3(direction.z, 0, -direction.x); Quaternion initialRotation = my.rotation; Quaternion finalRotation = Quaternion.AngleAxis(90, axis) * initialRotation; finalRotation = RoundRotation(finalRotation); ChangeRotation(initialRotation, finalRotation, timeToMove, direction); } if (Eating && !JustAte && currentlyEating.transform.parent != my) { currentlyEating.Push(direction); } if (currentlyEating && currentlyEating.FullyEaten) { StopEating(); } }