source: 2014/30/MitjaK/Attack to Agora/Attack to Agora/Attack to Agora/Attack to Agora/Attack_to_Agora.cs @ 6313

Revision 6313, 7.9 KB checked in by anlakane, 8 years ago (diff)
Line 
1using System;
2using System.Collections.Generic;
3using Jypeli;
4using Jypeli.Assets;
5using Jypeli.Controls;
6using Jypeli.Effects;
7using Jypeli.Widgets;
8using Microsoft.Xna.Framework.Content;
9
10public partial class Attack_to_Agora : PhysicsGame
11{
12    private Animation chuckinkavely;
13
14    /// <summary>
15    /// Vakiot kannattaa merkitä constiksi (ei voi muuttaa). Tällöin ne kirjoitetaan isolla.
16    /// </summary>
17    const double NOPEUS = 1000; // 250
18    const double HYPPYVOIMA = 1500;
19
20    Image level1 = LoadImage("kentta1");
21    Image taistelijanvarjo = LoadImage("varjo");
22    Image taistelijaOlio = LoadImage("olio");
23    Image cCaseD = LoadImage("laatikko_opening");
24    Image cCaseF = LoadImage("caseavausruutu");
25
26    PlatformCharacter taistelija;
27
28    int kenttaNro = 1;
29
30    /// <summary>
31    /// Viimeisen pelissä olevan kentän indeksi.
32    /// </summary>
33    const int VIIMEINEN_KENTTA = 15;
34
35    public override void Begin()
36    {
37        LataaContent();
38        LuoKentta();
39        AsetaOhjaimet();
40    }
41
42    void Liikuta(PlatformCharacter pelaaja, double suunta)
43    {
44        pelaaja.Walk(suunta);
45
46        if (!pelaaja.Animation.IsPlaying)
47            pelaaja.Animation.Resume();
48    }
49
50    void LiikutaYlos(PlatformCharacter pelaaja)
51    {
52        pelaaja.Jump(HYPPYVOIMA);
53    }
54
55    void LataaContent()
56    {
57        chuckinkavely = LoadAnimation("pelaajaliikkuu");
58    }
59
60    void LuoKentta()
61    {
62        SmoothTextures = false;
63        IsFullScreen = true;
64        Gravity = new Vector(0, -2500);
65
66        // Synkataan taustakuvan ja colortilemapin pikselit
67        Level.Background.Image = LoadImage("kentta" + kenttaNro.ToString());
68
69        Level.Width = Screen.Width;
70        double kerroin = (double)Level.Background.Image.Width / Level.Background.Image.Height;
71        Level.Height = Level.Width / kerroin;
72        Level.Background.FitToLevel();
73
74        Image pohjaLevynKuva = LoadImage("kentta" + kenttaNro.ToString() + "pohja");
75
76        ColorTileMap pohjaLevy = new ColorTileMap(pohjaLevynKuva);
77        pohjaLevy.SetTileMethod(Color.Gold, LuoPohja);
78        pohjaLevy.SetTileMethod(Color.Cyan, Luotaistelija);
79        pohjaLevy.Optimize(Color.Gold);
80
81        // oletuksena 1 pikseli = 1 unit
82        const double BASE_SIZE = 1.0;
83        double widthMultiplier = Level.Width / pohjaLevynKuva.Width;
84        double heightMultiplier = Level.Height / pohjaLevynKuva.Height;
85        pohjaLevy.Execute(BASE_SIZE * widthMultiplier, BASE_SIZE * heightMultiplier);
86
87        // PhysicsObject pohja = Level.CreateBottomBorder();
88        // pohja.Y = Level.Bottom + 100;
89        //pohja.IsVisible = false;
90        PhysicsObject vasenreuna = Level.CreateLeftBorder();
91        vasenreuna.X -= 75;
92        vasenreuna.Tag = "vasenreuna";
93        //PhysicsObject oikeareuna = Level.CreateRightBorder();
94        //oikeareuna.X += 75;
95        //oikeareuna.Tag = "oikeareuna";
96
97        Varjo();
98    }
99
100    void Luotaistelija(Vector paikka, double leveys, double korkeus)
101    {
102
103        taistelija = new PlatformCharacter(75, 75);
104        taistelija.Shape = Shape.Rectangle;
105        taistelija.Animation = new Animation(chuckinkavely);
106        taistelija.Animation.Start();
107        taistelija.Position = paikka;
108
109        AddCollisionHandler(taistelija, "vasenreuna", SeuraavaKentta);
110
111        taistelija.Mass = 10.0;
112        Add(taistelija);
113        Camera.Follow(taistelija);
114        Camera.StayInLevel = true;
115    }
116
117    void Varjo()
118    {
119        GameObject varjo = new GameObject(70, 30);
120        varjo.Image = taistelijanvarjo;
121        Add(varjo, -1);
122
123        Timer ajastin = new Timer();
124        ajastin.Interval = 1.0 / 60.0;
125        ajastin.Timeout += delegate
126        {
127            varjo.X = taistelija.X;
128            varjo.Y = EtsiVarjonY();
129        };
130        ajastin.Start();
131    }
132
133    /// <summary>
134    /// Etsii suoraan pelaajan alla olevan lattiapalikan Y-koordinaatin.
135    /// </summary>
136    /// <returns></returns>
137    double EtsiVarjonY()
138    {
139        Vector rayDown = new Vector(0.0, -2000.0);
140        List<GameObject> possibleObjects = GetObjects(x => x != taistelija && x is PhysicsObject );
141        List<GameObject> objectsBelow = new List<GameObject>();
142
143        for (int i = 0; i < possibleObjects.Count; i++)
144        {
145            if (possibleObjects[i].IsBlocking(new Vector(taistelija.X, taistelija.Bottom), new Vector(taistelija.X, taistelija.Bottom) + rayDown))
146            {
147                objectsBelow.Add(possibleObjects[i]);
148            }
149        }
150
151        if (objectsBelow.Count == 0)
152            return double.MinValue;
153
154        double minDistance = double.MaxValue;
155        int minIndex = -1;
156
157        for (int i = 0; i < objectsBelow.Count; i++)
158        {
159            double currentDistance = Math.Abs(taistelija.Bottom - objectsBelow[i].Top);
160            if (currentDistance < minDistance)
161            {
162                minDistance = currentDistance;
163                minIndex = i;
164            }
165        }
166
167        return objectsBelow[minIndex].Top;
168    }
169
170
171    void SeuraavaKentta(PhysicsObject tormaaja, PhysicsObject tormatty)
172    {
173        ClearAll();
174        kenttaNro++;
175
176        if (kenttaNro > VIIMEINEN_KENTTA)
177        {
178            VoititPelin();
179        }
180
181        else
182        {
183            LuoKentta();
184            AsetaOhjaimet();
185        }
186    }
187
188    void LuoPohja(Vector paikka, double leveys, double korkeus)
189    {
190        PhysicsObject pala = PhysicsObject.CreateStaticObject(leveys, korkeus);
191        pala.Position = paikka;
192        pala.IsVisible = false;
193        Add(pala);
194    }
195
196    void LuoChromaCase()
197    {
198        Mouse.IsCursorVisible = true;
199
200        List<GameObject> pItems = new List<GameObject>();
201        GameObject knife = new GameObject(LoadImage("rareitem_knife"));
202        knife.Tag = new object[2] {(object)50.0, (object)ItemType.Knife};
203        pItems.Add(knife);
204
205        GameObject avain = new GameObject(LoadImage("rareitem_avain_small"));
206        avain.Tag = new object[2] { (object)50.0, (object)ItemType.Key };
207        pItems.Add(avain);
208       
209
210        //for (int i = 0; i < 40; i++)
211        //{
212        //    GameObject obj = new GameObject(50, 50);
213        //    obj.Color = RandomGen.NextColor();
214        //    obj.Tag = 50.0;
215        //    pItems.Add(obj);
216        //}
217
218        GameObject cCaseDialog = new GameObject(cCaseD); // koko debug
219        Add(cCaseDialog);
220
221        Mouse.ListenOn(cCaseDialog, MouseButton.Left, ButtonState.Pressed, delegate
222        {
223            cCaseDialog.Destroy();
224
225            GameObject cCaseFront = new GameObject(cCaseF);
226
227            ChromaCase cCase = new ChromaCase(pItems, cCaseFront, null, 20);
228            cCase.Add(new Vector(0.0, Level.Bottom + cCaseFront.Height / 2), 2);
229            cCase.ItemReceived += delegate(GameObject o) { GotItemFromCase(o); };
230            cCase.ItemRelativePosition = new Vector((472 - 467.5), (455 - 204));
231            cCase.Start();
232
233        }, null);
234    }
235
236    void GotItemFromCase(GameObject o)
237    {
238        ItemType gotType = (ItemType)((object[])o.Tag)[0];
239
240        switch (gotType)
241        {
242            case ItemType.Knife:
243                break;
244            case ItemType.Key:
245                break;
246            default:
247                break;
248        }
249    }
250
251    /// <summary>
252    /// Asetetaan pelaajalle ohjaimet.
253    /// </summary>
254    void AsetaOhjaimet()
255    {
256        Keyboard.Listen(Key.A, ButtonState.Down, Liikuta, null, taistelija, -NOPEUS);
257        Keyboard.Listen(Key.D, ButtonState.Down, Liikuta, null, taistelija, NOPEUS);
258        Keyboard.Listen(Key.Space, ButtonState.Pressed, LiikutaYlos, null, taistelija);
259        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli");
260
261#if DEBUG
262        Keyboard.Listen(Key.C, ButtonState.Pressed, LuoChromaCase, null);
263#endif
264    }
265
266    void VoititPelin()
267    {
268
269    }
270}
Note: See TracBrowser for help on using the repository browser.