source: 2014/27/AleksanteriV/Protokolla236TrueSurvivor/Protokolla236TrueSurvivor/Protokolla236TrueSurvivor/Protokolla236TrueSurvivor.cs @ 5342

Revision 5342, 5.7 KB checked in by mijoilmo, 9 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 Proto236b : PhysicsGame //true survivor
10{
11    Player player;
12    private ColorTileMap map;
13    public ColorTileMap Map { get { return map; } set { map = value; } }
14    private Dictionary<string, Image> images = new Dictionary<string, Image>();
15    public Dictionary<string, Image> Images { get { return images; } set { images = value; } }
16    private Dictionary<string, Image[]> imageLists = new Dictionary<string, Image[]>();
17    public Dictionary<string, Image[]> ImageLists { get { return imageLists; } set { imageLists = value; } }
18    private ProtoLevel currentLevel;
19    public ProtoLevel CurrentLevel { get { return currentLevel; } set { currentLevel = value; } }
20
21    void AssignKeys()
22    {
23        Keyboard.Listen(Key.Escape, ButtonState.Down, Exit, "Lopeta Peli");
24        Keyboard.Listen(Key.Up, ButtonState.Down, player.thrusterStart, "Lento", 1.0);
25        Keyboard.Listen(Key.Up, ButtonState.Released, player.thrusterEnd, "Lento");
26        Keyboard.Listen(Key.Down, ButtonState.Down, player.thrusterStart, "Lento", -0.2);
27        Keyboard.Listen(Key.Down, ButtonState.Released, player.thrusterEnd, "Lento");
28        Keyboard.Listen(Key.Left, ButtonState.Down, player.rotate, "Lento", 4.0);
29        Keyboard.Listen(Key.Right, ButtonState.Down, player.rotate, "Lento", -4.0);
30    }
31    void LoadAllImages()
32    {
33        images["background0"] = LoadImage("graphics/backgrounds/space_background");
34        images["backgroundmars"] = LoadImage("graphics/backgrounds/Mars");
35        images["backgroundplanet1"] = LoadImage("graphics/backgrounds/planet1");
36        images["player"] = LoadImage("graphics/ships/player");
37        images["tile0"] = LoadImage("graphics/tiles/tile0");
38        imageLists["player_thruster"] = LoadImages("graphics/effects/thrusters/thruster0.1", "graphics/effects/thrusters/thruster0.2");
39        imageLists["enemy1"] = LoadImages("graphics/Enemy/Enemy1.0","graphics/Enemy/Enemy1.1","graphics/Enemy/Enemy1.2");
40    }
41    void LoadLevel(ProtoLevel level)
42    {
43        ClearAll();
44        currentLevel = level;
45        if (level.IsPlanet)
46        {
47            Gravity = new Vector(0, -200);
48            player.LinearDamping = 0.99;//ns ilmanvastus
49        }
50        else {
51            Gravity = new Vector(0, 0);
52            player.LinearDamping = 1;//avaruudessa ei ilmanvastusta
53        }
54        Add(this.player);
55        Camera.Follow(this.player);
56        AssignKeys();
57        LevelFromImage("graphics/levels/" + level.TileMapSrc);
58    }
59    void LevelFromImage(string levelName)
60    {
61        Dictionary<String, String> convert = new Dictionary<String, String>();
62        convert.Add("000000", "tile0");
63        ColorTileMap tileMap = ColorTileMap.FromLevelAsset(levelName);
64        tileMap.SetTileMethod(Color.FromHexCode("000000"), createTile, convert["000000"]);
65        tileMap.SetTileMethod(Color.FromHexCode("ff0000"), spawnPlayer);
66        tileMap.SetTileMethod(Color.FromHexCode("00ff00"), spawnEnemy);
67
68        double w = 40, h = 40;
69        tileMap.Execute(w, h);
70        map = tileMap;
71        //int laskValk = 0;
72        //int laskPun = 0;
73        //int lask = 0;
74        for (int y = 0; y < tileMap.RowCount; y++)
75        {
76            for (int x = 0; x < tileMap.ColumnCount; x++)
77            {
78                String hexColor = tileMap.GetTile(y, x).ToString().Substring(2, 6);
79
80                //if (hexColor == "FFFFFF") { laskValk++; continue; }
81                //if (hexColor == "FF00FF") { laskPun++; continue; }
82                //if (hexColor == "FF0000") { lask++; continue; }
83                //if (hexColor == "FF0000") { lask++; continue; }
84                if (convert.ContainsKey(hexColor))
85                {
86                    //tätä ei ole optimoitu; nyt kopioi koko kentän vaikka tarvitsisi vain murto-osan kentästä
87                    createTile(new Vector(x * w + w / 2 - Level.Width / 2 - Level.Width, (tileMap.RowCount - y - 1) * h + h / 2 - Level.Height / 2), w, h, convert[hexColor]);
88                    createTile(new Vector(x * w + w / 2 - Level.Width / 2 + Level.Width, (tileMap.RowCount - y - 1) * h + h / 2 - Level.Height / 2), w, h, convert[hexColor]);
89                }
90            }
91        }
92
93        //luo backgroundit (3)
94
95        GameObject bgCenter = new GameObject(Level.Width,Level.Height);
96        bgCenter.Image = images["backgroundplanet1"];
97        Add(bgCenter, -3);
98        GameObject bgLeft = new GameObject(Level.Width, Level.Height);
99        bgLeft.Image = images["backgroundplanet1"];
100        bgLeft.X += Level.Width;
101        Add(bgLeft, -3);
102        GameObject bgRight = new GameObject(Level.Width, Level.Height);
103        bgRight.Image = images["backgroundplanet1"];
104        bgRight.X -= Level.Width;
105        Add(bgRight, -3);
106    }
107    void createTile(Vector position, double w, double h, string id)
108    {
109        if (id == "") { return; }
110        MikonPhysicsObject tile = new MikonPhysicsObject(this, w, h);
111        tile.MakeStatic();
112        tile.AbsolutePosition = position;
113        tile.Image = images[id];
114        Add(tile);
115    }
116    void spawnPlayer(Vector position, double w, double h)
117    {
118        player.Position = position;
119    }
120    void spawnEnemy(Vector position, double w, double h)
121    {
122        new Enemy1(this, w, h, position);
123    }
124    public override void Begin()
125    {
126        // TODO: Kirjoita peli tähän
127        LoadAllImages();
128        this.player = new Player(this);
129        this.player.IsUpdated = true;
130        LoadLevel(new ProtoLevel(this, "0", true));//tää on huono metodi, TODO: luo level-classit ennen lataamista
131    }
132}
Note: See TracBrowser for help on using the repository browser.