/
gamemain.cs
148 lines (128 loc) · 4.23 KB
/
gamemain.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive;
using System.Reactive.Linq;
using System.Threading.Tasks;
namespace taticlearn
{
class gamemain
{
public int turn { get { return turn_; } }
public Tuple<int, int> selectedindex { get; set; }
public Tuple<int, int> activeIndex { get; set; }
grid agrid;
int turn_;
IGUImenu menu;
List<Igameobject> objects;
Dictionary<ConsoleKey, Action> keyMapping;
internal TimeSpan GameTime = new TimeSpan(); //Internal for debugging purposes
bool needsUpdate = true;
private Igameobject ActiveNpc;
void updateGUI(){ needsUpdate = true; }
public gamemain()
{
agrid = new grid(10, 10, this);
objects = new List<Igameobject>();
Igameobject onenpc = new npc(this);
insertObject(onenpc, 0, 0);
activeIndex = Tuple.Create(0, 0);
setActive(onenpc);
}
private void setActive(Igameobject onenpc)
{
ActiveNpc = onenpc;
menu = ActiveNpc.menu();
keyMapping = ActiveNpc.menu().keyMapping();
}
Action myaction;
internal void select()
{
keyMapping = agrid.keyMapping;
agrid.selection = true;
myaction = () => { setActive(ActiveNpc); };
agrid.SimpleEvent += agrid_SimpleEvent;
updateGUI();
}
internal void move()
{
keyMapping = agrid.keyMapping;
agrid.selection = true;
myaction = () => { agrid.moveFromTo(activeIndex, selectedindex); activeIndex = selectedindex; setActive(ActiveNpc); };
agrid.SimpleEvent += agrid_SimpleEvent;
updateGUI();
}
void agrid_SimpleEvent(object sender, grid.SomeEventArgs e)
{
selectedindex = e.arg;
myaction();
agrid.SimpleEvent -= agrid_SimpleEvent;
}
private void insertObject(Igameobject obj, int x, int y)
{
objects.Add(obj);
agrid.insertgrid(obj, x, y);
}
public void runturn()
{
turn_++;
}
public void Update(TimeSpan deltaT)
{
GameTime += deltaT;
//Linq ways of doing it
//needsUpdate |= objects.Aggregate<gameobject, Boolean, Boolean>(HudUpdate(), (workingUpdate, next) =>
// next.Update(deltaT) | workingUpdate, x => x);
//needsUpdate |= HudUpdate()|(from obj in objects
// select obj.Update(deltaT)).Aggregate(HudUpdate(), (workingUpdate, next) =>
// next | workingUpdate);
needsUpdate |= HudUpdate();
foreach (var a in objects)
needsUpdate |= a.Update(deltaT);
}
long lasttick = 0; //may overflow, can be changed for rem
private Boolean HudUpdate()
{
if (GameTime.Ticks / TimeSpan.TicksPerSecond > lasttick)
{
lasttick = (GameTime.Ticks / TimeSpan.TicksPerSecond);
return true;
}
else
{
return false;
}
}
internal void PrintGame()
{
if (needsUpdate)
{
Console.Clear();
printHud();
agrid.print();
menu.print();
needsUpdate = false;
}
}
private void printHud()
{
Console.WriteLine("Game time {0}", GameTime.TotalSeconds);
Console.WriteLine("Turn {0}", turn);
Console.WriteLine("Last Selected: {0}", selectedindex);
}
internal void keyInput(ConsoleKey a)
{
try
{
keyMapping[a]();
updateGUI();
}
catch (KeyNotFoundException)
{ }
catch (NotImplementedException b)
{ Console.WriteLine("Not implemented: {0}", b.Message); }
catch (Exception)
{ throw; }
}
}
}