Skip to content

Commit

Permalink
Add LocalStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
DamnDam committed Dec 23, 2014
1 parent a84f688 commit 4eea778
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 59 deletions.
Binary file modified xl2048.xlsm
Binary file not shown.
8 changes: 8 additions & 0 deletions xl2048.xlsm.src/Board.cls
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ Attribute VB_PredeclaredId = True
Attribute VB_Exposed = True
Option Explicit

''
' Public methods
'
'
Public Sub updateBoard(GameState As tGameState)
Dim isSaved As Boolean
isSaved = ThisWorkbook.Saved
Expand Down Expand Up @@ -51,6 +55,10 @@ Protect
ThisWorkbook.Saved = isSaved
End Sub

''
' Private methods
'
'
Private Property Let GameOver(Value As Boolean)
If Shapes("lblGameOver").Visible = Value Then Exit Property
Dim I As Integer
Expand Down
16 changes: 16 additions & 0 deletions xl2048.xlsm.src/GameApp.bas
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ End Type

Dim mManager As IGameManager
Dim mKBController As KeyboardControl
Dim mDefaultStorage As IStorageProvider

Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Expand All @@ -47,6 +48,13 @@ End If
Set KBController = mKBController
End Property

Private Property Get DefaultStorage() As IStorageProvider
If mDefaultStorage Is Nothing Then
Set mDefaultStorage = New LocalStorage
End If
Set DefaultStorage = mDefaultStorage
End Property

Public Property Get Style() As Object
Set Style = ThisWorkbook.Sheets(GRID_STYLE)
End Property
Expand All @@ -63,6 +71,14 @@ Sub Clear()
Manager.Clear
End Sub

Sub Save()
DefaultStorage.Save Manager.Save()
End Sub

Sub Load()
Manager.Load DefaultStorage.Load()
End Sub

Sub KBdoMove(Direction As tDirection)
KBController.Callback_DoMove Direction
End Sub
9 changes: 6 additions & 3 deletions xl2048.xlsm.src/GameManager.cls
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,15 @@ Board.updateBoard GameState
Control.Enable = False
End Sub

Private Sub IGameManager_Load(GameState As tGameState)
' TODO
Private Sub IGameManager_Load(newGameState As tGameState)
Set GameState.Grid = Nothing
GameState = newGameState
Board.updateBoard GameState
Control.Enable = True
End Sub

Private Function IGameManager_Save() As tGameState
' TODO
IGameManager_Save = GameState
End Function

''
Expand Down
80 changes: 36 additions & 44 deletions xl2048.xlsm.src/LocalStorage.cls
Original file line number Diff line number Diff line change
Expand Up @@ -7,82 +7,74 @@ Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
' TODO
#If 0 Then
Option Explicit
Implements IStorageProvider

''
' Private constants
'
'
Private Const emptyGrid = "!....!....!....!...."
Private Const rowSep = "!"
Private Const tileSep = "."

''
' Interface
'
'
Private Sub IStorageProvider_Save(GameState As tGameState)
Dim strRow As String, strGrid As String, I As Integer, J As Integer
Dim isSaved As Boolean
isSaved = ThisWorkbook.Saved
Dim Grid, strGrid As String, I As Integer, J As Integer

SaveSetting "xl2048", "2048", "Score", CStr(Board.Range("Score"))
SaveSetting "xl2048", "2048", "Best", CStr(Board.Range("BestScore"))
SaveSetting "xl2048", "2048", "Score", CStr(GameState.Score)
SaveSetting "xl2048", "2048", "Best", CStr(GameState.BestScore)

If Game.GameOver Then
Game.Clear
ThisWorkbook.Saved = isSaved
If GameState.GameOver Or GameState.Grid Is Nothing Then
GameApp.Clear
SaveSetting "xl2048", "2048", "Grid", ""
Exit Sub
End If

