source: 2012/23/RamiP/YAG2DSSBase/YAG2DSS/YAG2DSS/YAG2DSS.cs @ 2803

Revision 2803, 10.4 KB checked in by ramipasa, 11 years ago (diff)
Line 
1using System;
2using System.Collections.Generic;
3using System.IO;
4using Jypeli;
5using Jypeli.Assets;
6using Jypeli.Controls;
7using Jypeli.Effects;
8using Jypeli.Widgets;
9using ALTK;
10using ALTK.GUI.Dialogs;
11
12public class YAG2DSS : PhysicsGame
13{
14    ALTKHandler altkHandler;
15    GameObject Kursori;
16
17    List<PhysicsObject> Pelaaja = new List<PhysicsObject>();
18    List<PhysicsObject> Madonreika = new List<PhysicsObject>();
19    List<PhysicsObject> Minioni = new List<PhysicsObject>();
20
21    Timer Madonreikaajastin;
22    Timer Minioniajastin;
23
24    List<int> PelaajanKesto = new List<int>();
25
26    public override void Begin()
27    {
28        #region Cursor handling
29        Kursori = new GameObject(32, 32);
30        Kursori.Image = Image.FromStream(File.OpenRead(ALTKConstants.TexturePath + "cursor.png"));
31        Kursori.IsVisible = false;
32        AnalogHandler handler = new AnalogHandler(HandleCursor);
33        Mouse.ListenMovement(0.1, handler, "");
34        Add(Kursori);
35        #endregion
36        LuoKentta();
37        AsetaNappaimet();
38        LuoAjastin();
39        //LuoMadonreika();
40        //PhoneBackButton.Listen(ConfirmExit, "Lopeta peli");
41        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ShowExit, "Lopeta peli");
42    }
43
44    void LuoKentta()
45    {
46        PhysicsObject vasenReuna = Level.CreateLeftBorder();
47        vasenReuna.IsVisible = false;
48        PhysicsObject oikeaReuna = Level.CreateRightBorder();
49        oikeaReuna.IsVisible = false;
50        PhysicsObject ylaReuna = Level.CreateTopBorder();
51        ylaReuna.IsVisible = false;
52        PhysicsObject alaReuna = Level.CreateBottomBorder();
53        alaReuna.IsVisible = false;
54
55        Level.BackgroundColor = Color.Black;
56
57        LuoPelaaja(300);
58        LuoPelaaja(-300);
59    }
60
61    void LuoPelaaja(double x)
62    {
63        PhysicsObject pelaaja = new PhysicsObject(20, 20);
64
65        pelaaja.X = x;
66        pelaaja.Y = -250;
67        pelaaja.Restitution = 0.5;
68        pelaaja.AngularDamping = 0.1;
69        pelaaja.Shape = Shape.Triangle;
70        pelaaja.Angle = Angle.FromDegrees(pelaaja.Angle.Degrees + 90);
71        PelaajanKesto.Add(100);
72        Pelaaja.Add(pelaaja);
73        Add(Pelaaja[Pelaaja.Count - 1]);
74    }
75
76    void LuoAjastin()
77    {
78        Madonreikaajastin = new Timer();
79        Madonreikaajastin.Interval = 3;
80        Madonreikaajastin.Timeout += new Action(NaytaAnimaatio);
81        Madonreikaajastin.Start();
82
83        Minioniajastin = new Timer();
84        Minioniajastin.Interval = 2;
85        Minioniajastin.Timeout += new Action(Minioniajastin_Timeout);
86        Minioniajastin.Start();
87    }
88
89    void Minioniajastin_Timeout()
90    {
91        Timer.SingleShot(2, AjaLuoMinioni);
92    }
93
94    void AjaLuoMinioni()
95    {
96        for (int Madonreikaa = 0; Madonreikaa < Madonreika.Count; Madonreikaa++)
97        LuoMinioni(Madonreikaa);
98    }
99   
100    void NaytaAnimaatio()
101    {
102        Timer.SingleShot(2, LuoMadonreika);
103    }
104
105    void LuoMadonreika()
106    {
107        int arvo = RandomGen.NextInt(4);
108
109        double x = 0.0;
110        double y = 0.0;
111
112        // yläreuna
113        if (arvo == 0)
114        {
115            x = RandomGen.NextDouble(Level.Left + 30, Level.Right - 30);
116            y = RandomGen.NextDouble(Level.Top - 30, Level.Top - 300);
117        }
118        // alareuna
119        else if (arvo == 1)
120        {
121            x = RandomGen.NextDouble(Level.Left + 30, Level.Right - 30);
122            y = RandomGen.NextDouble(Level.Bottom + 30, Level.Bottom + 300);
123        }
124        // vasen reuna
125        else if (arvo == 2)
126        {
127            x = RandomGen.NextDouble(Level.Left + 30, Level.Left + 300);
128            y = RandomGen.NextDouble(Level.Bottom + 30, Level.Top - 30);
129        }
130        // oikea reuna
131        else
132        {
133            x = RandomGen.NextDouble(Level.Right - 30, Level.Right - 300);
134            y = RandomGen.NextDouble(Level.Bottom + 30, Level.Top - 30);
135        }
136
137        PhysicsObject madonreika = PhysicsObject.CreateStaticObject(50, 50);
138
139        madonreika.X = x;
140        madonreika.Y = y;
141        madonreika.CollisionIgnoreGroup = 5;
142        Madonreika.Add(madonreika);
143        Add(madonreika);
144        LuoMinioni(Madonreika.Count - 1);
145    }
146
147    void LuoMinioni(int madonreikaID)
148    {
149        PhysicsObject minioni = new PhysicsObject(20, 20);
150
151        minioni.X = Madonreika[madonreikaID].X;
152        minioni.Y = Madonreika[madonreikaID].Y;
153        minioni.CollisionIgnoreGroup = 5;
154        minioni.Shape = Shape.Triangle;
155        minioni.Color = Color.Red;
156        minioni.Angle = (Pelaaja[1].Position - minioni.Position).Angle;
157        minioni.Hit(Vector.FromLengthAndAngle(100, minioni.Angle));
158        Minioni.Add(minioni);
159        Add(Minioni[Minioni.Count - 1]);
160        MinioninLiike(Minioni[Minioni.Count - 1]);
161    }
162
163    void MinioninLiike(PhysicsObject minioni)
164    {
165        minioni.Angle = (Pelaaja[1].Position - minioni.Position).Angle;
166        Vector q = Vector.FromLengthAndAngle(5.0, minioni.Angle);
167        minioni.Hit(q);
168        return;
169    }
170
171    void AsetaNappaimet()
172    {
173        Keyboard.Listen(Key.Up, ButtonState.Down, AlkuKiihdytys, null, Pelaaja[0]);
174        Keyboard.Listen(Key.Up, ButtonState.Down, Kiihdyta, null, Pelaaja[0]);
175        Keyboard.Listen(Key.Down, ButtonState.Down, Hidasta, null, Pelaaja[0]);
176        Keyboard.Listen(Key.Left, ButtonState.Down, kaannaVasemmalle, null, Pelaaja[0]);
177        Keyboard.Listen(Key.Right, ButtonState.Down, kaannaOikealle, null, Pelaaja[0]);
178       
179        Keyboard.Listen(Key.W, ButtonState.Down, AlkuKiihdytys, null, Pelaaja[1]);
180        Keyboard.Listen(Key.W, ButtonState.Down, Kiihdyta, null, Pelaaja[1]);
181        Keyboard.Listen(Key.S, ButtonState.Down, Hidasta, null, Pelaaja[1]);
182        Keyboard.Listen(Key.A, ButtonState.Down, kaannaVasemmalle, null, Pelaaja[1]);
183        Keyboard.Listen(Key.D, ButtonState.Down, kaannaOikealle, null, Pelaaja[1]);
184    }
185
186    void AlkuKiihdytys(PhysicsObject pelaaja)
187    {
188        if (pelaaja.Velocity.Magnitude < 70)
189        {
190            Vector o = Vector.FromLengthAndAngle(50.0, pelaaja.Angle);
191            pelaaja.Hit(o);
192        }
193    }
194
195    void Kiihdyta(PhysicsObject pelaaja)
196    {
197        Vector z = Vector.FromLengthAndAngle(5.0, pelaaja.Angle);
198        pelaaja.Hit(z);
199    }
200
201    void Hidasta(PhysicsObject pelaaja)
202    {
203        pelaaja.Velocity = new Vector(pelaaja.Velocity.X * 0.95, pelaaja.Velocity.Y * 0.95);
204    }
205
206    void kaannaVasemmalle(PhysicsObject pelaaja)
207    {
208        pelaaja.Angle = Angle.FromDegrees(pelaaja.Angle.Degrees + 4);
209    }
210
211    void kaannaOikealle(PhysicsObject pelaaja)
212    {
213        pelaaja.Angle = Angle.FromDegrees(pelaaja.Angle.Degrees - 4);
214    }
215
216    #region XNA / ALTK-Based Code
217
218    private void HandleCursor(AnalogState analogState)
219    {
220        Kursori.AbsolutePosition = new Vector(Kursori.Position.X + analogState.MouseMovement.X, Kursori.Position.Y + analogState.MouseMovement.Y);
221    }
222
223    protected override void Initialize()
224    {
225        base.Initialize();
226
227        InitALTK();
228    }
229
230    protected override void Update(Microsoft.Xna.Framework.GameTime gameTime)
231    {
232        base.Update(gameTime);
233
234        for (int minioniId = 0; minioniId < Minioni.Count; minioniId++)
235        {
236            MinioninLiike(Minioni[minioniId]);
237        }
238
239        //ALTKHandler.MessageDisplay.AddItem("Trololollolollololoo, lololololooo...");
240
241        if (Kursori.Position.X > ALTKConstants.WindowWidth / 2.0)
242            Kursori.MoveTo(new Vector(ALTKConstants.WindowWidth / 2.0, Kursori.Position.Y), 1000000.0);
243        else if (Kursori.Position.X < ALTKConstants.WindowWidth / -2.0)
244            Kursori.MoveTo(new Vector(ALTKConstants.WindowWidth / -2.0, Kursori.Position.Y), 1000000.0);
245
246        if (Kursori.Position.Y > ALTKConstants.WindowHeight / 2.0)
247            Kursori.MoveTo(new Vector(Kursori.Position.X, ALTKConstants.WindowHeight / 2.0), 1000000.0);
248        else if (Kursori.Position.Y < ALTKConstants.WindowHeight / -2.0)
249            Kursori.MoveTo(new Vector(Kursori.Position.X, ALTKConstants.WindowHeight / -2.0), 1000000.0);
250
251        ALTK.Cursor.SecPosX = Convert.ToInt32((ALTKConstants.WindowWidth / 2) + Kursori.Position.X);
252        ALTK.Cursor.SecPosY = Convert.ToInt32(ALTK.ALTKConstants.WindowHeight - ((ALTKConstants.WindowHeight / 2) + Kursori.Position.Y));
253
254        //ALTK.Cursor.PosX = Convert.ToInt32((ALTKConstants.WindowWidth / 2) + Kursori.Position.X);
255        //ALTK.Cursor.PosY = ALTK.Cursor
256
257        //MessageDisplay.Clear();
258        //MessageDisplay.Add(Convert.ToString(ALTK.Cursor.SecPosX + "; " + ALTK.Cursor.SecPosY));
259
260        //if (Keyboard.GetKeyState(Key.Up) == ButtonState.Down)
261        //{
262        //    Vector impulssi = Vector.FromLengthAndAngle(2.0, po.Angle);
263        //    po.Hit(impulssi);
264        //}
265
266        //if (Keyboard.GetKeyState(Key.Left) == ButtonState.Down)
267        //{
268        //    po.Angle = Angle.FromDegrees(po.Angle.Degrees - 5.0);
269        //}
270
271        //if (Keyboard.GetKeyState(Key.Right) == ButtonState.Down)
272        //{
273        //    po.Angle = Angle.FromDegrees(po.Angle.Degrees + 5.0);
274        //}
275
276        //if (Keyboard.GetKeyState(Key.Down) == ButtonState.Down)
277        //{
278        //    if (po.Velocity.Angle == po.Angle)
279        //    {
280        //        Vector impulssi = Vector.FromLengthAndAngle(-2.0, po.Angle);
281        //        po.Hit(impulssi);
282        //    }
283        //}
284
285        if (ALTK.GameHandler.DoUpdate)
286        {
287        }
288        else
289        {
290            //PhysicsEnabled = false;
291        }
292    }
293
294    /// <summary>
295    /// Initializes the ALTKEngine components used by the game. -Rampastring
296    /// </summary>
297    private void InitALTK()
298    {
299        ALTKConstants.WindowWidth = this.Window.ClientBounds.Width;
300        ALTKConstants.WindowHeight = this.Window.ClientBounds.Height;
301
302        altkHandler = new ALTKHandler(this);
303        altkHandler.Initialize();
304        this.Components.Add(altkHandler);
305
306        for (int a = 1; a < 5; a++)
307        {
308            ALTKHandler.StaticAnimHandler.AddStaticAnim(a, new Microsoft.Xna.Framework.Vector2(0, 0));
309        }
310    }
311
312    private void ShowExit()
313    {
314        //this.StopAll();
315        if (!ALTKHandler.ExitWindow.Enabled)
316        {
317            ALTKHandler.ExitWindow.Enabled = true;
318            ALTKHandler.ExitWindow.Visible = true;
319
320            Kursori.IsVisible = true;
321        }
322    }
323
324    #endregion
325}
Note: See TracBrowser for help on using the repository browser.