Changeset 2635 for 2010


Ignore:
Timestamp:
2011-12-27 04:21:28 (8 years ago)
Author:
hniemi
Message:

Jonkinlainen toimiva protytyyppi kenttävalinnasta.

Location:
2010/23/hniemi/BlockPusher/Blockpusher/Blockpusher
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • 2010/23/hniemi/BlockPusher/Blockpusher/Blockpusher/Block.cs

    r2632 r2635  
    1010public class Block : GameObject 
    1111{ 
     12 
    1213    private BlockType type; 
    1314    private bool isGoalzone = false; 
  • 2010/23/hniemi/BlockPusher/Blockpusher/Blockpusher/BlockLevel.cs

    r2634 r2635  
    1414    private Block[,] playfield; 
    1515    private Blockpusher game; 
    16     private Dictionary<char, BlockType> charColor = new Dictionary<char, BlockType>(); 
     16    private List<Tuple<char, BlockType>> charcolors = new List<Tuple<char, BlockType>>(); 
    1717    private List<Block> goals = new List<Block>(); 
    1818 
     
    3333 
    3434    /// <summary> 
     35    /// Name of the level 
     36    /// </summary> 
     37    public string Name { get; set; } 
     38 
     39    /// <summary> 
    3540    /// Constructor from game 
    3641    /// </summary> 
     
    4045        this.game = game; 
    4146        InitCharColors(); 
     47    } 
     48 
     49    /// <summary> 
     50    /// Adds blocks to game 
     51    /// </summary> 
     52    public void AddToGame() 
     53    { 
     54        game.Level.Size = new Vector(playfield.GetLength(0) * (Blockpusher.BlockSize + 1), playfield.GetLength(1) * (Blockpusher.BlockSize + 1)); 
     55        game.Level.CreateBorders(); 
     56        for (int i = 0; i < playfield.GetLength(0); i++) 
     57        { 
     58            for (int j = 0; j < playfield.GetLength(1); j++) 
     59            { 
     60 
     61                playfield[i, j].X = i * (Blockpusher.BlockSize + 1) + game.Level.Left + (Blockpusher.BlockSize / 2); 
     62                playfield[i, j].Y = -(j * (Blockpusher.BlockSize + 1) + game.Level.Bottom + (Blockpusher.BlockSize / 2)); 
     63                game.Add(playfield[i,j]); 
     64            } 
     65        } 
    4266    } 
    4367 
     
    142166    public void InitCharColors() 
    143167    { 
    144         charColor.Add('x', BlockType.UnMovable); 
    145         charColor.Add('b', BlockType.Movable); 
    146         charColor.Add(' ', BlockType.Empty); 
    147         charColor.Add('m', BlockType.GoalZone); 
    148         charColor.Add('p', BlockType.Player); 
    149         charColor.Add('s', BlockType.SatisfiedGoalZone); 
     168        charcolors.Add(new Tuple<char, BlockType>('x', BlockType.UnMovable)); 
     169        charcolors.Add(new Tuple<char, BlockType>('b', BlockType.Movable)); 
     170        charcolors.Add(new Tuple<char, BlockType>(' ', BlockType.Empty)); 
     171        charcolors.Add(new Tuple<char, BlockType>('m', BlockType.GoalZone)); 
     172        charcolors.Add(new Tuple<char, BlockType>('p', BlockType.Player)); 
     173        charcolors.Add(new Tuple<char, BlockType>('s', BlockType.SatisfiedGoalZone)); 
     174    } 
     175 
     176    /// <summary> 
     177    /// Finds char matching type. 
     178    /// If not found, return ' ' 
     179    /// </summary> 
     180    /// <param name="type">Type you want to find</param> 
     181    /// <returns>Matching char</returns> 
     182    public char FindChar(BlockType type)  
     183    { 
     184        foreach (var item in charcolors) 
     185        { 
     186            if (item.Item2 == type) return item.Item1; 
     187        } 
     188        return ' '; 
     189    } 
     190 
     191    /// <summary> 
     192    /// Finds Blocktype matching char. 
     193    /// If not found, BlockType.Empty is returned. 
     194    /// </summary> 
     195    /// <param name="c">Char you want to find</param> 
     196    /// <returns>Matching type</returns> 
     197    public BlockType FindBlockType(char c) 
     198    { 
     199        foreach (var item in charcolors) 
     200        { 
     201            if (item.Item1 == c) return item.Item2; 
     202        } 
     203        return BlockType.Empty; 
    150204    } 
    151205 
     
    160214 
    161215        playfield = new Block[playfieldWidth, playfieldHeight]; 
    162         game.Level.Size = new Vector(playfieldWidth * (Blockpusher.BlockSize + 1), playfieldHeight * (Blockpusher.BlockSize + 1)); 
    163         game.Level.CreateBorders(); 
    164216 
    165217        for (int i = 0; i < playfield.GetLength(0); i++) 
     
    171223                block.Y = -(j * (Blockpusher.BlockSize + 1) + game.Level.Bottom + (Blockpusher.BlockSize / 2)); 
    172224 
    173                 block.Type = charColor[level[j][i]]; 
     225                block.Type = FindBlockType(level[j][i]); 
    174226                if (block.Type == BlockType.Player) 
    175227                { 
     
    181233                    goals.Add(block); 
    182234                } 
    183                 game.Add(block); 
    184235                playfield[i, j] = block; 
    185236            } 
     
    207258 
    208259    /// <summary> 
     260    /// Returns the current state of level. 
     261    /// </summary> 
     262    /// <returns></returns> 
     263    public List<string> CurrentState()  
     264    { 
     265        List<string> list = new List<string>(); 
     266 
     267        for (int i = 0; i < playfield.GetLength(1); i++) 
     268        { 
     269            StringBuilder line = new StringBuilder(); 
     270            for (int j = 0; j < playfield.GetLength(0); j++) 
     271            { 
     272                 
     273                line.Append(FindChar(playfield[j,i].Type)); 
     274            } 
     275            list.Add(line.ToString()); 
     276        } 
     277 
     278        return list; 
     279    } 
     280 
     281    /// <summary> 
    209282    /// Changes color of players coordinates 
    210283    /// </summary> 
  • 2010/23/hniemi/BlockPusher/Blockpusher/Blockpusher/Blockpusher.cs

    r2634 r2635  
    88using System.IO; 
    99 
     10/// <summary> 
     11/// Blockpusher-game 
     12/// </summary> 
    1013public class Blockpusher : Game 
    1114{ 
     
    1720 
    1821    private BlockLevel currentLevel; 
     22    private List<BlockLevel> levels = new List<BlockLevel>(); 
     23 
     24    private bool gameSaved = false; 
     25    private List<string> savedLevel; 
    1926 
    2027    private List<String> testLevel = new List<string>(new String[] 
     
    3340    { 
    3441        Menu(); 
    35  
    36  
    37     } 
    38  
    39     public void NewGame()  
    40     { 
    41         ClearAll(); 
    42         currentLevel = new BlockLevel(this); 
     42        LoadLevels(); 
     43    } 
     44 
     45    /// <summary> 
     46    /// Starts new game 
     47    /// </summary> 
     48    public void NewGame(BlockLevel level)  
     49    { 
     50        ClearAll(); 
     51        currentLevel = level; 
    4352        currentLevel.Victory += Victory; 
    44         currentLevel.CreateField(testLevel); 
     53        //currentLevel.CreateField(testLevel); 
     54        currentLevel.AddToGame(); 
    4555        SetControls(); 
     56    } 
     57 
     58    /// <summary> 
     59    /// Loads levels from directory 
     60    /// </summary> 
     61    /// <param name="directory">Not in use</param> 
     62    public void LoadLevels(string directory=@"Data\Levels") 
     63    { 
     64        //TODO: Tee lukeminen valmiiksi 
     65 
     66        IEnumerable<string> folders = Directory.EnumerateDirectories(directory,"*"); 
     67 
     68        foreach (string s in folders) 
     69        { 
     70            //Exludes hidden folder (with . before name) 
     71            if (s.Length != 0 && s[s.LastIndexOf('\\')+1] != '.') 
     72            { 
     73                //MessageDisplay.Add(s); 
     74                LoadLevels(s); 
     75            } 
     76        } 
     77 
     78        IEnumerable<string> files = Directory.EnumerateFiles(directory, "*"); 
     79        foreach (string file in files) 
     80        { 
     81            BlockLevel level = new BlockLevel(this); 
     82            level.CreateField(ReadFile(file)); 
     83 
     84            level.Name = file.Substring(file.LastIndexOf('\\')+1, file.LastIndexOf('.') - file.LastIndexOf('\\')-1); 
     85            levels.Add(level); 
     86        } 
     87    } 
     88 
     89    #region Menu 
     90    /// <summary> 
     91    /// Creates startmenu for game 
     92    /// </summary> 
     93    public void Menu() 
     94    { 
     95        int labelHeighth = 50; 
     96 
     97        ClearAll(); 
     98        IsMouseVisible = true; 
     99        PhoneBackButton.Listen(ConfirmExit, "Lopeta peli"); 
     100        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); 
     101 
     102        List<Tuple<string, Action>> labels = new List<Tuple<string, Action>>(); 
     103        if (gameSaved) labels.Add(new Tuple<string, Action>("Continue", ContinueGame)); 
     104        labels.Add(new Tuple<string, Action>("New game", delegate { LevelSelectionMenu(); })); 
     105        labels.Add(new Tuple<string, Action>("Quit?", ConfirmExit)); 
     106 
     107        for (int i = 0; i < labels.Count; i++) 
     108        { 
     109            PushButton button = new PushButton(200, labelHeighth); 
     110            button.Text = labels[i].Item1; 
     111            button.Y = -(i * labelHeighth); 
     112            button.Clicked += labels[i].Item2; 
     113            Add(button); 
     114        } 
     115    } 
     116 
     117    /// <summary> 
     118    /// Asks if you want to go to main menu or continue playing 
     119    /// </summary> 
     120    public void ConfirmMenu() 
     121    { 
     122        ClearControls(); 
     123        MultiSelectWindow w = new MultiSelectWindow("Back to menu?", new String[] { "Yes", "No!" }); 
     124        w.AddItemHandler(0, delegate { MakeContinuable(); Menu(); }); 
     125        w.AddItemHandler(1, delegate { SetControls(); w.Close(); }); 
     126        Add(w); 
    46127    } 
    47128 
     
    58139 
    59140    /// <summary> 
    60     /// Creates startmenu for game 
    61     /// </summary> 
    62     public void Menu()  
    63     { 
    64         ClearAll(); 
    65         IsMouseVisible = true; 
    66         PhoneBackButton.Listen(ConfirmExit, "Lopeta peli"); 
    67         Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); 
    68  
    69         List<Tuple<Label,Handler>> labels = new List<Tuple<Label,Handler>>(); 
    70         labels.Add(new Tuple<Label,Handler>(new Label("New game"), NewGame)); 
    71         labels.Add(new Tuple<Label,Handler>(new Label("Quit"), ConfirmExit)); 
    72  
    73         for (int i = 0; i < labels.Count; i++) 
    74         { 
    75             labels[i].Item1.Y = -(i * 30); 
    76             Add(labels[i].Item1); 
    77             Mouse.Listen(MouseButton.Left, ButtonState.Pressed, labels[i].Item2, null); 
    78         } 
    79  
    80     } 
     141    /// Shows levelselection menu 
     142    /// </summary> 
     143    public void LevelSelectionMenu(int current = 0) 
     144    { 
     145        ClearAll(); 
     146        int buttonheight = 50; 
     147 
     148        Label topic = new Label("Levels"); 
     149 
     150        Widget buttons = new Widget(new HorizontalLayout()); 
     151        PushButton previous = new PushButton(150, buttonheight, "Previous"); 
     152        buttons.Add(previous); 
     153 
     154        PushButton play = new PushButton(150, buttonheight, "Select"); 
     155        buttons.Add(play); 
     156 
     157        PushButton next = new PushButton(150, buttonheight, "Next"); 
     158        buttons.Add(next); 
     159 
     160        Label name = new Label(levels[current].Name); 
     161 
     162        topic.Y = Screen.Top - (topic.Height/2); 
     163        buttons.Y = topic.Bottom - (buttons.Height / 2); 
     164        name.Y = buttons.Bottom - (name.Height / 2); 
     165         
     166        Add(topic); 
     167        Add(buttons); 
     168        Add(name); 
     169 
     170        levels[current].AddToGame(); 
     171 
     172        previous.Clicked += delegate { LevelSelectionMenu((current-1 + levels.Count) % levels.Count); }; 
     173        next.Clicked += delegate { LevelSelectionMenu((current + 1) % levels.Count); }; 
     174        play.Clicked += delegate { NewGame(levels[current]); }; 
     175 
     176    } 
     177    #endregion 
     178 
     179    /// <summary> 
     180    /// Continues game from current save 
     181    /// </summary> 
     182    public void ContinueGame()  
     183    { 
     184        ClearAll(); 
     185        currentLevel = new BlockLevel(this); 
     186        currentLevel.Victory += Victory; 
     187 
     188        currentLevel.CreateField(ReadFile("Data/CurrentSave.txt")); 
     189        SetControls(); 
     190    } 
     191 
     192    /// <summary> 
     193    /// Saves game 
     194    /// </summary> 
     195    public void MakeContinuable() 
     196    { 
     197        gameSaved = true; 
     198        savedLevel = currentLevel.CurrentState(); 
     199 
     200        File.WriteAllLines("Data/CurrentSave.txt", savedLevel); 
     201    } 
     202 
    81203    /// <summary> 
    82204    /// Reads lines from file 
     
    123245        Keyboard.Listen(Key.Left, ButtonState.Pressed, MovePlayer, null, -1, 0); 
    124246        Keyboard.Listen(Key.Right, ButtonState.Pressed, MovePlayer, null, 1, 0); 
     247 
     248        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmMenu, "Lopeta peli"); 
    125249    } 
    126250} 
Note: See TracChangeset for help on using the changeset viewer.