summaryrefslogtreecommitdiffstats
path: root/demos/declarative/samegame/content/samegame.js
blob: fe5ac87d419f3ebcf1f9ae1b63f26bf4ee48d6e3 (plain)
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/* This script file handles the game logic */

var maxX = 10;//Nums are for tileSize 40
var maxY = 15;
var tileSize = 40;
var maxIndex = maxX*maxY;
var board = new Array(maxIndex);
var tileSrc = "content/BoomBlock.qml";
var backSrc = "content/pics/background.png";
var swapped = false;

var compSrc;
var component;

function swapTileSrc(){
    if(swapped)
        return;
    if(tileSrc == "content/FastBlock.qml"){
        tileSrc = "content/BoomBlock.qml";
        backSrc = "content/pics/background.png";
    }else{
        backSrc = "content/pics/qtlogo.png";
        tileSrc = "content/FastBlock.qml";
    }
    swapped = true;
}

function index(xIdx,yIdx){
    return xIdx + (yIdx * maxX);
}

function initBoard()
{
    for(i = 0; i<maxIndex; i++){
        //Delete old blocks
        if(board[i] != null)
            board[i].destroy();
    }

    background.source = backSrc;
    swapped = false;
    maxX = Math.floor(gameCanvas.width/tileSize);
    maxY = Math.floor(gameCanvas.height/tileSize);
    maxIndex = maxX*maxY;
    board = new Array(maxIndex);
    gameCanvas.score = 0;

    for(xIdx=0; xIdx<maxX; xIdx++){
        for(yIdx=0; yIdx<maxY; yIdx++){
            board[index(xIdx,yIdx)] = null;
            startCreatingBlock(xIdx,yIdx);
        }
    }
    //TODO: a flag that handleMouse uses to ignore clicks when we're loading
}

var removed;
var floodBoard;
function handleClick(x,y)
{
    //NOTE: Be careful with vars named x,y - they can set to the calling object?
    xIdx = Math.floor(x/tileSize);
    yIdx = Math.floor(y/tileSize);
    if(xIdx >= maxX || xIdx < 0 || yIdx >= maxY || yIdx < 0)
        return;
    if(board[index(xIdx, yIdx)] == null)
        return;
    removed = 0;
    floodBoard = new Array(maxIndex);
    floodKill(xIdx,yIdx, -1);
    if(removed <= 0)
        return;
    gameCanvas.score += (removed - 1) * (removed - 1);
    shuffleDown();
    victoryCheck();
}

function floodKill(xIdx,yIdx,type)
{
    if(xIdx >= maxX || xIdx < 0 || yIdx >= maxY || yIdx < 0)
        return;
    if(floodBoard[index(xIdx, yIdx)] == 1)
        return;
    if(board[index(xIdx, yIdx)] == null)
        return;
    var first = false;
    if(type == -1){
        type = board[index(xIdx,yIdx)].type;
        first = true;
    }else{
        if(type != board[index(xIdx,yIdx)].type)
            return;
    }
    floodBoard[index(xIdx, yIdx)] = 1;
    floodKill(xIdx+1,yIdx,type);
    floodKill(xIdx-1,yIdx,type);
    floodKill(xIdx,yIdx+1,type);
    floodKill(xIdx,yIdx-1,type);
    if(first==true && removed == 0){
        //TODO: Provide a way to inform the delegate
        return;//Can't remove single tiles
    }
    board[index(xIdx,yIdx)].dying = true;
    board[index(xIdx,yIdx)] = null;//They'll have to destroy themselves(can we do that?)
    removed += 1;
}

function shuffleDown()
{
    //Fall down
    for(xIdx=0; xIdx<maxX; xIdx++){
        fallDist = 0;
        for(yIdx=maxY-1; yIdx>=0; yIdx--){
            if(board[index(xIdx,yIdx)] == null){
                fallDist += 1;
            }else{
                if(fallDist > 0){
                    obj = board[index(xIdx,yIdx)];
                    obj.targetY += fallDist * tileSize;
                    board[index(xIdx,yIdx+fallDist)] = obj;
                    board[index(xIdx,yIdx)] = null;
                }
            }
        }
    }
    //Fall to the left
    fallDist = 0;
    for(xIdx=0; xIdx<maxX; xIdx++){
        if(board[index(xIdx, maxY - 1)] == null){
            fallDist += 1;
        }else{
            if(fallDist > 0){
                for(yIdx=0; yIdx<maxY; yIdx++){
                    obj = board[index(xIdx,yIdx)];
                    if(obj == null)
                        continue;
                    obj.targetX -= fallDist * tileSize;
                    board[index(xIdx-fallDist,yIdx)] = obj;
                    board[index(xIdx,yIdx)] = null;
                }
            }
        }
    }
}

function victoryCheck()
{
    //Only awards bonuses at the moment
    deservesBonus = true;
    for(xIdx=maxX-1; xIdx>=0; xIdx--)
        if(board[index(xIdx, maxY - 1)] != null)
            deservesBonus = false;
    if(deservesBonus)
        gameCanvas.score += 250;
}

//Need a simpler method of doing this?
var waitStack = new Array(maxIndex);
var waitTop = -1;

function finishCreatingBlock(xIdx,yIdx){
    //TODO: Doc that the 'x', 'y' that were here are hidden properties from the calling QFxItem
    if(component.isReady()){
        if(xIdx == undefined){
            //Called without arguments, create a previously stored (xIdx,yIdx)
            if(waitTop == -1)
                return;//Don't have a previously stored (xIdx,yIdx)
            xIdx = waitStack[waitTop] % maxX;
            yIdx = Math.floor(waitStack[waitTop] / maxX);
            waitTop -= 1;
        }
        dynamicObject = component.createObject();
        if(dynamicObject == null){
            print("error creating block");
            print(component.errorsString());
            return false;
        }
        dynamicObject.type = Math.floor(Math.random() * 3);
        dynamicObject.parent = gameCanvas;
        dynamicObject.targetX = xIdx*tileSize;
        dynamicObject.targetY = yIdx*tileSize;
        dynamicObject.width = tileSize;
        dynamicObject.height = tileSize;
        dynamicObject.spawning = true;
        board[index(xIdx,yIdx)] = dynamicObject;
        return true;
    }else if(component.isError()){
        print("error creating block");
        print(component.errorsString());
    }else{
        //It isn't ready, but we'll be called again when it is.
        //So store the requested (xIdx,yIdx) for later use
        waitTop += 1;
        waitStack[waitTop] = index(xIdx,yIdx);
    }
    return false;
}

function startCreatingBlock(xIdx,yIdx){
    if(component!=null && compSrc == tileSrc){
        finishCreatingBlock(xIdx,yIdx);
        return;
    }

    if(component!=null){//Changed source
        //delete component; //Does the engine handle this?
        compSrc = tileSrc;
    }
    component = createComponent(tileSrc);
    if(finishCreatingBlock(xIdx,yIdx))
        return;
    component.statusChanged.connect(finishCreatingBlock());
    return;
}