-
Notifications
You must be signed in to change notification settings - Fork 0
/
Simulator.cs
130 lines (104 loc) · 3.87 KB
/
Simulator.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
using EvolutionSimulator.UI;
using System.Windows.Forms;
using System.IO;
using EvolutionSimulator.Models;
using EvolutionSimulator.DAL;
namespace EvolutionSimulator
{
public class Simulator:IDisposable
{
private Screen gui;
private MapMatrix mMatrix;
private System.Timers.Timer aTimer;
private int CX = 13;
private int CY = 13;
static readonly string Key = "";
public Simulator(Screen gui)
{
this.gui = gui;
Screen.EventTriggered += Screen_MouseDoubleClick;
InitGame();
}
private void InitGame()
{
string mapname = Path.Combine(Environment.CurrentDirectory, @"UI\Maps\map1.txt");
mMatrix = new MapMatrix(13, 13, mapname, gui);
mMatrix.addPlantToGround(10, 10, new DNAPlant("12345678123456781234567812345678"));
mMatrix.Mapname = mapname;
}
public Simulator(Screen gui,MapMatrix MapMatrix)
{
this.gui = gui;
Screen.EventTriggered += Screen_MouseDoubleClick;
mMatrix = MapMatrix;
}
public void Start()
{
aTimer = new System.Timers.Timer();
// Hook up the Elapsed event for the timer.
aTimer.Elapsed += new ElapsedEventHandler(Cycle);
// Set the Interval to 2 seconds (2000 milliseconds).
aTimer.Interval = 500;
aTimer.Enabled = true;
GC.KeepAlive(aTimer);
}
private void Cycle(object source, ElapsedEventArgs e)
{
if (gui.mustResetGame)
{
InitGame();
gui.mustResetGame = false;
}
if(mMatrix.IsCiclyFinish && !gui.IsCycleStop) mMatrix.Cycle();
if (!mMatrix.IsCiclyFinish) aTimer.Interval += 250;
lock (Key)
{
if (mMatrix.IsCiclyFinish && gui.IsCycleStop && gui.mustSaveGame)
{
if (MapRepositoyModelFirst.ExistSavedMapForThisMap(mMatrix.Mapname))
{
DialogResult Result = MessageBox.Show("There map was saved previosly, the map will be overwritten", "Saving Game", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (Result == DialogResult.No)
{
gui.mustSaveGame = false;
return;
}
MapRepositoyModelFirst.Delete_Map(mMatrix.MapID);
}
mMatrix.Savegame = gui.SaveGameName;
mMatrix.SavetoDataBase();
MessageBox.Show("The game was saved correctly","Saving Game",MessageBoxButtons.OK, MessageBoxIcon.Information);
gui.mustSaveGame = false;
}
}
}
public void showGui()
{
gui.ShowDialog();
}
private void Screen_MouseDoubleClick(object sender, DoubleClick_Arg e)
{
int x = ((e.x - (gui.mapStartXCoordinate)) / 20) ;
int y = ((e.y - (gui.mapStartYCoordinate)) / 20) ;
if (x <= CX-1 && y <= CY-1)
{
Ground gr = mMatrix.mapM.FirstOrDefault(S => S.coordn.positionx == x
&& S.coordn.positiony == y);
if(gr!=null)
{
GroundProperties newGroundProperties = new GroundProperties(gr);
newGroundProperties.ShowDialog();
}
}
}
public void Dispose()
{
aTimer.Dispose();
}
}
}