Skip to content

Commit

Permalink
separated view and model (calc model asynchronously) but there's a li…
Browse files Browse the repository at this point in the history
…ttle bit delay.
  • Loading branch information
YKariyado committed Jun 15, 2021
1 parent a11cc80 commit a528dfe
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 119 deletions.
251 changes: 133 additions & 118 deletions Assets/GameManageSparse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public class GameManageSparse : MonoBehaviour

List<DotManageSparse> displaying_dots_list = new List<DotManageSparse>(); //the list of dots that's displaying now

Dictionary<Tuple<int, int, int>, int> current_cell_list = new Dictionary<Tuple<int, int, int>, int>(); 
Dictionary<Tuple<int, int, int>, int> current_cell_list = new Dictionary<Tuple<int, int, int>, int>();
Dictionary<Tuple<int, int, int>, int> cell_list_for_judge = new Dictionary<Tuple<int, int, int>, int>();

// if cell_location_matrix[x,y,z] == 1, the cell near by the player will be appeared
Sparse3DArray<int> cell_location_matrix = new Sparse3DArray<int>();
Expand All @@ -35,7 +36,7 @@ public class GameManageSparse : MonoBehaviour
//timeRecent is the time to refresh model with chords, timeRecent2 is the time to refresh model with sequential.
float timeRecent = 1, timeRecent2 = 0;

bool isRun=true, isPeriodic=true, isSequential=false;
bool isRun = true, isPeriodic = true, isSequential = false;

// Awake is called before Start
void Awake()
Expand All @@ -49,45 +50,60 @@ void Start()
//setting head_pref position flag_
head_pref.transform.position = new Vector3(((-n / 2.0f) + ((n / 2.0f) - 1)) / 2.0f, ((-n / 2.0f) + ((n / 2.0f) - 1)) / 2.0f, ((-n / 2.0f) + ((n / 2.0f) - 1)) / 2.0f);

//Random Debug
for (int i = n / 2 - 4; i < n / 2 + 4; i++)
{
for (int j = n / 2 - 4; j < n / 2 + 4; j++)
{
for (int k = n / 2 - 4; k < n / 2 + 4; k++)
{
if (UnityEngine.Random.Range(0, 5) == 0)
{
cell_location_matrix[i, j, k] = 1;
var key1 = new Tuple<int, int, int>(i,j,k);
current_cell_list.Add(key1, 0);
}
}
}
}

////Blinker Debug
////Random Debug
//for (int i = n / 2 - 4; i < n / 2 + 4; i++)
//{
// for (int j = n / 2 - 4; j < n / 2 + 4; j++)
// {
// for (int k = n / 2 - 4; k < n / 2 + 4; k++)
// {
// if (UnityEngine.Random.Range(0, 5) == 0)
// {
// cell_location_matrix[i, j, k] = 1;
// var key1 = new Tuple<int, int, int>(i, j, k);
// current_cell_list.Add(key1, 0);
// }
// }
// }
//}

////Blinker Debug
//cell_location_matrix[n / 2, n / 2, n / 2] = 1;
//var key1 = new Tuple<int, int, int>(n / 2, n / 2, n / 2);
//current_cell_list.Add(key1,0);

//current_cell_list.Add(key1, 0);

//cell_location_matrix[n / 2 + 1, n / 2, n / 2] = 1;
//var key2 = new Tuple<int, int, int>(n / 2 + 1, n / 2, n / 2);
//current_cell_list.Add(key2, 0);

//current_cell_list.Add(key2, 0);

//cell_location_matrix[n / 2, n / 2 + 1, n / 2 + 1] = 1;
//var key3 = new Tuple<int, int, int>(n / 2, n / 2+1, n / 2+1);
//current_cell_list.Add(key3, 0);

//var key3 = new Tuple<int, int, int>(n / 2, n / 2 + 1, n / 2 + 1);
//current_cell_list.Add(key3, 0);

//cell_location_matrix[n / 2 + 1, n / 2 + 1, n / 2 + 1] = 1;
//var key4 = new Tuple<int, int, int>(n / 2+1, n / 2+1, n / 2+1);
//current_cell_list.Add(key4, 0);

//var key4 = new Tuple<int, int, int>(n / 2 + 1, n / 2 + 1, n / 2 + 1);
//current_cell_list.Add(key4, 0);

cell_location_matrix[n / 2, n / 2, n / 2] = 1;
var key1 = new Tuple<int, int, int>(n / 2, n / 2, n / 2);
current_cell_list.Add(key1, 0);

cell_location_matrix[n / 2, n / 2, n / 2 + 1] = 1;
var key2 = new Tuple<int, int, int>(n / 2, n / 2, n / 2 + 1);
current_cell_list.Add(key2, 0);

