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

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