-
Notifications
You must be signed in to change notification settings - Fork 1
/
Prefabs.cs
170 lines (146 loc) · 6.46 KB
/
Prefabs.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
using System.Numerics;
using Walgelijk;
using Walgelijk.Physics;
namespace MadnessMicroactive;
public static class Prefabs
{
public static Entity CreateMuzzleflash(Scene scene, Vector2 point, Vector2 dir, float size)
{
var e = scene.CreateEntity();
scene.AttachComponent(e, new MuzzleflashComponent
{
Position = point,
Direction = dir,
Size = size + Utilities.RandomFloat(-0.1f, 0.1f),
MaxTime = Utilities.RandomFloat(0.05f, 0.15f)
});
return e;
}
public static void CreatePlayer(Scene scene)
{
var player = CreateCharacter(scene, 102, new Vector2(0, LevelComponent.FloorLevel));
player.Faction = Faction.Player;
player.Stats = CharacterStats.Player;
player.Refill();
player.OnDeath.AddListener(static scene =>
{
RoutineScheduler.Start(routine());
IEnumerator<IRoutineCommand> routine()
{
yield return new RoutineFrameDelay();
scene.Game.AudioRenderer.Play(SoundCache.Instance.LoadSoundEffect(Resources.Load<FixedAudioData>("death.wav")), 10);
if (scene.FindAnyComponent<CameraComponent>(out var camera) && scene.FindAnyComponent<PlayerControllerComponent>(out var player))
{
scene.AttachComponent(camera.Entity, new DeathSequenceComponent(new ComponentRef<CharacterComponent>(player.Entity)));
}
}
});
player.SetOutfit(scene, Outfits.Player);
scene.AttachComponent(player.Entity, new PlayerControllerComponent());
player.Health = PersistentData.Instance.Health;
if (PersistentData.Instance.Weapon != null)
{
var e = CreateWeapon(scene, PersistentData.Instance.Weapon, player.BottomCenter);
player.Equip(scene, scene.GetComponentFrom<EquippableComponent>(e.Entity));
e.RemainingRounds = PersistentData.Instance.Ammo;
}
}
public static void CreateBoss(Scene scene)
{
var boss = CreateCharacter(scene, 101, new Vector2(1000));
boss.Faction = Faction.AAHW;
boss.Stats = CharacterStats.Boss;
boss.Refill();
boss.OnDeath.AddListener(static scene =>
{
RoutineScheduler.Start(routine());
IEnumerator<IRoutineCommand> routine()
{
yield return new RoutineDelay(5);
Game.Main.Scene = FinalWinScene.Create(Game.Main);
}
});
boss.SetOutfit(scene, Outfits.Boss);
scene.AttachComponent(boss.Entity, new BossControllerComponent());
}
public static WeaponComponent CreateWeapon(Scene scene, IWeapon wpn, Vector2 position)
{
var e = scene.CreateEntity();
switch (wpn)
{
case Firearm firearm:
{
scene.AttachComponent(e, new TransformComponent
{
Position = position,
Scale = firearm.Texture.Size,
InterpolationFlags = InterpolationFlags.Position | InterpolationFlags.Rotation
});
scene.AttachComponent(e, new BatchedSpriteComponent(firearm.Texture)
{
SyncWithTransform = true
});
scene.AttachComponent(e, new EquippableComponent(firearm.HoldPoints));
return scene.AttachComponent(e, new WeaponComponent(wpn)
{
RemainingRounds = firearm.MaxRounds
});
}
default:
throw new NotImplementedException($"{wpn.GetType()} is unsupported");
}
}
public static CharacterComponent CreateCharacter(Scene scene, int layer, Vector2 bottomCenter)
{
var main = scene.CreateEntity();
var head = scene.CreateEntity();
var body = scene.CreateEntity();
var foot1 = scene.CreateEntity();
var foot2 = scene.CreateEntity();
var hand1 = scene.CreateEntity();
var hand2 = scene.CreateEntity();
var character = scene.AttachComponent(main, new CharacterComponent
{
BottomCenter = bottomCenter,
RenderLayer = layer
});
character.Head = createLimb(head, CharacterPoses.Idle.Head, Resources.Load<Texture>("character/head.png"), Limb.Head);
character.Body = createLimb(body, CharacterPoses.Idle.Body, Resources.Load<Texture>("character/body.png"), Limb.Body);
character.Foot1 = createLimb(foot1, CharacterPoses.Idle.Foot1, Resources.Load<Texture>("character/foot.png"), Limb.Foot1);
character.Foot2 = createLimb(foot2, CharacterPoses.Idle.Foot2, Resources.Load<Texture>("character/foot.png"), Limb.Foot2);
character.Hand1 = createLimb(hand1, CharacterPoses.Idle.Hand1, Resources.Load<Texture>("character/hand.png"), Limb.Hand1);
character.Hand2 = createLimb(hand2, CharacterPoses.Idle.Hand2, Resources.Load<Texture>("character/hand.png"), Limb.Hand2);
scene.AttachComponent(head, new PhysicsBodyComponent
{
BodyType = BodyType.Dynamic,
Collider = new RectangleCollider(character.Head.Get(scene), new Vector2(0.8f))
});
scene.AttachComponent(body, new PhysicsBodyComponent
{
BodyType = BodyType.Dynamic,
Collider = new RectangleCollider(character.Body.Get(scene), new Vector2(0.8f))
});
TransformComponent createLimb(Entity e, LimbPosition p, IReadableTexture tex, Limb limb)
{
scene.AttachComponent(e, new BatchedSpriteComponent(tex)
{
SyncWithTransform = true,
RenderOrder = new RenderOrder(layer, p.Order),
});
scene.AttachComponent(e, new LimbComponent(character));
character.LimbPositions[limb] = new VelocityPos(bottomCenter + p.Translation, default);
var t = scene.AttachComponent(e, new TransformComponent
{
Position = bottomCenter + p.Translation,
Rotation = p.Rotation,
Scale = tex.Size,
InterpolationFlags = InterpolationFlags.Position | InterpolationFlags.Rotation
});
// becasue the interpolation is broken lol
t.Position = t.Position;
t.Rotation = t.Rotation;
return t;
}
return character;
}
}