Created
April 21, 2011 21:20
-
-
Save follesoe/935501 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using MonoTouch.UIKit; | |
using System.Drawing; | |
using MonoMobile.MVVM; | |
namespace TileFlood | |
{ | |
public class GameUIController : UIViewController | |
{ | |
private readonly GameGridViewController _grid; | |
private readonly ButtonRowController _buttons; | |
private readonly GameState _gameState; | |
private readonly AdViewController _banner; | |
private readonly ScoreViewController _score; | |
private readonly ToolbarController _toolbar; | |
private readonly UINavigationController _navigationController; | |
public GameUIController(GameState gameState) | |
{ | |
_gameState = gameState; | |
_banner = new AdViewController(); | |
_score = new ScoreViewController(gameState); | |
_grid = new GameGridViewController(gameState); | |
_toolbar = new ToolbarController(); | |
_toolbar.NewGame = NewGame; | |
_toolbar.RetryGame = RetryGame; | |
_toolbar.ShowSettings = ShowSettings; | |
_buttons = new ButtonRowController(); | |
_buttons.ColorSelectedCallback = ColorSelected; | |
_navigationController = new UINavigationController(); | |
_navigationController.SetNavigationBarHidden(true, false); | |
} | |
public override void ViewDidLoad () | |
{ | |
base.ViewDidLoad(); | |
View.BackgroundColor = UIColor.Black; | |
View.AddSubviews(_banner.View, _score.View, _grid.View, _buttons.View, _toolbar.View, _navigationController.View); | |
} | |
private void ShowSettings() | |
{ | |
var binding = new BindingContext(new SettingsViewModel(), "Settings"); | |
_navigationController.PushViewController(new DialogViewController(UITableViewStyle.Grouped, binding, true), false); | |
} | |
private void NewGame() | |
{ | |
_gameState.NewGame(); | |
Update(); | |
} | |
private void RetryGame() | |
{ | |
_gameState.RetryGame(); | |
Update(); | |
} | |
private void ColorSelected(int color) | |
{ | |
if(_gameState.Level[0] == color) return; | |
if(_gameState.IsGameOver()) | |
{ | |
Console.WriteLine("You loose!"); | |
} | |
if(_gameState.IsGameWon()) | |
{ | |
Console.WriteLine("You win!"); | |
} | |
_gameState.Fill(color); | |
Update(); | |
} | |
private void Update() | |
{ | |
_grid.DrawGrid(); | |
_score.UpdateScore(); | |
} | |
} | |
} | |
using System.Linq; | |
using System.ComponentModel; | |
using System.Collections.ObjectModel; | |
namespace TileFlood | |
{ | |
public class SettingsViewModel : INotifyPropertyChanged | |
{ | |
public event PropertyChangedEventHandler PropertyChanged; | |
public ObservableCollection<Theme> Themes { get; private set; } | |
private Theme _selectedTheme; | |
public Theme SelectedTheme | |
{ | |
get { return _selectedTheme; } | |
set | |
{ | |
if(_selectedTheme != value) | |
{ | |
_selectedTheme = value; | |
RaisePropertyChanged("SelectedTheme"); | |
} | |
} | |
} | |
private bool _soundEffects; | |
public bool SoundEffects | |
{ | |
get { return _soundEffects; } | |
set | |
{ | |
_soundEffects = value; | |
RaisePropertyChanged("SoundEffects"); | |
} | |
} | |
private Level _difficulty; | |
public Level Difficulty | |
{ | |
get { return _difficulty; } | |
private set { _difficulty = value; } | |
} | |
private Mode _gameMode; | |
public Mode GameMode | |
{ | |
get { return _gameMode; } | |
private set { _gameMode = value; } | |
} | |
public bool IsNormalGame | |
{ | |
get { return _gameMode == Mode.Normal; } | |
set | |
{ | |
if (!value) return; | |
_gameMode = Mode.Normal; | |
RaiseModeChanged(); | |
} | |
} | |
public bool IsTimeGame | |
{ | |
get { return _gameMode == Mode.Time; } | |
set | |
{ | |
if (!value) return; | |
_gameMode = Mode.Time; | |
RaiseModeChanged(); | |
} | |
} | |
public bool IsEasy | |
{ | |
get { return _difficulty == Level.Easy; } | |
set | |
{ | |
if (!value) return; | |
_difficulty = Level.Easy; | |
RaiseDifficultyChanged(); | |
} | |
} | |
public bool IsNormal | |
{ | |
get { return _difficulty == Level.Normal; } | |
set | |
{ | |
if (!value) return; | |
_difficulty = Level.Normal; | |
RaiseDifficultyChanged(); | |
} | |
} | |
public bool IsHard | |
{ | |
get { return _difficulty == Level.Hard; } | |
set | |
{ | |
if (!value) return; | |
_difficulty = Level.Hard; | |
RaiseDifficultyChanged(); | |
} | |
} | |
private string _username; | |
public string Username | |
{ | |
get { return _username; } | |
set | |
{ | |
if (_username == value) return; | |
_username = value; | |
RaisePropertyChanged("Username"); | |
} | |
} | |
public SettingsViewModel() | |
{ | |
Themes = new ObservableCollection<Theme>(); | |
foreach(var theme in TileFlood.Themes.LoadThemes()) | |
{ | |
Themes.Add(theme); | |
} | |
if(StorageHelper.FileExists("Settings")) | |
{ | |
var savedSettings = StorageHelper.Load<SavedSettings>("Settings"); | |
Difficulty = savedSettings.Difficulty; | |
GameMode = savedSettings.GameMode; | |
SoundEffects = savedSettings.SoundEffects; | |
_selectedTheme = Themes.Where(t => t.Name == savedSettings.SelectedThemeName).SingleOrDefault() ?? Themes[0]; | |
} | |
else | |
{ | |
Difficulty = Level.Easy; | |
GameMode = Mode.Normal; | |
SoundEffects = true; | |
_selectedTheme = Themes.Where(t => t.Name == "Ice Cream").SingleOrDefault() ?? Themes[0]; | |
} | |
} | |
public void SaveSettings() | |
{ | |
var settings = new SavedSettings | |
{ | |
Difficulty = Difficulty, | |
GameMode = GameMode, | |
SelectedThemeName = SelectedTheme.Name, | |
SoundEffects = SoundEffects | |
}; | |
StorageHelper.Save("Settings", settings); | |
} | |
private void RaiseDifficultyChanged() | |
{ | |
RaisePropertyChanged("IsEasy"); | |
RaisePropertyChanged("IsNormal"); | |
RaisePropertyChanged("IsHard"); | |
} | |
private void RaiseModeChanged() | |
{ | |
RaisePropertyChanged("IsNormalGame"); | |
RaisePropertyChanged("IsTimeGame"); | |
} | |
private void RaisePropertyChanged(string propertyName) | |
{ | |
if(PropertyChanged != null) | |
PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); | |
} | |
} | |
public class SavedSettings | |
{ | |
public Level Difficulty { get; set; } | |
public Mode GameMode { get; set; } | |
public string SelectedThemeName { get; set; } | |
public bool SoundEffects { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment