void Update() { if (!grid) { return; } if (allowMove) { if (doMove) { //move towards the desination Vector3 newPosition = transform.position; newPosition.x = Mathf.MoveTowards(transform.position.x, goal.x, roamingSpeed * Time.deltaTime); newPosition.y = Mathf.MoveTowards(transform.position.y, goal.y, roamingSpeed * Time.deltaTime); transform.position = newPosition; //check if we reached the destination (use a certain tolerance so we don't miss the point becase of rounding errors) if (Mathf.Abs(transform.position.x - goal.x) < 0.01f && Mathf.Abs(transform.position.y - goal.y) < 0.01f) { doMove = false; } else { GetComponent <Hero> ().myDistance++; } //if we did stop moving // Set animation og rotation så det passer med bevægelsen animator.SetBool("Run", doMove); transform.rotation = Quaternion.Euler(rotation); } else { //make sure the time is always positive if (roamingTime < 0.01f) { roamingTime = 0.01f; } //find the next destination goal = FindNextFace(); //--- let's check if the goal is allowed, if not we will pick another direction during the next frame --- if (ForbiddenTilesVores.CheckSquare(goal).Equals('0')) { //calculate speed by dividing distance (one of the two distances will be 0, we need the other one) through time roamingSpeed = Mathf.Max(Mathf.Abs(transform.position.x - goal.x), Mathf.Abs(transform.position.y - goal.y)) / roamingTime; //resume movement with the new goal doMove = true; } else { //Debug.Log ("hit the obstacle"); } } } }
// Start() is called after Awake(), this ensures that the matrix has alrady been built void Start() { /* Set the entry that corresonds to the obstacle's position as * '1' brick * '2' reinforcedBrick * 'X' steel */ if (name.Contains("Reinforced")) { ForbiddenTilesVores.RegisterSquare(transform.position, '2'); } else if (name.Contains("Brick")) { ForbiddenTilesVores.RegisterSquare(transform.position, '1'); } else if (name.Contains("Steel")) { ForbiddenTilesVores.RegisterSquare(transform.position, 'x'); } }
/** Kigger på hvad flammen rammer. */ void OnTriggerEnter(Collider other) { if (other.name.Contains("BrickBlock")) { //Spawn pickUp, 1 ud af 3 gange. if (Random.Range(0, 3) == 1) { GameObject pickup = other.GetComponent <Spawner> ().SpawnElement( other.transform.position, new Vector3(270, 0, 0), Random.Range(0, 3)); // Hvis det er en sygdom ændrer vi navnet så det kan bruges. if (pickup.name.Contains("Sickness")) { SicknessSpawn(pickup); } } //Updater Grid DestroyObject(other.gameObject); ForbiddenTilesVores.RegisterSquare(other.transform.position, '0'); } else if (other.name.Contains("Player")) { SaveData(other); // Stopper den ramte spiller, så denne ikke længere kan bevæge sig. MovePlayer mover = other.GetComponent <MovePlayer> (); mover.doMove = false; mover.allowMove = false; // Sætter den ramte spillers animation til at denne har tabt. Animator playerAni = other.GetComponent <Animator> (); playerAni.SetBool("Run", false); playerAni.SetBool("Win", false); playerAni.SetTrigger("GameEnd"); } }
/** Flammer spawnes */ public void blastHallWithFire() { Spawner sp = gameObject.GetComponent <Spawner> (); Vector3 bombPos = transform.position; GameObject senesteFlamme; // Retninger Vector3 opVecDec = new Vector3(270, 0, 0); Vector3 nedVecDec = new Vector3(90, 180, 0); Vector3 hoejreVecDec = new Vector3(0, 90, 270); Vector3 venstreVecDec = new Vector3(0, 270, 90); // Center senesteFlamme = sp.SpawnElement(bombPos, opVecDec); senesteFlamme.GetComponent <Exploded> ().placer = placer; // Mid & End bool up = true; bool down = true; bool left = true; bool right = true; for (float i = 0; i <= range; i++) { /* Hvis enden er nået sættes element til at være flammeEnd * ellers sættes den til at være flammeMid */ int element = i < range ? 1 : 2; if (up) { senesteFlamme = sp.SpawnElement(bombPos + new Vector3(0, i, 0), opVecDec, element); senesteFlamme.GetComponent <Exploded> ().placer = placer; char ramte = ForbiddenTilesVores.CheckSquare(senesteFlamme.transform.position + new Vector3(0, 1, 0)); up = setFalseAndDestroy(senesteFlamme, ramte, up); } if (down) { senesteFlamme = sp.SpawnElement(bombPos + new Vector3(0, -i, 0), nedVecDec, element); senesteFlamme.GetComponent <Exploded> ().placer = placer; char ramte = ForbiddenTilesVores.CheckSquare(senesteFlamme.transform.position + new Vector3(0, -1, 0)); down = setFalseAndDestroy(senesteFlamme, ramte, down); } if (right) { senesteFlamme = sp.SpawnElement(bombPos + new Vector3(i, 0, 0), hoejreVecDec, element); senesteFlamme.GetComponent <Exploded> ().placer = placer; char ramte = ForbiddenTilesVores.CheckSquare(senesteFlamme.transform.position + new Vector3(1, 0, 0)); right = setFalseAndDestroy(senesteFlamme, ramte, right); } if (left) { senesteFlamme = sp.SpawnElement(bombPos + new Vector3(-i, 0, 0), venstreVecDec, element); senesteFlamme.GetComponent <Exploded> ().placer = placer; char ramte = ForbiddenTilesVores.CheckSquare(senesteFlamme.transform.position + new Vector3(-1, 0, 0)); left = setFalseAndDestroy(senesteFlamme, ramte, left); } } }
// Awake is called before Start() void Awake() { //We will build the matrix based on the grid that is attached to this object. //All entries are true by default, then each obstacle will mark its entry as false ForbiddenTilesVores.Initialize(GetComponent <GFRectGrid> ()); }
void OnGUI() { GUI.TextArea(new Rect(10, 10, 140, 230), ForbiddenTilesVores.MatrixToString()); }