source: 2016/23/ohjaajat/Punasininen/Punasininen/Punasininen/Punasininen.cs @ 7203

Revision 7203, 6.6 KB checked in by jotapoti, 7 years ago (diff)

ampuminen värjää kenttää & peli ei kaadu, jos yrittää ampua ilman asetta

Line 
1using System;
2using System.Collections.Generic;
3using Jypeli;
4using Jypeli.Assets;
5using Jypeli.Controls;
6using Jypeli.Effects;
7using Jypeli.Widgets;
8
9public class Punasininen : PhysicsGame
10{
11    private const double SPEED = 450;
12    private const double JUMPSPEED = 1250;
13    private const int TILE_SIZE = 40;
14
15    private Player blue;
16    private Player red; 
17    private Image bluepic;
18    private Image redpic;
19
20    private Image pistolpic;
21
22    DoubleMeter percentageTracker;
23
24    private Shader shader;
25
26    public override void Begin()
27    {
28        IsMouseVisible = true; // <- voi ottaa pois
29        shader = new Shader(GraphicsDevice, Content, Camera);
30
31        //Gravity = new Vector(0, -1000);
32        CreateLevel();
33        AddControls();
34    }
35
36    void CreateLevel()
37    {
38        Gravity = new Vector(0, -1000);
39
40        ColorTileMap map = ColorTileMap.FromLevelAsset("dungeon1");
41        map.SetTileMethod(Color.Black, LisaaTaso);
42        map.SetTileMethod(Color.FromHexCode("#FF0026FF"), delegate(Vector paikka, double leveys, double korkeus) { blue = CreatePlayer(paikka, leveys, korkeus, bluepic, Color.Blue);});
43        map.SetTileMethod(Color.FromHexCode("FFD800"), CreateWeaponCrate);
44        map.SetTileMethod(Color.Red, delegate(Vector paikka, double leveys, double korkeus) { red = CreatePlayer(paikka, leveys, korkeus, redpic, Color.Red); });
45        map.Execute(TILE_SIZE, TILE_SIZE);
46
47        Camera.ZoomToAllObjects();
48
49        Level.Background.Color = Color.Black;
50
51        percentageTracker = new DoubleMeter(0, 0, 100);
52
53        ProgressBar percentageBar = new ProgressBar(Level.Width / 2, 20) { Y = Screen.Top - 20, BarColor = red.Color, Color = blue.Color };
54        percentageBar.Y = Screen.Top - 20;
55        percentageBar.BindTo(percentageTracker);
56        Add(percentageBar);
57    }
58
59    void CreateWeaponCrate(Vector place, double width, double height)
60    {
61        WeaponCrate crate = new WeaponCrate(width, height);
62        crate.Position = place;
63        crate.Color = Color.DarkGray;
64        Add(crate);
65    }
66
67    void LisaaTaso(Vector paikka, double leveys, double korkeus)
68    {
69        PhysicsObject platform = PhysicsObject.CreateStaticObject(leveys, korkeus);
70        platform.Position = paikka;
71        platform.Color = Color.Black;
72        platform.Tag = "platform";
73        Add(platform);
74    }
75
76    Player CreatePlayer(Vector paikka, double leveys, double korkeus, Image playerspic, Color playersColor)
77    {
78        Player player = new Player(leveys, korkeus, playerspic, playersColor);
79        player.Position = paikka;
80        Add(player);
81
82        AddCollisionHandler(player, "platform", delegate(PhysicsObject a, PhysicsObject b)
83        {
84            ColorTile(a, b);
85        });
86        AddCollisionHandler(player, "crate", delegate(PhysicsObject a, PhysicsObject b)
87        {
88            //((Player)a).Weapon = ((WeaponCrate)b).GiveWeapon();
89            ((Player)a).Weapon = GunLottery();
90            ((Player)a).Weapon.ProjectileCollision = BulletHitsSomething;
91            b.Destroy();
92        });
93
94        return player;
95    }
96
97    void BulletHitsSomething (PhysicsObject bullet, PhysicsObject target)
98    {
99        if (target.Tag == "platform")
100        {
101            // TODO bullet must know its owner
102            if (bullet.Color == Color.Blue)
103            {
104                ColorTile(blue, target);
105            }
106            else if (bullet.Color == Color.Red)
107            {
108                ColorTile(red, target);
109            }
110        }
111        bullet.Destroy();
112    }
113
114    Weapon GunLottery()
115    {
116        return new AssaultRifle(50, 50);
117    }
118
119    void Win(Player player)
120    {
121       Pause();
122        //Haluaisin tehdä tähän jonkinlaisen hauskan ponnahdusefektin jossain vaiheessa. Sellaisen napakan zoomin ja sitten boing. Hurdur.
123       Camera.ZoomTo(player.Left, player.Bottom, player.Right, player.Top + 100);
124
125        Label announcement = new Label("Kiva homma hei")
126        {
127            TextColor = player.Color,
128            Position = Camera.WorldToScreen(player.Position + new Vector(0, 40)),
129            TextScale = new Vector(2, 2)
130        };
131        Add(announcement);
132
133       
134    }
135
136    void ColorTile(PhysicsObject player, PhysicsObject platform)
137    {
138        platform.Color = player.Color;
139
140        List<GameObject> colored = GetObjects(o => (o.Color == red.Color || o.Color == blue.Color) && (String)o.Tag == "platform");
141        percentageTracker.Value = (double)colored.FindAll(o => o.Color == red.Color).Count/colored.Count * 100;
142    }
143
144    void AddControls()
145    {
146        ControllerOne.Listen(Button.Back, ButtonState.Pressed, Exit, "Poistu pelistä");
147
148        ControllerOne.ListenAnalog(AnalogControl.LeftStick, 0.1, Move, "Liikuta pelaajaa", red);
149        ControllerOne.Listen(Button.A, ButtonState.Pressed, Jump, "Pelaaja hyppää", red, JUMPSPEED);
150        ControllerOne.ListenAnalog(AnalogControl.RightStick, 0.1, Aim, "Tähtää", red);
151        ControllerOne.ListenAnalog(AnalogControl.RightTrigger, 0.1, Shoot, "", red);
152
153        ControllerTwo.ListenAnalog(AnalogControl.LeftStick, 0.1, Move, "Liikuta pelaajaa", blue);
154        ControllerTwo.Listen(Button.A, ButtonState.Pressed, Jump, "Pelaaja hyppää", blue, JUMPSPEED);
155        ControllerOne.ListenAnalog(AnalogControl.RightStick, 0.1, Aim, null, blue);
156        ControllerTwo.ListenAnalog(AnalogControl.RightTrigger, 0.1, Shoot, "", blue);
157
158        Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Ohjeet");
159        Keyboard.Listen(Key.Escape, ButtonState.Pressed, Exit, "Lopeta");
160
161        Mouse.Listen(MouseButton.Left, ButtonState.Pressed, () => shader.NewWave(Mouse.PositionOnWorld), null);
162    }
163
164    void Move(AnalogState stick, Player player)
165    {
166        if (stick.StateVector.Magnitude > 0.15)
167            player.Walk(stick.StateVector.X > 0 ? Direction.Right : Direction.Left);
168    }
169
170    void Shoot(AnalogState trigger, Player player)
171    {
172        if (player.Weapon != null)
173        {
174            PhysicsObject bullet = player.Weapon.Shoot();
175            if (bullet != null)
176            {
177                bullet.Color = player.Color; 
178            }
179        }
180    }
181
182    void Jump(Player player, double speed)
183    {
184        player.Jump(speed);
185    }
186
187    void Aim(AnalogState tatinTila, Player player)
188    {
189
190    }
191
192    protected override void Draw(Microsoft.Xna.Framework.GameTime gameTime)
193    {
194        //shader.Draw(gameTime, base.Draw);
195
196        shader.Draw(gameTime);
197        base.Draw(gameTime);
198        shader.DrawEnd(gameTime);
199    }
200}
Note: See TracBrowser for help on using the repository browser.