- Home /
help with figuring out combination concept?
Hi community!
I was recently going through google and came across the 2016b google halloween game. ( https://www.google.com/doodles/halloween-2016 ). I find this game to be simple and interesting and i'm sure there are nuggets in there for us all. I was interested in the combinations which appear over the ghosts and how something like this would be scripted in c#. I have been hitting roadblocks trying to figure this out. If someone could help me to understand how the combination sequence works, and probably provide some pseudo code (or actual code) that would be amazing. From what I see when the pattern is drawn on screen, the combo sequence begins, and if the pattern drawn matches the pattern in the sequence it is destroyed, and it goes in order until all the patterns of the sequence are drawn, then when the sequence is complete the ghost gets destroyed. Can someone help me with a script or pseudo as to how to accomplish this?
Answer by Rekrahttam1 · Jan 29, 2017 at 01:07 PM
The way I would use is to find the changes in mouse direction. This code will detect each change of direction, and add the point of change to a list.
It then calls ProcessPoints() when the drawing is finished, which converts the points into line vectors. It is easy to then detect a shape from the major direction of each line. e.i. a horizontal +x line would high +x and near 0 y and a diagonal line would be roughly the same (absolute value) of x and y.
Going further, you could normalise the vectors and use the proportions of each for more accuracy or even do inverse tan to get the angle of the line. These would have higher processing costs though, so are only required if precision is needed.
List<Vector2> points = new List<Vector2>(); //store directions of each line
float minimumDistance = 20; //minimum length of line (stops mini lines from being formed - is in each axis though, 18 in x and 18 in y = 25.5 length, but each axis is less, so is still shorter)
float xMost = 0;
float yMost = 0;
// Use this for initialization
void Start() {
}
// Update is called once per frame
void Update() {
if (Input.GetMouseButtonDown(0)) { //mouse click = start new
print("new drawing");
points = new List<Vector2>();
points.Add(new Vector2(Input.mousePosition.x, Input.mousePosition.y));
xMost = 0;
yMost = 0;
}
else if (Input.GetMouseButton(0)) { //mouse held
if (Mathf.Abs(Input.mousePosition.x - points[points.Count - 1].x) > Mathf.Abs(xMost)) { //checks if mouse has been moved further in x from last point
xMost = Input.mousePosition.x - points[points.Count - 1].x;
}
if (Mathf.Abs(Input.mousePosition.y - points[points.Count - 1].y) > Mathf.Abs(yMost)) { //checks if mouse has been moved further in y from last point
yMost = Input.mousePosition.y - points[points.Count - 1].y;
}
if ((Mathf.Abs(xMost) >= minimumDistance || Mathf.Abs(yMost) >= minimumDistance) && (Mathf.Abs(Input.mousePosition.x - points[points.Count - 1].x) < Mathf.Abs(xMost) - minimumDistance || Mathf.Abs(Input.mousePosition.y - points[points.Count - 1].y) < Mathf.Abs(yMost) - minimumDistance)) { //changed direction in x or y and line is longer than minimum
points.Add(new Vector2(xMost + points[points.Count - 1].x, yMost + points[points.Count - 1].y));
xMost = 0;
yMost = 0;
print("point added");
}
} else if (points.Count > 0) { //mouse not pressed and positions stored = end of drawing
if (Mathf.Abs(xMost) >= minimumDistance && Mathf.Abs(yMost) >= minimumDistance) {
points.Add(new Vector2(xMost + points[points.Count - 1].x, yMost + points[points.Count - 1].y));
xMost = 0;
yMost = 0;
print("point added");
}
print("drawing finished");
ProcessPoints();
points = new List<Vector2>();
}
}
void ProcessPoints() {
print("processing points");
//process list of points to find shape
//perhaps turn list of points into list of directions - recognise whether each line is mainly horizontal, vertical or diagonal, and in which direction - then check if combination matches.
List<Vector2> lines = new List<Vector2>();
for (int counter = 0; counter < points.Count - 1; counter ++) {
lines.Add(new Vector2(points[counter + 1].x - points[counter].x, points[counter + 1].y - points[counter].y));
print(lines[counter].x + ", " + lines[counter].y); //print lines
}
}
However, in its present state, it will not detect a change in direction along the axis directly (from horizontal to vertical, I_ or _I), but it will detect diagonal to another direction of diagonal (\/, <, /\ or >). This is due to neither axis getting shorter (and so the algorithm sees it as a continued expansion).
This may even be a reason why there are no such lines in the game you linked, only \/ and /\, not I_ or _I.
AHHHHH!! This makes so much sense! I cannot wait to try it out. I will add a comment when i test it out later and let update on how it works.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Play animations in order with C# 0 Answers
drop down list c# 1 Answer
GameObjects are moving incredibly fast, possibly make slower? 1 Answer