For I = 1 To 4
Grid = GameState.Grid.getBoard
For I = 1 To GRID_SIZE
strGrid = strGrid & rowSep
For J = 1 To 4
strGrid = strGrid & tileSep & CStr(Board.Range("Playground").Cells(I, J))
For J = 1 To GRID_SIZE
strGrid = strGrid & tileSep & Grid(I, J)
Next J
Next I

SaveSetting "xl2048", "2048", "Grid", strGrid

End Sub

Private Function IStorageProvider_Load() As tGameState
Dim Row, Grid, I As Integer, J As Integer
Dim isSaved As Boolean
isSaved = ThisWorkbook.Saved
Dim Row, Grid, Cell As tCoordinates

Application.ScreenUpdating = False
Game.Clear
Board.Unprotect
Dim GameState As tGameState

Board.Range("Score") = GetSetting("xl2048", "2048", "Score")
Board.Range("BestScore") = GetSetting("xl2048", "2048", "Best")
Grid = GetSetting("xl2048", "2048", "Grid")
Set GameState.Grid = Nothing
Set GameState.Grid = New Grid

If Animate Then Board.updateScreen 500
GameState.Score = GetSetting("xl2048", "2048", "Score")
GameState.BestScore = GetSetting("xl2048", "2048", "Best")
Grid = GetSetting("xl2048", "2048", "Grid")

If Grid = "" Or Grid = emptyGrid Then
Board.Protect
ThisWorkbook.Saved = isSaved
Application.OnTime Now, "Game.newGame"
Application.OnTime Now, "GameApp.newGame"
IStorageProvider_Load = GameState
Exit Function
End If

Grid = Split(Grid, rowSep)
For I = 1 To 4
Row = Split(Grid(I), tileSep)
For J = 1 To 4
If Row(J) <> "" Then
If Animate Then Board.updateScreen 75
Board.Range("Playground").Cells(I, J) = CLng(Row(J))
Style.Apply Board.Range("Playground").Cells(I, J)
If CLng(Row(J)) >= 2048 Then
Application.OnTime Now, "Game.Continue"
For Cell.Top = 1 To 4
Row = Split(Grid(Cell.Top), tileSep)
For Cell.Left = 1 To 4
If Row(Cell.Left) <> "" Then
GameState.Grid.addTile Cell, CLng(Row(Cell.Left))
If CLng(Row(Cell.Left)) >= 2048 Then
GameState.Continue = True
End If
End If
Next J
Next I

If Animate Then Board.updateScreen
Next Cell.Left
Next Cell.Top

Board.Protect
ThisWorkbook.Saved = isSaved
IStorageProvider_Load = GameState
End Function

#End If
20 changes: 9 additions & 11 deletions xl2048.xlsm.src/ThisWorkbook.cls
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,33 @@ Attribute VB_PredeclaredId = True
Attribute VB_Exposed = True
Option Explicit

''
' Events
'
'
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
'LocalStorage.Save
GameApp.Save

Application.ScreenUpdating = False
GameApp.Clear
'Board.Unprotect
'Range("BestScore") = ""
'Board.Protect
End Sub

Private Sub Workbook_AfterSave(ByVal Success As Boolean)
'LocalStorage.Load
GameApp.Load
End Sub

Private Sub Workbook_Open()
Application.ScreenUpdating = False
'Board.Unprotect
Board.ScrollArea = "A1:A1"
Board.Activate
Application.WindowState = xlMaximized
Board.Range("A1:H8").Select
ActiveWindow.Zoom = True
Board.Range("XL2048").Select
'Board.Protect

'Application.OnTime Now, "'LocalStorage.Load Animate:=True'"
Me.Saved = True

GameApp.Load
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
'LocalStorage.Save
GameApp.Save
End Sub
11 changes: 10 additions & 1 deletion xl2048.xlsm.src/Tile.cls
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,17 @@ Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit


''
' Private members
'
'
Private mCell As tCoordinates

''
' Properties
'
'
Public Value As Long

Public Property Get Cell() As tCoordinates
Expand Down

0 comments on commit 4eea778

Please sign in to comment.