using UnityEngine;
using System.Collections;

public class WallController : MonoBehaviour {

    public Transform tracerReferencePoint;
    public Collider tracerCollider;
    public WallSegment wallPrefab;

    // Referenz auf unser Objekt "Walls" unter "Player"
    public Transform wallsParent;

    private Material wallsMaterial;
    private TracerColor tracerColor;

    private WallSegment currentWall = null;

    // ein einfacher Zähler für unsere Wände
    private int wallCounter = 0;

    public void Awake() {
        Material sharedMat = wallPrefab.wallRenderer.sharedMaterial;
        wallsMaterial = new Material(sharedMat);

        tracerColor = GetComponent<TracerColor>();
    }

    public void TracerTurned() {

        if (currentWall != null) {
            currentWall.StopGrowing();
            Physics.IgnoreCollision(tracerCollider,
                        currentWall.wallCollider,
                        false);
        }

        currentWall = (WallSegment)Instantiate(
                              wallPrefab,
                              tracerReferencePoint.position,
                              tracerReferencePoint.rotation);

        Physics.IgnoreCollision(tracerCollider,
                                currentWall.wallCollider);

        currentWall.TracerReferencePoint = tracerReferencePoint;

        // das neue Objekt unter "Walls" hängen:
        currentWall.transform.parent = wallsParent;

        // das neue Objekte benennen: WallSegment.000, WallSegment.001 usw.
        currentWall.name = string.Format("WallSegment.{0:000}", wallCounter++);

        wallsMaterial.SetColor("_Color", tracerColor.playerColor);
        wallsMaterial.SetColor("_EmissionColor",
                               tracerColor.playerColor);
        currentWall.wallRenderer.material = wallsMaterial;

    }

}
