标签:
看注释吧,写的很清楚了
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ZoneTest
{
public class Zone
{
public int mId;
public float Width;
public float Height;
public List<Zone> VisibleZoneList = new List<Zone>();
public Zone(int id, float width, float height)
{
mId = id;
Width = width;
Height = height;
}
public void AddVisibleZone(Zone zone)
{
VisibleZoneList.Add(zone);
}
}
public class Scene
{
private List<Zone> mZoneList = new List<Zone>();
public Scene(float w,float h)
{
mSceneWidth = w;
mSceneHeight = h;
}
public const float ZONE_SIDE = 1.0f;
//场景宽高
private float mSceneWidth;
private float mSceneHeight;
//格子列数和行数
private int mZoneColumnCount;
private int mZoneLineCount;
public Zone GetZone(int id)
{
if (id < 0 || id > mZoneList.Count)
{
return null;
}
return mZoneList[id];
}
//Zone示意图
/*
| mSceneWidth |
----------------- --
| | | | |ZONE_SIDE
-----------------
| | | | |
----------------- mSceneHeight
| | | | |
----------------- --
----------------- line 0
----------------- line 1
----------------- line 2
----------------- line 3
column0 column1 column2 column3
| | | |
| | | |
| | | |
| | | |
*
*/
public bool InitZone()
{
//计算场景内zone行和列
mZoneLineCount = (int)Math.Ceiling((double)mSceneHeight / (double)ZONE_SIDE);
mZoneColumnCount = (int)Math.Ceiling((double)mSceneWidth / (double)ZONE_SIDE);
//创建zone
for (int i = 0; i < mZoneLineCount; i++)
{
for (int j = 0; j < mZoneColumnCount; j++)
{
int id = i * mZoneColumnCount + j;
Zone zone = new Zone(id, ZONE_SIDE, ZONE_SIDE);
mZoneList.Add(zone);
}
}
//最大周围多少个格子被可见 MaxNearByZoneNumber*MaxNearByZoneNumber 个
const int MaxNearByZoneNumber = 3;
//可见格子的起始偏移
const int Offset = MaxNearByZoneNumber / 2;
//把周围可见格子加入列表
/*
* -------------
* |0 |1 |2 |
* -------------
* |3 |4 |5 |
* -------------
* |6 |7 |8 |
* -------------
*/
//0的可见格子为 0134
//4的可见格子为 0123456789
//7的可见格子为 345678
//遍历所有格子行
for (int i = 0; i < mZoneLineCount; i++)
{
//遍历所有格子列
for (int j = 0; j < mZoneColumnCount; j++)
{
//当前要判断可见性的格子id
int id = i * mZoneColumnCount + j;
Zone zone = mZoneList[id];
//遍历周围 MaxNearByZoneNumber*MaxNearByZoneNumber个格子
//判断[x,y]这个格子是否出界
//行
for (int n = 0; n < MaxNearByZoneNumber; n++)
{
int y = i - Offset + n;
if (y < 0 || y >= mZoneLineCount) continue;//y出界
//列
for (int m = 0; m < MaxNearByZoneNumber; m++)
{
int x = j - Offset + m;
if (x < 0 || x >= mZoneColumnCount) continue;//x出界
//算当前格子id
int visibleZoneId = y * mZoneColumnCount + x;
//if (visibleZoneId == id) continue;//是自己格子也要加入,自己属于自己可见格子
zone.AddVisibleZone(mZoneList[visibleZoneId]);
}
}
}
}
return true;
}
public void Print()
{
for (int i = 0; i < mZoneLineCount; i++)
{
string str = "";
//遍历所有格子列
for (int j = 0; j < mZoneColumnCount; j++)
{
str += (i * mZoneColumnCount + j).ToString()+"\t";
}
Debug.Print(str);
}
}
public void PrintZone(int i)
{
Debug.Print("[{0}]---------------------",i);
var zone = mZoneList[i];
string str = "";
foreach(var z in zone.VisibleZoneList)
{
str+=z.mId.ToString()+"\t";
}
Debug.Print(str);
Debug.Print("[{0}]---------------------",i);
}
}
class Program
{
static void Main(string[] args)
{
Scene scene = new Scene(5,7);
scene.InitZone();
scene.Print();
int oldId = 5;
int newId = 10;
Zone oldZone = scene.GetZone(oldId);
Zone newZone = scene.GetZone(newId);
/*
//比较计算应该删除我的zone
List<Zone> deleteMeZone = new List<Zone>();
foreach (var zone in oldZone.VisibleZoneList)
{
if (!newZone.VisibleZoneList.Contains(zone))
{
deleteMeZone.Add(zone);
Debug.Print(zone.mId.ToString());
}
}
Debug.Print("-----------");
//比较计算应该创建我的
List<Zone> createMeZone = new List<Zone>();
foreach (var zone in newZone.VisibleZoneList)
{
if (!oldZone.VisibleZoneList.Contains(zone))
{
createMeZone.Add(zone);
Debug.Print(zone.mId.ToString());
}
}
*/
scene.PrintZone(4);
scene.PrintZone(16);
scene.PrintZone(28);
scene.PrintZone(30);
}
}
}
标签:
原文地址:http://www.cnblogs.com/mrblue/p/4652091.html