cell_location_matrix[n / 2, n / 2 + 1, n / 2] = 1;
var key3 = new Tuple<int, int, int>(n / 2, n / 2 + 1, n / 2);
current_cell_list.Add(key3, 0);

cell_location_matrix[n / 2, n / 2 + 1, n / 2 + 1] = 1;
var key4 = new Tuple<int, int, int>(n / 2, n / 2 + 1, n / 2 + 1);
current_cell_list.Add(key4, 0);

}

void Update()
{

//Moving the head
head_location = head_pref.transform.position;
head_location.x = Mathf.Clamp(head_location.x, (-n / 2) + range, (n / 2) - range);
Expand All @@ -100,6 +116,8 @@ void Update()

if (isRun)
{
timeRecent2 += Time.deltaTime;

//View update
//Updates the view when the player's position changes in integer increments
if (pre_x != (int)head_location.x || pre_y != (int)head_location.y || pre_z != (int)head_location.z || timeRecent == 0)
Expand All @@ -112,97 +130,11 @@ void Update()
UpdateDotView();
}

timeRecent2 += Time.deltaTime;

if (timeRecent == 0)
{
timeRecent++;

Dictionary<Tuple<int, int, int>, int> cell_list_for_judge = new Dictionary<Tuple<int, int, int>, int>();

////非同期のオペレーション。judgeとcurrentがスピード差でやられちゃう。次のtask実行まで待てばいい
Task.Run(() =>
{

//add cells to alives and deads
foreach (var e in current_cell_list)
{
//add 1 to each adjacency cell
for (int _i = -1; _i < 2; _i++)
{
for (int _j = -1; _j < 2; _j++)
{
for (int _k = -1; _k < 2; _k++)
{
int x = _i + e.Key.Item1;
int y = _j + e.Key.Item2;
int z = _k + e.Key.Item3;

if (_i == 0 && _j == 0 && _k == 0)
continue;

if (x < 0)
{
x += n;
}
else if (x >= n)
{
x -= n;
}

if (y < 0)
{
y += n;
}
else if (y >= n)
{
y -= n;
}

if (z < 0)
{
z += n;
}
else if (z >= n)
{
z -= n;
}

var key = new Tuple<int, int, int>(x, y, z);

if (cell_list_for_judge.ContainsKey(key))
{
cell_list_for_judge[key]++;
}
else
{
cell_list_for_judge.Add(key, 1);
}

}
}
}
}

cell_location_matrix.dataClear();
current_cell_list.Clear();

foreach (var e in cell_list_for_judge)
{
if (e.Value <= r2 && e.Value >= r1)
{
//flag
cell_location_matrix[e.Key.Item1, e.Key.Item2, e.Key.Item3] = 1;
current_cell_list.Add(e.Key,0);
}
}


});


Debug.Log("the number of cells in current_cell_list: " + current_cell_list.Count());
Debug.Log("the number of cells in cell_list_for_judge: " + cell_list_for_judge.Count());
Judge();

}

Expand Down Expand Up @@ -245,6 +177,89 @@ public void UpdateDotView()
}
}
}

}

public async void Judge()
{
await Task.Run(() =>
{
//add cells to alives and deads
foreach (var e in current_cell_list)
{
//add 1 to each adjacency cell
for (int _i = -1; _i < 2; _i++)
{
for (int _j = -1; _j < 2; _j++)
{
for (int _k = -1; _k < 2; _k++)
{
int x = _i + e.Key.Item1;
int y = _j + e.Key.Item2;
int z = _k + e.Key.Item3;

if (_i == 0 && _j == 0 && _k == 0)
continue;

if (x < 0)
{
x += n;
}
else if (x >= n)
{
x -= n;
}

if (y < 0)
{
y += n;
}
else if (y >= n)
{
y -= n;
}

if (z < 0)
{
z += n;
}
else if (z >= n)
{
z -= n;
}

var key = new Tuple<int, int, int>(x, y, z);

if (cell_list_for_judge.ContainsKey(key))
{
cell_list_for_judge[key]++;
}
else
{
cell_list_for_judge.Add(key, 1);
}
}
}
}
}

cell_location_matrix.dataClear();
current_cell_list.Clear();

foreach (var e in cell_list_for_judge)
{
if (e.Value <= r2 && e.Value >= r1)
{
//flag
cell_location_matrix[e.Key.Item1, e.Key.Item2, e.Key.Item3] = 1;
current_cell_list.Add(e.Key, 0);
}
}

cell_list_for_judge.Clear();

});

}

}
2 changes: 1 addition & 1 deletion Assets/Scenes/Sparse.unity
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 1217d0940cd414e7bafa0ae9b974ead4, type: 3}
m_Name:
m_EditorClassIdentifier:
speed: 0.05
speed: 0.1
--- !u!81 &1975725066
AudioListener:
m_ObjectHideFlags: 0
Expand Down

0 comments on commit a528dfe

Please sign in to comment.