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

Revision 7198, 6.4 KB checked in by sieerinn, 3 years ago (diff)

Varjostinohjelmakikkailua

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        IsFullScreen = true;
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, new Vector(-Screen.Width / 2 + 50, Screen.Height / 2 - 50)); });
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, new Vector(Screen.Width / 2 - 50, Screen.Height / 2 - 50)); });
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        PhysicsObject crate = PhysicsObject.CreateStaticObject(width, height);
62        crate.Position = place;
63        Add(crate);
64    }
65
66    void LisaaTaso(Vector paikka, double leveys, double korkeus)
67    {
68        PhysicsObject platform = PhysicsObject.CreateStaticObject(leveys, korkeus);
69        platform.Position = paikka;
70        platform.Color = Color.Black;
71        platform.Tag = "platform";
72        Add(platform);
73    }
74
75    Player CreatePlayer(Vector paikka, double leveys, double korkeus, Image playerspic, Color playersColor, Vector trackerPosition)
76    {
77        Player player = new Player(leveys, korkeus, playerspic, playersColor);
78        player.Position = paikka;
79        Add(player);
80
81        AddCollisionHandler(player, "platform", delegate(PhysicsObject a, PhysicsObject b)
82        {
83            ColorTile(a, b, percentageTracker);
84        });
85
86    /*    Label percentageLabel = new Label() { TextColor = playersColor, DoubleFormatString = "{0:0.0}%", Position = trackerPosition};
87        Add(percentageLabel);
88
89        DoubleMeter percentageTracker = new DoubleMeter(0, 0, 100);
90        percentageLabel.BindTo(percentageTracker);
91
92        percentageTracker.AddTrigger(30, TriggerDirection.Up, delegate() { Win(player); });
93
94        AddCollisionHandler(player, "platform", delegate(PhysicsObject a, PhysicsObject b)
95        {
96            ColorTile(a, b, percentageTracker);
97        });
98     * */
99
100        return player;
101    }
102
103    void Win(Player player)
104    {
105        Pause();
106        //Haluaisin tehdä tähän jonkinlaisen hauskan ponnahdusefektin jossain vaiheessa. Sellaisen napakan zoomin ja sitten boing. Hurdur.
107       Camera.ZoomTo(player.Left, player.Bottom, player.Right, player.Top + 100);
108
109        Label announcement = new Label("Kiva homma hei")
110        {
111            TextColor = player.Color,
112            Position = Camera.WorldToScreen(player.Position + new Vector(0, 40)),
113            TextScale = new Vector(2, 2)
114        };
115        Add(announcement);
116
117       
118    }
119
120    void ColorTile(PhysicsObject player, PhysicsObject platform, DoubleMeter tracker)
121    {
122        platform.Color = player.Color;
123
124        List<GameObject> colored = GetObjects(o => (o.Color == red.Color || o.Color == blue.Color) && (String)o.Tag == "platform");
125        tracker.Value = (double)colored.FindAll(o => o.Color == red.Color).Count/colored.Count * 100;
126
127        //tracker.Value = (double)GetObjects(o => o.Color == player.Color && (String)o.Tag == "platform").Count / GetObjects(o => o.Color == player.Color || o.Color == blue.Color && (String)o.Tag == "platform").Count * 100.0;
128        //tracker.Value = (double)GetObjects(o => o.Color == player.Color && (String)o.Tag == "colored").Count / GetObjectsWithTag("platform").Count * 100.0;
129    }
130
131    void AddControls()
132    {
133        ControllerOne.Listen(Button.Back, ButtonState.Pressed, Exit, "Poistu pelistä");
134
135        ControllerOne.ListenAnalog(AnalogControl.LeftStick, 0.1, Move, "Liikuta pelaajaa", red);
136        ControllerOne.Listen(Button.A, ButtonState.Pressed, Jump, "Pelaaja hyppää", red, JUMPSPEED);
137        ControllerOne.ListenAnalog(AnalogControl.RightStick, 0.1, Aim, "Tähtää", red);
138
139        ControllerTwo.ListenAnalog(AnalogControl.LeftStick, 0.1, Move, "Liikuta pelaajaa", blue);
140        ControllerTwo.Listen(Button.A, ButtonState.Pressed, Jump, "Pelaaja hyppää", blue, JUMPSPEED);
141        ControllerOne.ListenAnalog(AnalogControl.RightStick, 0.1, Aim, null, blue);
142
143        Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Ohjeet");
144        Keyboard.Listen(Key.Escape, ButtonState.Pressed, Exit, "Lopeta");
145
146        Mouse.Listen(MouseButton.Left, ButtonState.Pressed, () => shader.NewWave(Mouse.PositionOnWorld), null);
147    }
148
149    void Move(AnalogState stick, Player player)
150    {
151        //player.Walk(stick.StateVector.X * 500);
152       
153        if (stick.StateVector.Magnitude > 0.15)
154            player.Walk(stick.StateVector.X > 0 ? Direction.Right : Direction.Left);
155    }
156
157    void Jump(Player player, double speed)
158    {
159        player.Jump(speed);
160    }
161
162    void Aim(AnalogState tatinTila, Player player)
163    {
164
165    }
166
167    protected override void Draw(Microsoft.Xna.Framework.GameTime gameTime)
168    {
169        //shader.Draw(gameTime, base.Draw);
170
171        shader.Draw(gameTime);
172        base.Draw(gameTime);
173        shader.DrawEnd(gameTime);
174    }
175}
Note: See TracBrowser for help on using the repository browser.