<commit_before>﻿using System;
using System.Collections.Generic;
using Engine.cgimin.material.normalmapping;
using Engine.cgimin.object3d;
using Engine.cgimin.texture;
using OpenTK;

namespace Mugo
{
    public class TunnelSegment : BaseObject3D
	{
	    private readonly Dictionary<int, ITunnelSegementElementModel> elements;
	    public const float Width = 6;
        public const int RailCount = 3;
		public const int Height = 3;
		public const int Depth = 10;
		public const float RailWidth = Width / RailCount;

		public TunnelSegment()
        {
            elements = new Dictionary<int, ITunnelSegementElementModel>(RailCount);
			Textures = TextureLoader.Load("data/textures/textures.jpg");
            Material = new NormalMappingMaterial();

			//left
			addTriangle(
				new Vector3(0, Height, -Depth), new Vector3(0, Height, 0), new Vector3(0, 0, 0), 
				Vector3.One, Vector3.One, Vector3.One,
				new Vector2(0.5f, 0.4f), new Vector2(0, 0.4f), new Vector2(0f, 0.7f));

			addTriangle(
				new Vector3(0, 0, 0), new Vector3(0, 0, -Depth), new Vector3(0, Height, -Depth), 
				Vector3.One, Vector3.One, Vector3.One,
				new Vector2(0f, 0.7f), new Vector2(0.5f, 0.7f), new Vector2(0.5f, 0.4f));

			//right
			addTriangle(
				new Vector3(Width, 0, 0), new Vector3(Width, Height, 0), new Vector3(Width, Height, -Depth), 
				Vector3.One, Vector3.One, Vector3.One,
				new Vector2(0f, 0.7f), new Vector2(0f, 0.4f), new Vector2(0.5f, 0.4f));

			addTriangle(
				new Vector3(Width, Height, -Depth), new Vector3(Width, 0, -Depth), new Vector3(Width, 0, 0), 
				Vector3.One, Vector3.One, Vector3.One,
				new Vector2(0.5f, 0.4f), new Vector2(0.5f, 0.7f), new Vector2(0f, 0.7f));

            //bottom
            for (int i = 0; i < RailCount; i++)
            {
                addTriangle(
                new Vector3(RailWidth + (RailWidth * i), 0, 0), new Vector3(RailWidth * i, 0, -Depth), new Vector3(RailWidth * i, 0, 0),
                Vector3.One, Vector3.One, Vector3.One,
                new Vector2(0.3f, 0.4f), new Vector2(0, 0), new Vector2(0, 0.4f));

                addTriangle(
                    new Vector3(RailWidth + (RailWidth * i), 0, 0), new Vector3(RailWidth + (RailWidth * i), 0, -Depth), new Vector3(RailWidth * i, 0, -Depth),
                    Vector3.One, Vector3.One, Vector3.One,
                    new Vector2(0.3f, 0.4f), new Vector2(0.3f, 0f), new Vector2(0f, 0f));
            }
			

			//top
			addTriangle(
				new Vector3(0, Height, 0), new Vector3(0, Height, -Depth), new Vector3(Width, Height, 0), 
				Vector3.One, Vector3.One, Vector3.One,
				new Vector2(0f, 0.4f), new Vector2(0.5f, 0.4f), new Vector2(0f, 0.7f));

			addTriangle(
				new Vector3(0, Height, -Depth), new Vector3(Width, Height, -Depth), new Vector3(Width, Height, 0), 
				Vector3.One, Vector3.One, Vector3.One,
				new Vector2(0.5f, 0.4f), new Vector2(0.5f, 0.7f), new Vector2(0f, 0.7f));

            averageTangents();

            CreateVAO();
        }

		public TextureHolder Textures
		{
			get;
			set;
		}

	    public NormalMappingMaterial Material
	    {
	        get;
            set;
        }

	    public IReadOnlyDictionary<int, ITunnelSegementElementModel> Elements => elements;

	    public void SetElementAtPosition(int index, ITunnelSegementElementModel element)
	    {
	        if (index < 0 || index >= RailCount)
                throw new ArgumentOutOfRangeException(nameof(index));
			
			element.Transformation *= Matrix4.CreateTranslation(RailWidth * index + (RailWidth / 2), 0f, 0f);

	        elements[index] = element;
	    }

	    public void Draw()
	    {
	        Material.Draw(this, Textures.TextureId, Textures.NormalMapId, 1f);

	        foreach (var element in elements.Values)
	        {
	            element.Draw();
	        }
	    }

	    public override void UnLoad()
	    {
            foreach (var element in elements.Values)
            {
                element.UnLoad();
            }

            base.UnLoad();
        }
    }<commit_msg>Unload previous tunnel elements when replaced.<commit_after>﻿using System;
using System.Collections.Generic;
using Engine.cgimin.material.normalmapping;
using Engine.cgimin.object3d;
using Engine.cgimin.texture;
using OpenTK;

namespace Mugo
{
    public class TunnelSegment : BaseObject3D
	{
	    private readonly Dictionary<int, ITunnelSegementElementModel> elements;
	    public const float Width = 6;
        public const int RailCount = 3;
		public const int Height = 3;
		public const int Depth = 10;
		public const float RailWidth = Width / RailCount;

		public TunnelSegment()
        {
            elements = new Dictionary<int, ITunnelSegementElementModel>(RailCount);
			Textures = TextureLoader.Load("data/textures/textures.jpg");
            Material = new NormalMappingMaterial();

			//left
			addTriangle(
				new Vector3(0, Height, -Depth), new Vector3(0, Height, 0), new Vector3(0, 0, 0), 
				Vector3.One, Vector3.One, Vector3.One,
				new Vector2(0.5f, 0.4f), new Vector2(0, 0.4f), new Vector2(0f, 0.7f));

			addTriangle(
				new Vector3(0, 0, 0), new Vector3(0, 0, -Depth), new Vector3(0, Height, -Depth), 
				Vector3.One, Vector3.One, Vector3.One,
				new Vector2(0f, 0.7f), new Vector2(0.5f, 0.7f), new Vector2(0.5f, 0.4f));

			//right
			addTriangle(
				new Vector3(Width, 0, 0), new Vector3(Width, Height, 0), new Vector3(Width, Height, -Depth), 
				Vector3.One, Vector3.One, Vector3.One,
				new Vector2(0f, 0.7f), new Vector2(0f, 0.4f), new Vector2(0.5f, 0.4f));

			addTriangle(
				new Vector3(Width, Height, -Depth), new Vector3(Width, 0, -Depth), new Vector3(Width, 0, 0), 
				Vector3.One, Vector3.One, Vector3.One,
				new Vector2(0.5f, 0.4f), new Vector2(0.5f, 0.7f), new Vector2(0f, 0.7f));

            //bottom
            for (int i = 0; i < RailCount; i++)
            {
                addTriangle(
                new Vector3(RailWidth + (RailWidth * i), 0, 0), new Vector3(RailWidth * i, 0, -Depth), new Vector3(RailWidth * i, 0, 0),
                Vector3.One, Vector3.One, Vector3.One,
                new Vector2(0.3f, 0.4f), new Vector2(0, 0), new Vector2(0, 0.4f));

                addTriangle(
                    new Vector3(RailWidth + (RailWidth * i), 0, 0), new Vector3(RailWidth + (RailWidth * i), 0, -Depth), new Vector3(RailWidth * i, 0, -Depth),
                    Vector3.One, Vector3.One, Vector3.One,
                    new Vector2(0.3f, 0.4f), new Vector2(0.3f, 0f), new Vector2(0f, 0f));
            }
			

			//top
			addTriangle(
				new Vector3(0, Height, 0), new Vector3(0, Height, -Depth), new Vector3(Width, Height, 0), 
				Vector3.One, Vector3.One, Vector3.One,
				new Vector2(0f, 0.4f), new Vector2(0.5f, 0.4f), new Vector2(0f, 0.7f));

			addTriangle(
				new Vector3(0, Height, -Depth), new Vector3(Width, Height, -Depth), new Vector3(Width, Height, 0), 
				Vector3.One, Vector3.One, Vector3.One,
				new Vector2(0.5f, 0.4f), new Vector2(0.5f, 0.7f), new Vector2(0f, 0.7f));

            averageTangents();

            CreateVAO();
        }

		public TextureHolder Textures
		{
			get;
			set;
		}

	    public NormalMappingMaterial Material
	    {
	        get;
            set;
        }

	    public IReadOnlyDictionary<int, ITunnelSegementElementModel> Elements => elements;

	    public void SetElementAtPosition(int index, ITunnelSegementElementModel element)
	    {
	        if (index < 0 || index >= RailCount)
                throw new ArgumentOutOfRangeException(nameof(index));

			ITunnelSegementElementModel previousElement;

			if(elements.TryGetValue(index, out previousElement) && previousElement != null)
			{
				previousElement.UnLoad();
			}

			if (element != null)
			{
				element.Transformation *= Matrix4.CreateTranslation(RailWidth * index + (RailWidth / 2), 0f, 0f);
			}

	        elements[index] = element;
	    }

	    public void Draw()
	    {
	        Material.Draw(this, Textures.TextureId, Textures.NormalMapId, 1f);

	        foreach (var element in elements.Values)
	        {
	            element.Draw();
	        }
	    }

	    public override void UnLoad()
	    {
            foreach (var element in elements.Values)
            {
                element.UnLoad();
            }

            base.UnLoad();
        }
    }
{'fraction_non_alphanumeric': 0.12769566270898958, 'fraction_numerical': 0.0484613520717228, 'mean_word_length': 2.439583333333333, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1318724', 'n_tokens_mistral': 3219, 'n_tokens_neox': 2735, 'n_words': 704}