-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
147 lines (132 loc) · 4.33 KB
/
Program.cs
File metadata and controls
147 lines (132 loc) · 4.33 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
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
using TopDownDungeon.Enums;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.DependencyInjection;
using TopDownDungeon.Models;
using TopDownDungeon.Logic;
using TopDownDungeon.UI;
using TopDownDungeon.Audio;
using TopDownDungeon.Utility;
HostApplicationBuilder builder = new HostApplicationBuilder(args);
builder.Services.AddSingleton<GameState>();
builder.Services.AddSingleton<Canvas>();
builder.Services.AddTransient<MapFactory>();
builder.Services.AddTransient<BattleEngine>();
builder.Services.AddTransient<EncounterFactory>();
builder.Services.AddTransient<FoodFactory>();
builder.Services.AddTransient<PotionFactory>();
builder.Services.AddTransient<InterfaceHelper>();
builder.Services.AddTransient<TextPrompts>();
builder.Services.AddTransient<Movement>();
builder.Services.AddTransient<Player>();
if (System.OperatingSystem.IsLinux())
builder.Services.AddTransient<IAudioController, LinuxAudio>();
else
builder.Services.AddTransient<IAudioController, AudioController>();
using IHost host = builder.Build();
host.Start();
//Game Defaults
const int PlayerHealth = 100;
const int PlayerStamina = 20;
const int PlayerSpeed = 1;
const bool CanMove = true;
//Utility
var canvas = host.Services.GetRequiredService<Canvas>();
var mapBuilder = host.Services.GetRequiredService<MapFactory>();
var battleEngine = host.Services.GetService<BattleEngine>();
var movement = host.Services.GetRequiredService<Movement>();
var audio = host.Services.GetRequiredService<IAudioController>();
var map = mapBuilder.CreateMap(5, 5);
var state = host.Services.GetRequiredService<GameState>();
state.OnShowMessage += (s, e) => canvas.ShowMapMessage(e);
Map CreateNewMap()
{
var tempMap = mapBuilder.CreateMap(canvas.GetWindowSize().Height, canvas.GetWindowSize().Width);
tempMap.Win += HandleWinState;
tempMap.FoundEncounter += HandleEncounter;
tempMap.FoundFood += HandleFood;
tempMap.FoundPotion += HandlePotion;
return tempMap;
}
//Logic
void HandleWinState(object sender, EventArgs args)
{
audio.PlayWinnerEffect();
canvas.ShowMapMessage("WINNER!");
ResetState();
}
void HandleFood(object sender, MapEventArgs args)
{
canvas.ShowMapMessage($"Found {args.Food.Name} increasing HP and stamina by {args.Food.EffectValue}");
state.Player.EatFood(args.Food);
}
void HandlePotion(object sender, MapEventArgs args)
{
canvas.ShowMapMessage($"Found {args.Potion?.Effect} potion, bottoms up!");
state.Player.DrinkPotion(args.Potion);
}
void HandleEncounter(object sender, MapEventArgs args)
{
audio.PlayEncounterEffect();
canvas.ShowMapMessage($"Encountered {args.Encounter.Opponent.Name}. ; Get ready to fight!");
var outcome = battleEngine.StartEncounter(args.Encounter, state.Player.Health);
if (outcome.Success)
{
state.Player.Health = outcome.PlayerHP;
map.Encounters.Remove(args.Encounter);
canvas.DrawMap(map);
}
else
{
ResetState();
}
}
void ResetState()
{
map = CreateNewMap();
state.Player.Location = map.Spawn;
state.Player.Health = PlayerHealth;
state.Player.Stamina = PlayerStamina;
state.Player.Speed = PlayerSpeed;
state.Player.CanMove = CanMove;
state.VisitedLocations.Clear();
canvas.DrawMap(map);
}
//Main
canvas.ShowStartMenu();
ResetState();
while (true)
{
var input = Console.ReadKey(true);
switch (input.Key)
{
case ConsoleKey.Escape:
break;
case ConsoleKey.Spacebar:
break;
case ConsoleKey.LeftArrow:
movement.MovePlayer(MovementDirection.West);
break;
case ConsoleKey.UpArrow:
movement.MovePlayer(MovementDirection.North);
break;
case ConsoleKey.RightArrow:
movement.MovePlayer(MovementDirection.East);
break;
case ConsoleKey.DownArrow:
movement.MovePlayer(MovementDirection.South);
break;
case ConsoleKey.A:
movement.MovePlayer(MovementDirection.West);
break;
case ConsoleKey.D:
movement.MovePlayer(MovementDirection.East);
break;
case ConsoleKey.S:
movement.MovePlayer(MovementDirection.South);
break;
case ConsoleKey.W:
movement.MovePlayer(MovementDirection.North);
break;
}
map.ExamineNewLocation(state.Player.Location);
}