Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- public class LightSource{
- public int x,y,intensity,droprate,droprateblock;
- public LightSource(int x, int y, int intensity, int droprate,int droprateblock)
- {
- this.x=x;this.y=y;this.intensity=intensity;this.droprate=droprate;this.droprateblock=droprateblock;
- }
- public virtual void ApplyLight(List<List<Block>> positive, List<List<Block>> negative, int lun,int min, int max)
- {
- }
- }
- public class SpotLight : LightSource{
- public SpotLight(int x , int y ,int intensity, int droprate) : base(x,y,intensity,droprate,droprate)
- {
- }
- public override void ApplyLight(List<List<Block>> positive, List<List<Block>> negative, int lun,int min, int max)
- {
- this.SpotApplyLight(x,y,intensity,positive,negative,lun,min,max);
- }
- private void SpotApplyLight(int x,int y, int intensity, List<List<Block>> positive, List<List<Block>> negative, int lun,int min, int max)
- {
- if(x<min || x>max || y>=Consts.MAXHEIGHT || y<=0)
- return;
- if(intensity<=0)
- return;
- int nx = x;
- List<List<Block>> target = positive;
- if(x<0)
- {
- target=negative;
- nx=-1-x;
- }
- int nextIntensity=intensity-droprate;
- if(target[nx][y] != null)
- {
- /*if(target[nx][y].lastLightUpdate == lun)
- return;
- target[nx][y].lastLightUpdate = lun;*/
- nextIntensity=intensity-droprateblock;
- target[nx][y].OnScreenObject.GetComponent<Lighting>().SetLight(intensity);
- }
- this.SpotApplyLight(x,y-1,nextIntensity,positive,negative,lun,min,max);
- this.SpotApplyLight(x,y+1,nextIntensity,positive,negative,lun,min,max);
- this.SpotApplyLight(x-1,y,nextIntensity,positive,negative,lun,min,max);
- this.SpotApplyLight(x+1,y,nextIntensity,positive,negative,lun,min,max);
- }
- }
- public class SunLight : LightSource
- {
- public SunLight(int x):base(x,Consts.MAXHEIGHT-1,100,0,20)
- {
- }
- public override void ApplyLight(List<List<Block>> positive, List<List<Block>> negative, int lun,int min, int max)
- {
- this.SunApplyLight(x,y,intensity,positive,negative,lun,min,max);
- }
- private void SunApplyLight(int x,int y, int intensity,List<List<Block>> positive, List<List<Block>> negative, int lun,int min, int max)
- {
- if(x<min || x>max || y>=Consts.MAXHEIGHT || y<=0)
- return;
- if(intensity<=0)
- return;
- int nx = x;
- List<List<Block>> target = positive;
- if(x<0)
- {
- target=negative;
- nx=-1-x;
- }
- int nextIntensity=intensity-droprate;
- if(target[nx][y] != null)
- {
- if(target[nx][y].lastLightUpdate == lun)
- return;
- target[nx][y].lastLightUpdate = lun;
- nextIntensity=intensity-droprateblock;
- target[nx][y].OnScreenObject.GetComponent<Lighting>().SetLight(intensity);
- }
- this.SunApplyLight(x,y-1,nextIntensity,positive,negative,lun,min,max);
- if(x>min+1)
- if(x<0&& negative[-x+-1][y]!=null)
- this.SunApplyLight(x-1,y,nextIntensity,positive,negative,lun,min,max);
- if(x<max-1)
- if(x>0&&positive[x][y]!=null)
- this.SunApplyLight(x-1,y,nextIntensity,positive,negative,lun,min,max);
- }
- }
- public class Consts{
- public const int WORLDSIZE = 2048;
- public const int WORLDHEIGHT = 64;
- public const int MAXHEIGHT = 128;
- public const int MINLANDSCAPE = 56;
- public const int MAXLANDSCAPE = 72;
- public const int BLOCKID = 100;
- public const int INIT_MIN = -8;
- public const int INIT_MAX = 8;
- public const int EXPANSION_RATE = 64;
- public const int REACH_RADIUS = 3;
- public const int DIST = 5;
- public static int SEED = 555;
- public const int VISION = 8;
- GameObject initializer;
- static Sprite[] blockSprites;
- static Sprite[] blockIconSprites;
- public static void Start()
- {
- Consts.SEED = Random.Range(-100,100);
- Texture2D temp;
- blockSprites = new Sprite[4];
- for (int i = 0; i< blockSprites.Length; i++) {
- temp = (Texture2D)Resources.Load ("Blocks/"+i);
- blockSprites[i]=Sprite.Create(temp,new Rect(0,0,temp.width,temp.height),new Vector2(0.5f,0.05f),100.0f);
- }
- blockIconSprites = new Sprite[4];
- for (int i = 0; i< blockIconSprites.Length; i++) {
- temp = (Texture2D)Resources.Load ("BlocksIcons/"+i);
- blockIconSprites[i]=Sprite.Create(temp,new Rect(0,0,temp.width,temp.height),new Vector2(0.5f,0.05f),100.0f);
- }
- }
- public static Sprite GetBlockSprite(int id)
- {
- return blockSprites [id];
- }
- public static Sprite GetBlockIconSprite(int id)
- {
- return blockIconSprites [id];
- }
- }
- public class WorldInitialize : MonoBehaviour
- {
- public GameObject blockPrefab;
- public List<List<Block>> positive;
- public List<List<Block>> negative;
- public List<LightSource> lights;
- private int min, max;
- private Vector3 initialPosition;
- private int lightUpdateNumber= 100;
- void Start()
- {
- Consts.Start ();
- lights = new List<LightSource>();
- positive = new List<List<Block>>();
- negative = new List<List<Block>>();
- min = Consts.INIT_MIN;
- max= Consts.INIT_MAX;
- Expand(min,max);
- CheckLighting(min,max);
- }
- private void Expand(int newMin, int newMax)
- {
- if(newMin < this.min)
- this.min = newMin;
- if(newMax > this.max)
- this.max = newMax;
- for(int i = newMin; i <= newMax; i++)
- {
- lights.Add (new SunLight(i));
- }
- Terrain terrain = new Terrain(positive,negative,newMin,newMax,blockPrefab);
- Render(newMin,newMax);
- }
- public void Render(int min, int max)
- {
- List<List<Block>> target; int ni;
- for(int i = min; i <= max; i++)
- {
- ni = i;
- target = positive;
- if(i<0)
- {
- ni = -i-1;
- target = negative;
- }
- foreach(Block block in target[ni])
- if(block!=null)
- block.Render(transform.position);
- }
- }
- public void Report(Vector3 position)
- {
- playerpos = new Vector2(position.x,position.y);
- float size = blockPrefab.transform.localScale.x;
- float x = Mathf.Round(position.x-transform.position.x)/size;
- int nmin=0,nmax=0;
- if(x >= max - 16)
- {
- nmin = max+1;
- nmax = max+Consts.EXPANSION_RATE;
- Expand (nmin,nmax);
- CheckLighting(nmin,nmax);
- }
- if(x <= min + 16)
- {
- nmin=min - Consts.EXPANSION_RATE;
- nmax=min-1;
- Expand (nmin,nmax);
- CheckLighting(nmin,nmax);
- }
- }
- public Block GetBlockAtPoint(int x,int y)
- {
- List<List<Block>> target = positive;
- if(x<0)
- {
- target = negative;
- x = Mathf.Abs(x)-1;
- }
- if(!(y>=0 && y< Consts.MAXHEIGHT))
- return null;
- try{
- return target[x][y];
- }catch{
- return null;
- }
- }
- private Vector2 playerpos;
- //CURSOR
- public GameObject cursor;
- private void CheckCursor()
- {
- Vector3 mouse = Input.mousePosition;
- mouse.z=11;
- float size = blockPrefab.transform.localScale.x;
- Vector3 actual= Camera.main.ScreenToWorldPoint(mouse);
- int symbol = 1;
- if(actual.x < 0)
- symbol = -1;
- float x = symbol * Mathf.Round(Mathf.Abs(actual.x)-transform.localPosition.x)/size;
- float y = Mathf.Round(actual.y-transform.localPosition.y)/size;
- if((playerpos.x-x)*(playerpos.x-x)+(playerpos.y-y)*(playerpos.y-y) <= Consts.REACH_RADIUS*Consts.REACH_RADIUS)
- {
- cursor.transform.position = new Vector3(x,y);
- }
- else
- cursor.transform.position = new Vector3(0,-60);
- }
- private void CheckLighting(int min, int max)
- {
- foreach(LightSource l in lights)
- {
- l.ApplyLight(positive,negative,lightUpdateNumber,min,max);
- lightUpdateNumber++;
- }
- }
- void Update () {
- CheckCursor();
- }
- }
- public class Block : Item
- {
- bool isSpawned = false;
- int x;
- int y;
- public int lastLightUpdate = -1;
- public int id1;
- //Parameter = block prefab
- 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)
- {
- this.x = x;
- this.y = y;
- this.id1 = id1;
- }
- //Parameter = Camera position
- public void Render(Vector3 initial)
- {
- Vector3 definitePosition = new Vector3(initial.x+x,initial.y+y,Consts.DIST);
- this.OnScreenObject = (GameObject)GameObject.Instantiate (this.OnScreenObject,definitePosition,Quaternion.identity);
- base.OnScreenObject.GetComponent<SpriteRenderer> ().sprite = Consts.GetBlockSprite (id1);
- base.OnScreenObject.transform.localScale=new Vector3(OnScreenObject.transform.localScale.x,-OnScreenObject.transform.localScale.y);
- isSpawned = true;
- }
- public void Destroy()
- {
- GameObject.Destroy(this.OnScreenObject);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement