Advertisement
Guest User

Untitled

a guest
Aug 4th, 2015
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.21 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class LightSource{
  6.     public int x,y,intensity,droprate,droprateblock;
  7.     public LightSource(int x, int y, int intensity, int droprate,int droprateblock)
  8.     {
  9.         this.x=x;this.y=y;this.intensity=intensity;this.droprate=droprate;this.droprateblock=droprateblock;
  10.     }
  11.     public virtual void ApplyLight(List<List<Block>> positive, List<List<Block>> negative, int lun,int min, int max)
  12.     {
  13.     }
  14. }
  15. public class SpotLight : LightSource{
  16.     public SpotLight(int x , int y ,int intensity, int droprate) : base(x,y,intensity,droprate,droprate)
  17.     {
  18.     }
  19.     public override void ApplyLight(List<List<Block>> positive, List<List<Block>> negative, int lun,int min, int max)
  20.     {
  21.         this.SpotApplyLight(x,y,intensity,positive,negative,lun,min,max);
  22.     }
  23.     private void SpotApplyLight(int x,int y, int intensity, List<List<Block>> positive, List<List<Block>> negative, int lun,int min, int max)
  24.     {
  25.         if(x<min || x>max || y>=Consts.MAXHEIGHT || y<=0)
  26.             return;
  27.         if(intensity<=0)
  28.             return;
  29.         int nx = x;
  30.         List<List<Block>> target = positive;
  31.         if(x<0)
  32.         {
  33.             target=negative;
  34.             nx=-1-x;
  35.         }
  36.         int nextIntensity=intensity-droprate;
  37.         if(target[nx][y] != null)
  38.         {
  39.             /*if(target[nx][y].lastLightUpdate == lun)
  40.                 return;
  41.             target[nx][y].lastLightUpdate = lun;*/
  42.             nextIntensity=intensity-droprateblock;
  43.             target[nx][y].OnScreenObject.GetComponent<Lighting>().SetLight(intensity);
  44.         }
  45.         this.SpotApplyLight(x,y-1,nextIntensity,positive,negative,lun,min,max);
  46.         this.SpotApplyLight(x,y+1,nextIntensity,positive,negative,lun,min,max);
  47.         this.SpotApplyLight(x-1,y,nextIntensity,positive,negative,lun,min,max);
  48.         this.SpotApplyLight(x+1,y,nextIntensity,positive,negative,lun,min,max);
  49.     }
  50. }
  51.  
  52.  
  53.  
  54. public class SunLight : LightSource
  55. {
  56.     public SunLight(int x):base(x,Consts.MAXHEIGHT-1,100,0,20)
  57.     {
  58.  
  59.     }
  60.     public override void ApplyLight(List<List<Block>> positive, List<List<Block>> negative, int lun,int min, int max)
  61.     {
  62.         this.SunApplyLight(x,y,intensity,positive,negative,lun,min,max);
  63.     }
  64.     private void SunApplyLight(int x,int y, int intensity,List<List<Block>> positive, List<List<Block>> negative, int lun,int min, int max)
  65.     {
  66.         if(x<min || x>max || y>=Consts.MAXHEIGHT || y<=0)
  67.             return;
  68.         if(intensity<=0)
  69.             return;
  70.         int nx = x;
  71.         List<List<Block>> target = positive;
  72.         if(x<0)
  73.         {
  74.             target=negative;
  75.             nx=-1-x;
  76.         }
  77.         int nextIntensity=intensity-droprate;
  78.         if(target[nx][y] != null)
  79.         {
  80.             if(target[nx][y].lastLightUpdate == lun)
  81.                 return;
  82.             target[nx][y].lastLightUpdate = lun;
  83.             nextIntensity=intensity-droprateblock;
  84.             target[nx][y].OnScreenObject.GetComponent<Lighting>().SetLight(intensity);
  85.         }
  86.         this.SunApplyLight(x,y-1,nextIntensity,positive,negative,lun,min,max);
  87.         if(x>min+1)
  88.             if(x<0&& negative[-x+-1][y]!=null)
  89.                 this.SunApplyLight(x-1,y,nextIntensity,positive,negative,lun,min,max);
  90.         if(x<max-1)
  91.             if(x>0&&positive[x][y]!=null)
  92.                 this.SunApplyLight(x-1,y,nextIntensity,positive,negative,lun,min,max);
  93.     }
  94. }
  95. public class Consts{
  96.     public const int WORLDSIZE = 2048;
  97.     public const int WORLDHEIGHT = 64;
  98.     public const int MAXHEIGHT = 128;
  99.     public const int MINLANDSCAPE = 56;
  100.     public const int MAXLANDSCAPE = 72;
  101.     public const int BLOCKID = 100;
  102.     public const int INIT_MIN = -8;
  103.     public const int INIT_MAX = 8;
  104.     public const int EXPANSION_RATE = 64;
  105.     public const int REACH_RADIUS = 3;
  106.     public const int DIST = 5;
  107.     public static int SEED = 555;
  108.     public const int VISION = 8;
  109.     GameObject initializer;
  110.     static Sprite[] blockSprites;
  111.     static Sprite[] blockIconSprites;
  112.     public static void Start()
  113.     {
  114.         Consts.SEED = Random.Range(-100,100);
  115.         Texture2D temp;
  116.         blockSprites = new Sprite[4];
  117.         for (int i = 0; i< blockSprites.Length; i++) {
  118.             temp = (Texture2D)Resources.Load ("Blocks/"+i);
  119.             blockSprites[i]=Sprite.Create(temp,new Rect(0,0,temp.width,temp.height),new Vector2(0.5f,0.05f),100.0f);
  120.         }
  121.         blockIconSprites = new Sprite[4];
  122.         for (int i = 0; i< blockIconSprites.Length; i++) {
  123.             temp = (Texture2D)Resources.Load ("BlocksIcons/"+i);
  124.             blockIconSprites[i]=Sprite.Create(temp,new Rect(0,0,temp.width,temp.height),new Vector2(0.5f,0.05f),100.0f);
  125.         }
  126.     }
  127.     public static Sprite GetBlockSprite(int id)
  128.     {
  129.         return blockSprites [id];
  130.     }
  131.     public static Sprite GetBlockIconSprite(int id)
  132.     {
  133.         return blockIconSprites [id];
  134.     }
  135. }
  136. public class WorldInitialize : MonoBehaviour
  137. {
  138.     public GameObject blockPrefab;
  139.     public List<List<Block>> positive;
  140.     public List<List<Block>> negative;
  141.     public List<LightSource> lights;
  142.     private int min, max;
  143.     private Vector3 initialPosition;
  144.     private int lightUpdateNumber= 100;
  145.     void Start()
  146.     {
  147.         Consts.Start ();
  148.        
  149.         lights = new List<LightSource>();
  150.         positive = new List<List<Block>>();
  151.         negative = new List<List<Block>>();
  152.         min = Consts.INIT_MIN;
  153.         max= Consts.INIT_MAX;
  154.         Expand(min,max);
  155.         CheckLighting(min,max);
  156.     }
  157.     private void Expand(int newMin, int newMax)
  158.     {
  159.         if(newMin < this.min)
  160.             this.min = newMin;
  161.         if(newMax > this.max)
  162.             this.max = newMax;
  163.         for(int i = newMin; i <= newMax; i++)
  164.         {
  165.             lights.Add (new SunLight(i));
  166.         }
  167.         Terrain terrain = new Terrain(positive,negative,newMin,newMax,blockPrefab);
  168.         Render(newMin,newMax);
  169.     }
  170.     public void Render(int min, int max)
  171.     {
  172.         List<List<Block>> target; int ni;
  173.         for(int i = min; i <= max; i++)
  174.         {
  175.             ni = i;
  176.             target = positive;
  177.             if(i<0)
  178.             {
  179.                 ni = -i-1;
  180.                 target = negative;
  181.             }
  182.             foreach(Block block in target[ni])
  183.                 if(block!=null)
  184.                     block.Render(transform.position);
  185.  
  186.         }
  187.     }
  188.     public void Report(Vector3 position)
  189.     {
  190.         playerpos = new Vector2(position.x,position.y);
  191.         float size = blockPrefab.transform.localScale.x;
  192.         float x = Mathf.Round(position.x-transform.position.x)/size;
  193.         int nmin=0,nmax=0;
  194.         if(x >= max - 16)
  195.         {
  196.             nmin = max+1;
  197.             nmax = max+Consts.EXPANSION_RATE;
  198.             Expand (nmin,nmax);
  199.             CheckLighting(nmin,nmax);
  200.  
  201.         }
  202.         if(x <= min + 16)
  203.         {
  204.             nmin=min - Consts.EXPANSION_RATE;
  205.             nmax=min-1;
  206.             Expand (nmin,nmax);
  207.             CheckLighting(nmin,nmax);
  208.         }
  209.  
  210.     }
  211.     public Block GetBlockAtPoint(int x,int y)
  212.     {
  213.         List<List<Block>> target = positive;
  214.         if(x<0)
  215.         {
  216.             target = negative;
  217.             x = Mathf.Abs(x)-1;
  218.         }
  219.         if(!(y>=0 && y< Consts.MAXHEIGHT))
  220.             return null;
  221.         try{
  222.             return target[x][y];
  223.         }catch{
  224.             return null;
  225.         }
  226.     }
  227.  
  228.  
  229.     private Vector2 playerpos;
  230.     //CURSOR
  231.     public GameObject cursor;
  232.     private void CheckCursor()
  233.     {
  234.         Vector3 mouse = Input.mousePosition;
  235.         mouse.z=11;
  236.         float size = blockPrefab.transform.localScale.x;
  237.         Vector3 actual= Camera.main.ScreenToWorldPoint(mouse);
  238.         int symbol = 1;
  239.         if(actual.x < 0)
  240.             symbol = -1;
  241.         float x = symbol * Mathf.Round(Mathf.Abs(actual.x)-transform.localPosition.x)/size;
  242.         float y = Mathf.Round(actual.y-transform.localPosition.y)/size;
  243.         if((playerpos.x-x)*(playerpos.x-x)+(playerpos.y-y)*(playerpos.y-y) <= Consts.REACH_RADIUS*Consts.REACH_RADIUS)
  244.         {
  245.             cursor.transform.position = new Vector3(x,y);
  246.         }
  247.         else
  248.             cursor.transform.position = new Vector3(0,-60);
  249.     }
  250.     private void CheckLighting(int min, int max)
  251.     {
  252.         foreach(LightSource l in lights)
  253.         {
  254.             l.ApplyLight(positive,negative,lightUpdateNumber,min,max);
  255.             lightUpdateNumber++;
  256.         }
  257.     }
  258.     void Update () {
  259.         CheckCursor();
  260.     }
  261. }
  262. public class Block : Item
  263. {
  264.     bool isSpawned = false;
  265.     int x;
  266.     int y;
  267.     public int lastLightUpdate = -1;
  268.     public int id1;
  269.     //Parameter = block prefab
  270.     public Block(int x, int y, int id1,GameObject prefab) : base("BLOCK_"+id1,Consts.BLOCKID+id1,"Hello world xD claude smiley",Consts.GetBlockIconSprite(id1),prefab,true,64)
  271.     {
  272.         this.x = x;
  273.         this.y = y;
  274.         this.id1 = id1;
  275.     }
  276.     //Parameter = Camera position
  277.     public void Render(Vector3 initial)
  278.     {
  279.         Vector3 definitePosition = new Vector3(initial.x+x,initial.y+y,Consts.DIST);
  280.         this.OnScreenObject = (GameObject)GameObject.Instantiate (this.OnScreenObject,definitePosition,Quaternion.identity);
  281.         base.OnScreenObject.GetComponent<SpriteRenderer> ().sprite = Consts.GetBlockSprite (id1);
  282.         base.OnScreenObject.transform.localScale=new Vector3(OnScreenObject.transform.localScale.x,-OnScreenObject.transform.localScale.y);
  283.         isSpawned = true;
  284.     }
  285.     public void Destroy()
  286.     {
  287.         GameObject.Destroy(this.OnScreenObject);
  288.        
  289.     }
  290. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement