source: 2012/23/JimB/NameInPending/NameinPending/NameinPending/NameinPending/NameinPending.cs @ 2806

Revision 2806, 6.6 KB checked in by jijoback, 11 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 NameinPending : PhysicsGame
10{
11
12    double ampumisnopeus = 500;
13
14    public override void Begin()
15    {
16        // TODO: Kirjoita ohjelmakoodisi tähän
17        LuoKentta();
18
19
20
21
22
23
24        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli");
25    }
26    void LuoKentta()
27    {
28        TileMap ruudut = TileMap.FromLevelAsset("level");
29        //TileMap pelaaja = TileMap.FromLevelAsset("level");
30        ruudut.SetTileMethod('=', LuoPalikka);
31        ruudut.SetTileMethod('s', LuoPalikka);
32        ruudut.SetTileMethod('1', LuoPelaaja1);
33        ruudut.SetTileMethod('2', LuoPelaaja2);
34        ruudut.SetTileMethod('3', LuoPelaaja3);
35        ruudut.SetTileMethod('4', LuoPelaaja4);
36        ruudut.Execute(25, 25);
37
38        Level.BackgroundColor = new Color(96, 38,0);
39        Camera.ZoomToLevel();
40    }
41
42    void LuoPelaaja1(Vector paikka, double leveys, double korkeus)
43    {
44
45        PhysicsObject pelaaja1 = LuoPelaaja(paikka);
46        pelaaja1.Image = LoadImage("Pelaaja1");
47        pelaaja1.CollisionIgnoreGroup = 1;
48
49        Keyboard.Listen(Key.A, ButtonState.Down, Liike,null,pelaaja1, new Vector(-200,0));
50        Keyboard.Listen(Key.A, ButtonState.Released, Liike, null, pelaaja1, new Vector(0, 0));
51        Keyboard.Listen(Key.D, ButtonState.Down, Liike, null, pelaaja1, new Vector(200, 0));
52        Keyboard.Listen(Key.D, ButtonState.Released, Liike, null, pelaaja1, new Vector(0, 0));
53        Keyboard.Listen(Key.S, ButtonState.Down, Liike, null, pelaaja1, new Vector(0, -200));
54        Keyboard.Listen(Key.S, ButtonState.Released, Liike, null, pelaaja1, new Vector(0, 0));
55        Keyboard.Listen(Key.W, ButtonState.Down, Liike, null, pelaaja1, new Vector(0, 200));
56        Keyboard.Listen(Key.W, ButtonState.Released, Liike, null, pelaaja1, new Vector(0, 0));
57        Keyboard.Listen(Key.Q, ButtonState.Down, Ammu, null, pelaaja1);       
58    }
59    void LuoPelaaja2(Vector paikka, double leveys, double korkeus)
60    {
61       
62        PhysicsObject pelaaja2 = LuoPelaaja(paikka);
63        pelaaja2.Image = LoadImage("Pelaaja2");
64        pelaaja2.CollisionIgnoreGroup = 2;
65
66        Keyboard.Listen(Key.Left, ButtonState.Down, Liike, null, pelaaja2, new Vector(-200, 0));
67        Keyboard.Listen(Key.Left, ButtonState.Released, Liike, null, pelaaja2, new Vector(0, 0));
68        Keyboard.Listen(Key.Right, ButtonState.Down, Liike, null, pelaaja2, new Vector(200, 0));
69        Keyboard.Listen(Key.Right, ButtonState.Released, Liike, null, pelaaja2, new Vector(0, 0));
70        Keyboard.Listen(Key.Down, ButtonState.Down, Liike, null, pelaaja2, new Vector(0, -200));
71        Keyboard.Listen(Key.Down, ButtonState.Released, Liike, null, pelaaja2, new Vector(0, 0));
72        Keyboard.Listen(Key.Up, ButtonState.Down, Liike, null, pelaaja2, new Vector(0, 200));
73        Keyboard.Listen(Key.Up, ButtonState.Released, Liike, null, pelaaja2, new Vector(0, 0));
74        Keyboard.Listen(Key.NumPad0, ButtonState.Down, Ammu, null, pelaaja2); 
75    }
76    void LuoPelaaja3(Vector paikka, double leveys, double korkeus)
77    {
78
79        PhysicsObject pelaaja3 = LuoPelaaja(paikka);
80        pelaaja3.Image = LoadImage("Pelaaja3");
81        pelaaja3.CollisionIgnoreGroup = 3;
82
83        Keyboard.Listen(Key.F, ButtonState.Down, Liike, null, pelaaja3, new Vector(-200, 0));
84        Keyboard.Listen(Key.F, ButtonState.Released, Liike, null, pelaaja3, new Vector(0, 0));
85        Keyboard.Listen(Key.H, ButtonState.Down, Liike, null, pelaaja3, new Vector(200, 0));
86        Keyboard.Listen(Key.H, ButtonState.Released, Liike, null, pelaaja3, new Vector(0, 0));
87        Keyboard.Listen(Key.G, ButtonState.Down, Liike, null, pelaaja3, new Vector(0, -200));
88        Keyboard.Listen(Key.G, ButtonState.Released, Liike, null, pelaaja3, new Vector(0, 0));
89        Keyboard.Listen(Key.T, ButtonState.Down, Liike, null, pelaaja3, new Vector(0, 200));
90        Keyboard.Listen(Key.T, ButtonState.Released, Liike, null, pelaaja3, new Vector(0, 0));
91        Keyboard.Listen(Key.R, ButtonState.Down, Ammu, null, pelaaja3); 
92    }
93    void LuoPelaaja4(Vector paikka, double leveys, double korkeus)
94    {
95
96        PhysicsObject pelaaja4 = LuoPelaaja(paikka);
97        pelaaja4.Image = LoadImage("Pelaaja4");
98        pelaaja4.CollisionIgnoreGroup = 4;
99
100        Keyboard.Listen(Key.J, ButtonState.Down, Liike, null, pelaaja4, new Vector(-200, 0));
101        Keyboard.Listen(Key.J, ButtonState.Released, Liike, null, pelaaja4, new Vector(0, 0));
102        Keyboard.Listen(Key.L, ButtonState.Down, Liike, null, pelaaja4, new Vector(200, 0));
103        Keyboard.Listen(Key.L, ButtonState.Released, Liike, null, pelaaja4, new Vector(0, 0));
104        Keyboard.Listen(Key.K, ButtonState.Down, Liike, null, pelaaja4, new Vector(0, -200));
105        Keyboard.Listen(Key.K, ButtonState.Released, Liike, null, pelaaja4, new Vector(0, 0));
106        Keyboard.Listen(Key.I, ButtonState.Down, Liike, null, pelaaja4, new Vector(0, 200));
107        Keyboard.Listen(Key.I, ButtonState.Released, Liike, null, pelaaja4, new Vector(0, 0));
108        Keyboard.Listen(Key.U, ButtonState.Down, Ammu, null, pelaaja4); 
109    }
110    PhysicsObject LuoPelaaja(Vector paikka)
111    {
112        PhysicsObject Pelaaja = new PhysicsObject(18, 18);
113        Pelaaja.Restitution = 0.01;
114        Pelaaja.AngularDamping = 1;
115        Pelaaja.Position = paikka;
116        Pelaaja.Shape = Shape.Circle;
117        Add(Pelaaja);
118        return Pelaaja;
119
120    }
121    void Ammu(PhysicsObject pelaaja)
122    {
123        PhysicsObject bullet = new PhysicsObject(LoadImage("bullet"));
124        Add(bullet);
125        // AddCollisionHandler(bullet, CollisionHandler.ExplodeObject(100, false));
126        AddCollisionHandler(bullet, CollisionHandler.DestroyObject);
127        bullet.Position = pelaaja.Position;
128        bullet.Angle = pelaaja.Angle;
129        bullet.CollisionIgnoreGroup = pelaaja.CollisionIgnoreGroup;
130        Vector suunta = Vector.FromLengthAndAngle(ampumisnopeus, pelaaja.Angle + Angle.RightAngle);
131        bullet.Hit(suunta);
132
133    }
134    void Liike(PhysicsObject pelaaja, Vector nopeus)
135    {
136        pelaaja.Velocity = nopeus;
137        if (nopeus.Magnitude != 0)
138        {
139            pelaaja.Angle = nopeus.Angle - Angle.RightAngle;
140        }
141    }
142    void LuoPalikka(Vector paikka, double leveys, double korkeus)
143    {
144        PhysicsObject palikka = PhysicsObject.CreateStaticObject(leveys, korkeus);
145        palikka.Position = paikka;
146        palikka.Shape = Shape.Rectangle;
147        palikka.Color = Color.White;
148        Add(palikka);
149    }
150}
Note: See TracBrowser for help on using the repository browser.