forked from eatfrog/bulletmllib4unity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmitter.cs
96 lines (86 loc) · 3.26 KB
/
Emitter.cs
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
91
92
93
94
95
96
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BulletMLLib
{
/// <summary>
/// The enemy, or such, that is emitting the bullets
/// </summary>
public class Emitter
{
// This is not really a bullet, but its pattern holder. needed for x, y and such, for now at least.
private readonly Bullet _rootBullet;
public BulletPattern Pattern { get; private set; }
private readonly IBulletManager _bulletManager;
public Emitter(IBulletManager bulletManager, BulletPattern pattern, Bullet rootBullet)
{
_bulletManager = bulletManager;
Pattern = pattern;
_rootBullet = rootBullet;
_rootBullet.Emitter = this;
InitTopNode(pattern.RootNode);
}
public void ClearTasks()
{
_rootBullet.Tasks.Clear();
}
public void Update(float x, float y)
{
_rootBullet.X = x;
_rootBullet.Y = y;
_rootBullet.Update();
}
/// <summary>
/// Initialize this bullet with a top level node
/// </summary>
/// <param name="rootNode">This is a top level node... find the first "top" node and use it to define this bullet</param>
public void InitTopNode(BulletMLNode rootNode)
{
//okay find the item labelled 'top'
bool bValidBullet = false;
BulletMLNode topNode = rootNode.FindLabelNode("top", NodeName.Action);
if (topNode != null)
{
//initialize with the top node we found!
_rootBullet.InitNode(topNode);
bValidBullet = true;
_rootBullet.BulletSpawned();
}
else
{
//ok there is no 'top' node, so that means we have a list of 'top#' nodes
for (int i = 1; i < 10; i++)
{
topNode = rootNode.FindLabelNode("top" + i, NodeName.Action);
if (topNode != null)
{
if (!bValidBullet)
{
//Use this bullet!
_rootBullet.InitNode(topNode);
bValidBullet = true;
_rootBullet.BulletSpawned();
}
else
{
//Create a new bullet
Bullet b = _bulletManager.CreateBullet(this);
//set the position to this dude's position
b.X = _rootBullet.X;
b.Y = _rootBullet.Y;
//initialize with the node we found
b.InitNode(topNode);
b.BulletSpawned();
}
}
}
}
if (!bValidBullet)
{
//We didnt find a "top" node for this dude, remove him from the game.
_bulletManager.RemoveBullet(_rootBullet);
}
}
}
}