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

Revision 7199, 5.4 KB checked in by empaheik, 3 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 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
18    private Image bluepic;
19    private Image redpic;
20    DoubleMeter percentageTracker;
21
22    private Shader shader;
23
24    public override void Begin()
25    {
26        IsMouseVisible = true; // <- voi ottaa pois
27        shader = new Shader(GraphicsDevice, Content, Camera);
28
29        //Gravity = new Vector(0, -1000);
30        CreateLevel();
31        AddControls();
32    }
33
34    void CreateLevel()
35    {
36        Gravity = new Vector(0, -1000);
37
38        ColorTileMap map = ColorTileMap.FromLevelAsset("dungeon1");
39        map.SetTileMethod(Color.Black, LisaaTaso);
40        map.SetTileMethod(Color.FromHexCode("#FF0026FF"), delegate(Vector paikka, double leveys, double korkeus) { blue = CreatePlayer(paikka, leveys, korkeus, bluepic, Color.Blue, new Vector(-Screen.Width / 2 + 50, Screen.Height / 2 - 50)); });
41        map.SetTileMethod(Color.FromHexCode("FFD800"), CreateWeaponCrate);
42        map.SetTileMethod(Color.Red, delegate(Vector paikka, double leveys, double korkeus) { red = CreatePlayer(paikka, leveys, korkeus, redpic, Color.Red, new Vector(Screen.Width / 2 - 50, Screen.Height / 2 - 50)); });
43        map.Execute(TILE_SIZE, TILE_SIZE);
44
45        Camera.ZoomToAllObjects();
46
47        Level.Background.Color = Color.Black;
48
49        percentageTracker = new DoubleMeter(0, 0, 100);
50
51        ProgressBar percentageBar = new ProgressBar(Level.Width / 2, 20) { Y = Screen.Top - 20, BarColor = red.Color, Color = blue.Color };
52        percentageBar.Y = Screen.Top - 20;
53        percentageBar.BindTo(percentageTracker);
54        Add(percentageBar);
55    }
56
57    void CreateWeaponCrate(Vector place, double width, double height)
58    {
59        PhysicsObject crate = PhysicsObject.CreateStaticObject(width, height);
60        crate.Position = place;
61        Add(crate);
62    }
63
64    void LisaaTaso(Vector paikka, double leveys, double korkeus)
65    {
66        PhysicsObject platform = PhysicsObject.CreateStaticObject(leveys, korkeus);
67        platform.Position = paikka;
68        platform.Color = Color.Black;
69        platform.Tag = "platform";
70        Add(platform);
71    }
72
73    Player CreatePlayer(Vector paikka, double leveys, double korkeus, Image playerspic, Color playersColor, Vector trackerPosition)
74    {
75        Player player = new Player(leveys, korkeus, playerspic, playersColor);
76        player.Position = paikka;
77        Add(player);
78
79        AddCollisionHandler(player, "platform", delegate(PhysicsObject a, PhysicsObject b)
80        {
81            ColorTile(a, b, percentageTracker);
82        });
83
84        return player;
85    }
86
87    void Win(Player player)
88    {
89        Pause();
90        //Haluaisin tehdä tähän jonkinlaisen hauskan ponnahdusefektin jossain vaiheessa. Sellaisen napakan zoomin ja sitten boing. Hurdur.
91       Camera.ZoomTo(player.Left, player.Bottom, player.Right, player.Top + 100);
92
93        Label announcement = new Label("Kiva homma hei")
94        {
95            TextColor = player.Color,
96            Position = Camera.WorldToScreen(player.Position + new Vector(0, 40)),
97            TextScale = new Vector(2, 2)
98        };
99        Add(announcement);
100
101       
102    }
103
104    void ColorTile(PhysicsObject player, PhysicsObject platform, DoubleMeter tracker)
105    {
106        platform.Color = player.Color;
107
108        List<GameObject> colored = GetObjects(o => (o.Color == red.Color || o.Color == blue.Color) && (String)o.Tag == "platform");
109        tracker.Value = (double)colored.FindAll(o => o.Color == red.Color).Count/colored.Count * 100;
110    }
111
112    void AddControls()
113    {
114        ControllerOne.Listen(Button.Back, ButtonState.Pressed, Exit, "Poistu pelistä");
115
116        ControllerOne.ListenAnalog(AnalogControl.LeftStick, 0.1, Move, "Liikuta pelaajaa", red);
117        ControllerOne.Listen(Button.A, ButtonState.Pressed, Jump, "Pelaaja hyppää", red, JUMPSPEED);
118        ControllerOne.ListenAnalog(AnalogControl.RightStick, 0.1, Aim, "Tähtää", red);
119
120        ControllerTwo.ListenAnalog(AnalogControl.LeftStick, 0.1, Move, "Liikuta pelaajaa", blue);
121        ControllerTwo.Listen(Button.A, ButtonState.Pressed, Jump, "Pelaaja hyppää", blue, JUMPSPEED);
122        ControllerOne.ListenAnalog(AnalogControl.RightStick, 0.1, Aim, null, blue);
123
124        Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Ohjeet");
125        Keyboard.Listen(Key.Escape, ButtonState.Pressed, Exit, "Lopeta");
126
127        Mouse.Listen(MouseButton.Left, ButtonState.Pressed, () => shader.NewWave(Mouse.PositionOnWorld), null);
128    }
129
130    void Move(AnalogState stick, Player player)
131    {
132        if (stick.StateVector.Magnitude > 0.15)
133            player.Walk(stick.StateVector.X > 0 ? Direction.Right : Direction.Left);
134    }
135
136    void Jump(Player player, double speed)
137    {
138        player.Jump(speed);
139    }
140
141    void Aim(AnalogState tatinTila, Player player)
142    {
143
144    }
145
146    protected override void Draw(Microsoft.Xna.Framework.GameTime gameTime)
147    {
148        //shader.Draw(gameTime, base.Draw);
149
150        shader.Draw(gameTime);
151        base.Draw(gameTime);
152        shader.DrawEnd(gameTime);
153    }
154}
Note: See TracBrowser for help on using the repository browser.