source: 2015/24/TuroR/Omapeli/Omapeli/Omapeli/Omapeli.cs @ 5997

Revision 5997, 5.0 KB checked in by iisaaira, 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;
8
9public class Omapeli : PhysicsGame
10{
11    Image taustaKuva = LoadImage("");
12
13    Image ukkoAmpuu = LoadImage("ukko ampuu2");
14
15    IntMeter pisteLaskuri;
16    IntMeter ammusLaskuri;
17
18    Image panoksenKuva = LoadImage("panos");
19
20    Image[] pallonKuvat = LoadImages("apina", "UUS APINA", "sukkamato", "silma", "kännykkä", "Cannon", "possu", "tamm", "putin", "pall o o");
21
22    AssaultRifle pelaajan1Ase;
23
24    Image ukonKuva = LoadImage("ukko");
25
26
27
28    public override void Begin()
29    {
30       
31
32        LuoUusiApina();
33
34        Gravity = new Vector(0, -1000);
35
36        //tykki.ProjectileCollision = AmmusOsui;
37
38        IsFullScreen = false;
39        IsMouseVisible = true;
40        Level.Width = Screen.Width;
41        Level.Height = Screen.Height;
42
43        Level.CreateRightBorder(1.0, false);
44        Level.CreateLeftBorder(1.0, false);
45        PhysicsObject ylaReuna = Level.CreateTopBorder(1.0, false);
46       
47
48        Surface alaReuna = Surface.CreateBottom(Level, 30, 100, 40, 10);
49        alaReuna.Y = Screen.Bottom + 100;
50        Add(alaReuna);
51
52       
53
54        PlatformCharacter2 ukko = new PlatformCharacter2(50, 50);
55        ukko.Shape = Shape.Circle;
56        ukko.Mass = 20.0;
57        ukko.Color = Color.LimeGreen;
58        Add(ukko);
59        ukko.Image = ukonKuva;
60
61        pelaajan1Ase = new AssaultRifle(30, 10);
62        pelaajan1Ase.X = 21.0;
63        pelaajan1Ase.Y = -12;
64
65        pelaajan1Ase.Ammo.Value = 100;
66        pelaajan1Ase.ProjectileCollision = AmmusOsui;
67        ukko.Add(pelaajan1Ase);
68
69        LuoPistelaskuri();
70        LuoAmmusLaskuri();
71
72        PhoneBackButton.Listen(ConfirmExit, "Lopeta peli");
73        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli");
74        Mouse.Listen(MouseButton.Left, ButtonState.Down, AmmuAseella, "Ammu",ukko, pelaajan1Ase);
75        Mouse.Listen(MouseButton.Left, ButtonState.Released, AmpumisenLopetus, null, ukko);
76        Keyboard.Listen(Key.W, ButtonState.Down, Hyppääukolla, "Hyppää", ukko);
77        Keyboard.Listen(Key.A, ButtonState.Down, Liiku, "Liiku", ukko, Direction.Left);
78        Keyboard.Listen(Key.D, ButtonState.Down, Liiku, "Liiku", ukko, Direction.Right);
79        Mouse.ListenMovement(0.0, HiirenLiikutus, "Tahtaa", ukko);
80    }
81
82    void AmmusOsui(PhysicsObject ammus, PhysicsObject kohde)
83    {
84        ammus.Destroy();
85
86       
87
88        if (kohde.Tag.Equals("apina"))
89        {
90            Timer.SingleShot(2.0, delegate { LuoUusiApina(); });
91            kohde.Destroy();
92            Explosion rajahdys = new Explosion(1100);
93            rajahdys.Position = ammus.Position;
94            Add(rajahdys);
95
96            pisteLaskuri.Value += 1;
97
98        }
99    }
100
101    void AmmuAseella(PlatformCharacter2 ukko, AssaultRifle ase)
102    {
103        PhysicsObject ammus = ase.Shoot();
104       
105
106        if (ammus != null)
107        {
108            ammusLaskuri.Value += 1; 
109            ammus.Image = panoksenKuva;
110            ammus.Velocity = ammus.AbsoluteAngle.GetVector()*3000;
111            ukko.Image = ukkoAmpuu;
112        }
113    }
114
115    void AmpumisenLopetus(PlatformCharacter2 ukko)
116    {
117        ukko.Image = ukonKuva;   
118    }
119
120
121    void Hyppääukolla(PlatformCharacter2 ukko)
122    {
123        ukko.ForceJump(500.0);
124    }
125
126    void Liiku(PlatformCharacter2 ukko, Direction suunta)
127    {
128        ukko.Walk(suunta);
129        pelaajan1Ase.X = 21.0*suunta.GetVector().X;
130    }
131
132    void HiirenLiikutus(AnalogState Hiiri, PlatformCharacter2 ukko)
133    {
134        pelaajan1Ase.AbsoluteAngle = (Mouse.PositionOnWorld - ukko.Position).Angle;
135
136    }
137
138    void LuoUusiApina()
139    {
140        PhysicsObject apina = new PhysicsObject(60, 60);
141        apina.Shape = Shape.Circle;
142        apina.IgnoresGravity = true;
143        apina.Hit(new Vector(-500, 200));
144
145        Double Xkoordinaatti = RandomGen.NextDouble(Level.Left, Level.Right);
146        apina.Position = new Vector(Xkoordinaatti,0);
147        apina.Restitution = 1.0;
148        apina.Tag = "apina";
149        apina.Image = pallonKuvat[RandomGen.NextInt(pallonKuvat.Length)];
150        Add(apina);
151    }
152
153    void LuoPistelaskuri()
154    {
155        pisteLaskuri = new IntMeter(0);
156
157        Label pisteNaytto = new Label();
158        pisteNaytto.X = Screen.Left + 100;
159        pisteNaytto.Y = Screen.Top - 100;
160        pisteNaytto.TextColor = Color.Black;
161        pisteNaytto.Color = Color.White;
162
163        pisteNaytto.BindTo(pisteLaskuri);
164        Add(pisteNaytto);
165    }
166
167    void LuoAmmusLaskuri()
168    {
169        ammusLaskuri = new IntMeter(0);
170
171        Label ammusNaytto = new Label();
172        ammusNaytto.X = Screen.Right - 100;
173        ammusNaytto.Y = Screen.Top - 100;
174        ammusNaytto.TextColor = Color.Black;
175        ammusNaytto.Color = Color.White;
176
177        ammusNaytto.BindTo(ammusLaskuri);
178        Add(ammusNaytto); 
179    }
180}
Note: See TracBrowser for help on using the repository browser.