-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSample.cs
More file actions
90 lines (72 loc) · 2.19 KB
/
Sample.cs
File metadata and controls
90 lines (72 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using System;
using System.Numerics;
using System.Collections.Generic;
// 飞机
public class PlaneEntity {
public int id;
public Vector2 pos;
}
// 飞机仓库
public class PlaneRepository {
Dictionary<int, PlaneEntity> all;
public PlaneRepository() {
all = new Dictionary<int, PlaneEntity>();
}
public void Add(PlaneEntity entity) {
all.Add(entity.id, entity);
}
public PlaneEntity FindByID(int id) {
// 看字典里有没有
all.TryGetValue(id, out PlaneEntity entity);
return entity;
}
public void FindX(Predicate<PlaneEntity> predicate) {
foreach (var plane in all.Values) {
bool isTrue = predicate.Invoke(plane);
if (isTrue) {
System.Console.WriteLine($"Plane {plane.id} at {plane.pos}");
}
}
}
public void ForeachInside(Vector2 center, float radius, Action<PlaneEntity> action) {
foreach (var plane in all.Values) {
if (Vector2.Distance(plane.pos, center) < radius) {
action.Invoke(plane);
}
}
}
}
public static class Sample {
public static void Entry() {
PlaneRepository repo = new PlaneRepository();
// o o o o 4
// o o o x 2
// o o - o 3
// o 1 o o o
// o o o o o
repo.Add(new PlaneEntity() { id = 1, pos = new Vector2(-1, -1) });
repo.Add(new PlaneEntity() { id = 2, pos = new Vector2(2, 1) });
repo.Add(new PlaneEntity() { id = 3, pos = new Vector2(0, 2) });
repo.Add(new PlaneEntity() { id = 4, pos = new Vector2(2, 2) });
repo.ForeachInside(new Vector2(1, 1), 2, LogPlane);
repo.FindX(FindIDLess3);
repo.FindX(FIndIDMoreThan2);
}
static void LogPlane(PlaneEntity plane) {
System.Console.WriteLine($"Plane {plane.id} at {plane.pos}");
}
static bool FindIDLess3(PlaneEntity plane) {
if (plane.id < 3) {
return true;
} else {
return false;
}
}
static bool FIndIDMoreThan2(PlaneEntity plane) {
if (plane.id > 2) {
return true;
} else {
return false;
}
}
}