-
Notifications
You must be signed in to change notification settings - Fork 0
/
AchievementHub.cs
160 lines (131 loc) · 5.14 KB
/
AchievementHub.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace numBlock
{
public class Achievement
{
public int achId = 0;
public String title;
public String description;
public bool achieved = false;
public Achievement(int _achId, String _title, String _description)
{
achId = _achId;
title = _title;
description = _description;
}
public override bool Equals(object obj)
{
return (((Achievement)obj).achId == this.achId);
}
public override int GetHashCode()
{
return achId;
}
}
public class AchievementHub
{
public static int ACH_CLEAN_SLATE = 0;
public static int ACH_FINAL_COUNTDOWN = 1;
public static int ACH_3_to_1 = 2;
public static int ACH_OVER_9000 = 3;
public static int ACH_COMBO_BREAKER = 4;
public static int ACH_SURVIVOR_MAN = 5;
public static int ACH_QUIT_GAME = 6;
public static int MIN_COMBO_RECORD = 2000;
List<Achievement> newAchievements = null;
public Dictionary<int,Achievement> library = null;
public AchievementHub(achievment[] savedDataList)
{
newAchievements = new List<Achievement>();
GenerateLibrary(savedDataList);
}
private void GenerateLibrary(achievment[] savedDataList)
{
library = new Dictionary<int, Achievement>();
/*
* Declare all achievements
* (try to inject a new line around 30 characters)
*/
Achievement cleanSlate = new Achievement(ACH_CLEAN_SLATE, "Clean Slate", "Clear the game board of all blocks");
library.Add(ACH_CLEAN_SLATE, cleanSlate);
Achievement finalCountdown = new Achievement(ACH_FINAL_COUNTDOWN, "Final Countdown!", "Create a combo clearing all \nnumbers from 8 to 1");
library.Add(ACH_FINAL_COUNTDOWN, finalCountdown);
Achievement threeToOne = new Achievement(ACH_3_to_1, "Countdown", "Create a combo clearing the \nnumbers 3 2 and 1 in order");
library.Add(ACH_3_to_1, threeToOne);
Achievement over9000 = new Achievement(ACH_OVER_9000, "Over 9000", "Create a combo for more than \n9000 points");
library.Add(ACH_OVER_9000, over9000);
Achievement comboBreaker = new Achievement(ACH_COMBO_BREAKER, "Combo Breaker", "Break your highest combo record\nmust be greater than "+MIN_COMBO_RECORD+"");
library.Add(ACH_COMBO_BREAKER, comboBreaker);
Achievement survivorMan = new Achievement(ACH_SURVIVOR_MAN, "Surivor Man", "Last more than 30 levels");
library.Add(ACH_SURVIVOR_MAN, survivorMan);
Achievement quitGame = new Achievement(ACH_QUIT_GAME, "You'll be back", "...");
library.Add(ACH_QUIT_GAME, quitGame);
/*
* Flag Achieved
*/
foreach (achievment savedAch in savedDataList)
{
Achievement ach = null;
library.TryGetValue(Convert.ToInt32(savedAch.ach_id), out ach);
if (ach != null)
{
ach.achieved = true;
}
}
}
public bool hasAchieved(int aId)
{
Achievement ach = null;
library.TryGetValue(aId, out ach);
if (ach != null)
return ach.achieved;
return false;
}
public void addAchievement(int aId)
{
Achievement ach = null;
library.TryGetValue(aId, out ach);
if (ach != null)
{
ach.achieved = true;
newAchievements.Add(ach);
}
}
private void addNewAchievementsToStoredData(playerinfo storedData)
{
int curLength = storedData.achievments.Count();
int newAchLength = newAchievements.Count();
achievment[] newStoredArray = new achievment[curLength + newAchLength];
for (int i = 0; i < curLength; i++)
{
newStoredArray[i] = storedData.achievments[i];
}
for (int i = 0; i < newAchLength; i++)
{
Achievement ach = (Achievement)this.newAchievements.ElementAt(i);
achievment newAch = new achievment();
newAch.ach_id = ach.achId.ToString();
newStoredArray[curLength + i] = newAch;
}
storedData.achievments = newStoredArray;
//Clear the list for new achievements
newAchievements = new List<Achievement>();
}
public bool SaveAchievements(playerinfo storedData)
{
bool requiresSave = newAchievements.Count() > 0;
if (requiresSave)
addNewAchievementsToStoredData(storedData);
return requiresSave;
}
internal Achievement getAchievement(int p)
{
Achievement ach = null;
library.TryGetValue(p, out ach);
return ach;
}
}
}