From: Joe Wreschnig Date: Tue, 30 Apr 2013 11:04:34 +0000 (+0200) Subject: Look I can OO. X-Git-Url: https://git.yukkurigames.com/?a=commitdiff_plain;h=af304a2eabfff32ed5d316ea3979230819f4a932;p=123456789.git Look I can OO. --- diff --git a/game.js b/game.js index 27a89ab..ac5b69f 100644 --- a/game.js +++ b/game.js @@ -34,7 +34,7 @@ var state = LEVEL_SELECT; var max_level = 1; var current_level = 1; -var count = 0; +var current_level_inst = null; function QueueChange(bead, sound) { if (sound == null) @@ -70,83 +70,86 @@ function CheckExact(pos) { return true; }; -function FollowMeStart(x, y) { - count = 0; +function FollowMe () { + var count = 0; PS.BeadColor(PS.Random(3) - 1, PS.Random(3) - 1, PS.COLOR_BLACK); -}; -function FollowMePress(x, y) { - if (PS.BeadColor(x, y) == PS.COLOR_BLACK) { - PS.BeadColor(x, y, PS.COLOR_WHITE); - count++; - if (count >= 4) { - Win(); + this.Press = function (x, y) { + if (PS.BeadColor(x, y) == PS.COLOR_BLACK) { + PS.BeadColor(x, y, PS.COLOR_WHITE); + count++; + if (count >= 4) { + Win(); + } else { + PS.BeadColor(PS.Random(3) - 1, PS.Random(3) - 1, + PS.COLOR_BLACK); + PS.AudioPlay(PS.Xylophone(count == 3 ? 14 : 13), 0.5); + } } else { - PS.BeadColor(PS.Random(3) - 1, PS.Random(3) - 1, PS.COLOR_BLACK); - PS.AudioPlay(PS.Xylophone(count == 3 ? 14 : 13), 0.5); + Mistake(); + count = 0; } - } else { - Mistake(); - count = 0; - } -}; - -function EnumerateStart() { + }; }; -function EnumeratePress(x, y) { - for (var n = 1; n < LEVEL_IDX[y][x]; ++n) { - if (PS.BeadColor(LEVEL_POS[n][0], LEVEL_POS[n][1]) != PS.COLOR_BLACK) { +function Enumerate () { + this.Press = function (x, y) { + for (var n = 1; n < LEVEL_IDX[y][x]; ++n) { + if (PS.BeadColor(LEVEL_POS[n][0], LEVEL_POS[n][1]) + != PS.COLOR_BLACK) { + Mistake(); + return; + } + } + if (PS.BeadColor(x, y) == PS.COLOR_BLACK) { Mistake(); - return; + } else { + PS.BeadColor(x, y, PS.COLOR_BLACK); + if (LEVEL_IDX[y][x] == 9) + Win(); + else if (LEVEL_IDX[y][x] == 3) + PS.AudioPlay(PS.Xylophone(15), 0.5); + else if (LEVEL_IDX[y][x] == 6) + PS.AudioPlay(PS.Xylophone(16), 0.5); + else if (LEVEL_IDX[y][x] > 6) + PS.AudioPlay(PS.Xylophone(15), 0.5); + else if (LEVEL_IDX[y][x] > 3) + PS.AudioPlay(PS.Xylophone(14), 0.5); + else + PS.AudioPlay(PS.Xylophone(13), 0.5); } - } - if (PS.BeadColor(x, y) == PS.COLOR_BLACK) { - Mistake(); - } else { - PS.BeadColor(x, y, PS.COLOR_BLACK); - if (LEVEL_IDX[y][x] == 9) - Win(); - else if (LEVEL_IDX[y][x] == 3) - PS.AudioPlay(PS.Xylophone(15), 0.5); - else if (LEVEL_IDX[y][x] == 6) - PS.AudioPlay(PS.Xylophone(16), 0.5); - else if (LEVEL_IDX[y][x] > 6) - PS.AudioPlay(PS.Xylophone(15), 0.5); - else if (LEVEL_IDX[y][x] > 3) - PS.AudioPlay(PS.Xylophone(14), 0.5); - else - PS.AudioPlay(PS.Xylophone(13), 0.5); - } + }; }; -var repeatorder = []; -function RepeatStart() { +function Repeat() { + var repeatorder = Shuffle([1, 2, 3, 4, 5, 6, 7, 8, 9]).slice(0, 6); PS.Clock(45); - repeatorder = Shuffle([1, 2, 3, 4, 5, 6, 7, 8, 9]).slice(0, 6); for (var n = 0; n < repeatorder.length; ++n) { QueueChange([[PS.COLOR_BLACK, repeatorder[n]]], - [PS.Xylophone(2 + n)]); - } -}; + [PS.Xylophone(2 + n)]); + + this.Press = function Press (x, y) { + var n = repeatorder.shift(); + if (LEVEL_IDX[y][x] == n) { + PS.BeadColor(x, y, PS.COLOR_GREEN); + if (repeatorder.length == 0) { + Win(); + } else { + PS.AudioPlay(PS.Xylophone(9 + repeatorder.length), 0.5); + PS.AudioPlay(PS.Xylophone(9 + repeatorder.length), 0.5); + } + } else { + Mistake(); + } + }; + }; +}; + +function TickMissing() { + var repeatorder; + var missing_valid; + var count = 0; -function RepeatPress(x, y) { - var n = repeatorder.shift(); - if (LEVEL_IDX[y][x] == n) { - PS.BeadColor(x, y, PS.COLOR_GREEN); - if (repeatorder.length == 0) { - Win(); - } else { - PS.AudioPlay(PS.Xylophone(9 + repeatorder.length), 0.5); - PS.AudioPlay(PS.Xylophone(9 + repeatorder.length), 0.5); - } - } else { - Mistake(); - } -}; - -var missing_valid = []; -function TickMissingStart() { PS.Clock(45); var d = {}; missing_valid = []; @@ -162,107 +165,106 @@ function TickMissingStart() { [PS.Xylophone(13 + n)]); } } + for (var n = 1; n <= 9; ++n) if (repeatorder.indexOf(n) < 0) missing_valid.push(n); - count = 0; -}; -function TickMissingPress(x, y) { - if (missing_valid.indexOf(LEVEL_IDX[y][x]) < 0) { - Mistake(); - } else { - PS.BeadColor(x, y, PS.COLOR_BLACK); - if (CheckExact(missing_valid)) { - Win(); + this.Press = function (x, y) { + if (missing_valid.indexOf(LEVEL_IDX[y][x]) < 0) { + Mistake(); } else { - count++; - PS.AudioPlay(PS.Xylophone(count == 2 ? 14 : 13), 0.5); + PS.BeadColor(x, y, PS.COLOR_BLACK); + if (CheckExact(missing_valid)) { + Win(); + } else { + count++; + PS.AudioPlay(PS.Xylophone(count == 2 ? 14 : 13), 0.5); + } } - } + }; }; -var third_row; - -function ThreeRowGenerate() { - third_row = Shuffle(Choose([ - [1, 2, 3], [4, 5, 6], [7, 8, 9], - [1, 4, 7], [2, 5, 8], [3, 6, 9], - [1, 5, 9], [3, 5, 7]])); - QueueChange([[PS.COLOR_BLACK, third_row[0]]], [PS.Xylophone(13)]); - QueueChange([[PS.COLOR_BLACK, third_row[1]]], [PS.Xylophone(13)]); -} - -function ThreeRowStart() { - count = 0; +function ThreeRow () { + var third_row; + var count = 0; PS.Clock(45); - ThreeRowGenerate(); -}; - -function ThreeRowPress(x, y) { - if (LEVEL_IDX[y][x] == third_row[2]) { - count++; - PS.BeadColor(x, y, PS.COLOR_BLACK); - PS.Clock(PS.Clock()); - if (count >= 3) { - Win(); + Generate(); + + function Generate() { + third_row = Shuffle(Choose([ + [1, 2, 3], [4, 5, 6], [7, 8, 9], + [1, 4, 7], [2, 5, 8], [3, 6, 9], + [1, 5, 9], [3, 5, 7]])); + QueueChange([[PS.COLOR_BLACK, third_row[0]]], [PS.Xylophone(13)]); + QueueChange([[PS.COLOR_BLACK, third_row[1]]], [PS.Xylophone(13)]); + }; + + this.Press = function (x, y) { + if (LEVEL_IDX[y][x] == third_row[2]) { + count++; + PS.BeadColor(x, y, PS.COLOR_BLACK); + PS.Clock(PS.Clock()); + if (count >= 3) { + Win(); + } else { + PS.AudioPlay(PS.Xylophone(14), 0.5); + QueueChange([[PS.COLOR_GREEN, third_row[2]], + [PS.COLOR_GREEN, third_row[1]], + [PS.COLOR_GREEN, third_row[0]]], + [PS.Xylophone(34)]); + QueueChange([[PS.COLOR_WHITE, third_row[2]], + [PS.COLOR_WHITE, third_row[1]], + [PS.COLOR_WHITE, third_row[0]]]); + Generate(); + } } else { - PS.AudioPlay(PS.Xylophone(14), 0.5); - QueueChange([[PS.COLOR_GREEN, third_row[2]], - [PS.COLOR_GREEN, third_row[1]], - [PS.COLOR_GREEN, third_row[0]]], - [PS.Xylophone(34)]); - QueueChange([[PS.COLOR_WHITE, third_row[2]], - [PS.COLOR_WHITE, third_row[1]], - [PS.COLOR_WHITE, third_row[0]]]); - ThreeRowGenerate(); + count = 0; + Mistake(); } - } else { - count = 0; - Mistake(); - } -}; - -var sudoku_row; -function SudokuRowGenerate() { - sudoku_row = Shuffle(Choose([[1, 8, 6], [4, 8, 3], [4, 2, 9], [7, 2, 6]])); - QueueChange([[PS.COLOR_RED, sudoku_row[0]]], - [PS.Xylophone(3)]); - QueueChange([[PS.COLOR_RED, sudoku_row[1]]], - [PS.Xylophone(2)]); + }; }; -function SudokuStart() { - count = 0; +function SudokuRow () { + var sudoku_row; + var count = 0; PS.Clock(45); - SudokuRowGenerate(); -} - -function SudokuPress(x, y) { - if (LEVEL_IDX[y][x] == sudoku_row[2]) { - count++; - PS.BeadColor(x, y, PS.COLOR_BLACK); - if (count >= 3) { - Win(); + Generate(); + + function Generate() { + sudoku_row = Shuffle( + Choose([[1, 8, 6], [4, 8, 3], [4, 2, 9], [7, 2, 6]])); + QueueChange([[PS.COLOR_RED, sudoku_row[0]]], + [PS.Xylophone(3)]); + QueueChange([[PS.COLOR_RED, sudoku_row[1]]], + [PS.Xylophone(2)]); + }; + + this.Press = function (x, y) { + if (LEVEL_IDX[y][x] == sudoku_row[2]) { + count++; + PS.BeadColor(x, y, PS.COLOR_BLACK); + if (count >= 3) { + Win(); + } else { + PS.AudioPlay(PS.Xylophone(13), 0.5); + QueueChange([[PS.COLOR_GREEN, sudoku_row[2]], + [PS.COLOR_GREEN, sudoku_row[1]], + [PS.COLOR_GREEN, sudoku_row[0]]], + [PS.Xylophone(14)]); + QueueChange([[PS.COLOR_WHITE, sudoku_row[2]], + [PS.COLOR_WHITE, sudoku_row[1]], + [PS.COLOR_WHITE, sudoku_row[0]]]); + Generate(); + } } else { - PS.AudioPlay(PS.Xylophone(13), 0.5); - QueueChange([[PS.COLOR_GREEN, sudoku_row[2]], - [PS.COLOR_GREEN, sudoku_row[1]], - [PS.COLOR_GREEN, sudoku_row[0]]], - [PS.Xylophone(14)]); - QueueChange([[PS.COLOR_WHITE, sudoku_row[2]], - [PS.COLOR_WHITE, sudoku_row[1]], - [PS.COLOR_WHITE, sudoku_row[0]]]); - SudokuRowGenerate(); + count = 0; + Mistake(); } - } else { - count = 0; - Mistake(); - } + }; }; -function DigitsStart() { - PS.Clock(40); +function Digits () { QueueChange([[PS.COLOR_BLACK, 1]], ["perc_hihat_closed"]); QueueChange([[PS.COLOR_WHITE, 1], [PS.COLOR_BLACK, 4]], ["perc_hihat_closed"]); @@ -281,33 +283,32 @@ function DigitsStart() { QueueChange([[PS.COLOR_WHITE, 2], [PS.COLOR_WHITE, 5], [PS.COLOR_WHITE, 8]]); - count = 4; -}; + var count = 4; -function DigitsPress(x, y) { - if (PS.BeadColor(x, y) == PS.COLOR_WHITE) { - PS.BeadColor(x, y, PS.COLOR_BLACK); - } else { - PS.BeadColor(x, y, PS.COLOR_WHITE); - } - - if (count == 4 && ApproveIfExact([7, 9, 4, 5, 6, 3])) - count = 7; - if (count == 7 && (CheckExact([7, 8, 9, 6, 3]) - || CheckExact([7, 8, 9, 6, 2]))) { - Win(); - return; - } + this.Press = function (x, y) { + if (PS.BeadColor(x, y) == PS.COLOR_WHITE) { + PS.BeadColor(x, y, PS.COLOR_BLACK); + } else { + PS.BeadColor(x, y, PS.COLOR_WHITE); + } - if (PS.BeadColor(x, y) == PS.COLOR_WHITE) { - PS.AudioPlay(PS.Xylophone(10), 0.5); - } else { - PS.AudioPlay(PS.Xylophone(13), 0.3); - } + if (count == 4 && ApproveIfExact([7, 9, 4, 5, 6, 3])) + count = 7; + if (count == 7 && (CheckExact([7, 8, 9, 6, 3]) + || CheckExact([7, 8, 9, 6, 2]))) { + Win(); + return; + } + if (PS.BeadColor(x, y) == PS.COLOR_WHITE) { + PS.AudioPlay(PS.Xylophone(10), 0.5); + } else { + PS.AudioPlay(PS.Xylophone(13), 0.3); + } + }; }; -function ShapesStart() { +function Shapes() { PS.Clock(40); QueueChange([[PS.COLOR_BLACK, 1]], [PS.Xylophone(13)]); QueueChange([[PS.COLOR_GREEN, 1]], [PS.Xylophone(14)]); @@ -322,45 +323,32 @@ function ShapesStart() { QueueChange([[PS.COLOR_BLACK, 1], [PS.COLOR_BLACK, 4], [PS.COLOR_BLACK, 7]]); - count = 3; -}; + var count = 3; -function ShapesPress(x, y) { - if (PS.BeadColor(x, y) == PS.COLOR_WHITE) { - PS.BeadColor(x, y, PS.COLOR_BLACK); - } else { - PS.BeadColor(x, y, PS.COLOR_WHITE); - } - - if (count == 3 && (ApproveIfExact([1, 4, 7, 2, 5, 3], PS.COLOR_BLACK) - || ApproveIfExact([1, 4, 7, 8, 5, 3], PS.COLOR_BLACK))) { - count = 4; - if (PS.BeadColor(x, y) == PS.COLOR_WHITE) - PS.AudioPlay(PS.Xylophone(10), 0.3); - else - PS.AudioPlay(PS.Xylophone(13), 0.5); - } else if (count == 4 && CheckExact([1, 2, 3, 4, 5, 6, 7, 8, 9])) - Win(); - else { - if (PS.BeadColor(x, y) == PS.COLOR_WHITE) - PS.AudioPlay(PS.Xylophone(10), 0.3); - else - PS.AudioPlay(PS.Xylophone(13), 0.5); - } -}; + this.Press = function (x, y) { + if (PS.BeadColor(x, y) == PS.COLOR_WHITE) { + PS.BeadColor(x, y, PS.COLOR_BLACK); + } else { + PS.BeadColor(x, y, PS.COLOR_WHITE); + } -function DieStart() { - PS.Clock(40); - QueueChange([[PS.COLOR_BLACK, 5]], [PS.Xylophone(13)]); - QueueChange([[PS.COLOR_GREEN, 5]], [PS.Xylophone(14)]); - QueueChange([[PS.COLOR_WHITE, 5]]); - QueueChange([[PS.COLOR_BLACK, 1]], [PS.Xylophone(13)]); - QueueChange([[PS.COLOR_BLACK, 9]], [PS.Xylophone(13)]); - QueueChange([[PS.COLOR_GREEN, 1], - [PS.COLOR_GREEN, 9]], [PS.Xylophone(14)]); - QueueChange([[PS.COLOR_WHITE, 1], - [PS.COLOR_WHITE, 9]]); - count = 3; + if (count == 3 + && (ApproveIfExact([1, 4, 7, 2, 5, 3], PS.COLOR_BLACK) + || ApproveIfExact([1, 4, 7, 8, 5, 3], PS.COLOR_BLACK))) { + count = 4; + if (PS.BeadColor(x, y) == PS.COLOR_WHITE) + PS.AudioPlay(PS.Xylophone(10), 0.3); + else + PS.AudioPlay(PS.Xylophone(13), 0.5); + } else if (count == 4 && CheckExact([1, 2, 3, 4, 5, 6, 7, 8, 9])) { + Win(); + } else { + if (PS.BeadColor(x, y) == PS.COLOR_WHITE) + PS.AudioPlay(PS.Xylophone(10), 0.3); + else + PS.AudioPlay(PS.Xylophone(13), 0.5); + } + }; }; function ApproveIfExact(a, color) { @@ -384,51 +372,65 @@ function ApproveIfExact(a, color) { } }; -function DiePress(x, y) { - if (PS.BeadColor(x, y) == PS.COLOR_WHITE) { - PS.BeadColor(x, y, PS.COLOR_BLACK); - } else { - PS.BeadColor(x, y, PS.COLOR_WHITE); - } - switch (count) { - case 3: - if (ApproveIfExact([1, 5, 9]) || ApproveIfExact([7, 5, 3])) - count = 4; - break; - case 4: - if (ApproveIfExact([1, 3, 7, 9])) - count = 5; - break; - case 5: - if (ApproveIfExact([1, 3, 5, 7, 9])) - count = 6; - break; - case 6: - if (CheckExact([1, 2, 3, 7, 8, 9]) - || CheckExact([1, 4, 7, 3, 6, 9])) { - Win(); - return; +function Die () { + PS.Clock(40); + QueueChange([[PS.COLOR_BLACK, 5]], [PS.Xylophone(13)]); + QueueChange([[PS.COLOR_GREEN, 5]], [PS.Xylophone(14)]); + QueueChange([[PS.COLOR_WHITE, 5]]); + QueueChange([[PS.COLOR_BLACK, 1]], [PS.Xylophone(13)]); + QueueChange([[PS.COLOR_BLACK, 9]], [PS.Xylophone(13)]); + QueueChange([[PS.COLOR_GREEN, 1], + [PS.COLOR_GREEN, 9]], [PS.Xylophone(14)]); + QueueChange([[PS.COLOR_WHITE, 1], + [PS.COLOR_WHITE, 9]]); + var count = 3; + + this.Press = function (x, y) { + if (PS.BeadColor(x, y) == PS.COLOR_WHITE) { + PS.BeadColor(x, y, PS.COLOR_BLACK); + } else { + PS.BeadColor(x, y, PS.COLOR_WHITE); + } + switch (count) { + case 3: + if (ApproveIfExact([1, 5, 9]) || ApproveIfExact([7, 5, 3])) + count = 4; + break; + case 4: + if (ApproveIfExact([1, 3, 7, 9])) + count = 5; + break; + case 5: + if (ApproveIfExact([1, 3, 5, 7, 9])) + count = 6; + break; + case 6: + if (CheckExact([1, 2, 3, 7, 8, 9]) + || CheckExact([1, 4, 7, 3, 6, 9])) { + Win(); + return; + } } - } - if (PS.BeadColor(x, y) == PS.COLOR_WHITE) { - PS.AudioPlay(PS.Xylophone(14), 0.5); - } else { - PS.AudioPlay(PS.Xylophone(13), 0.3); - } + if (PS.BeadColor(x, y) == PS.COLOR_WHITE) { + PS.AudioPlay(PS.Xylophone(14), 0.5); + } else { + PS.AudioPlay(PS.Xylophone(13), 0.3); + } + }; }; var LEVELS = [ null, - [FollowMeStart, FollowMePress], - [ThreeRowStart, ThreeRowPress], - [EnumerateStart, EnumeratePress], - [RepeatStart, RepeatPress], - [TickMissingStart, TickMissingPress], - [SudokuStart, SudokuPress], - [ShapesStart, ShapesPress], - [DieStart, DiePress], - [DigitsStart, DigitsPress], + FollowMe, + ThreeRow, + Enumerate, + Repeat, + TickMissing, + SudokuRow, + Shapes, + Die, + Digits, ]; function Reset(color) { @@ -502,7 +504,7 @@ function StartLevel(level) { PS.AudioPlay(PS.Xylophone(1), 0.5); Reset(PS.COLOR_WHITE); current_level = level; - LEVELS[current_level][0](); + current_level_inst = new LEVELS[current_level](); state = LEVEL_IN; }; @@ -529,7 +531,7 @@ PS.Click = function (x, y, data, options) { StartLevel(LEVEL_IDX[y][x]); break; case LEVEL_IN: - LEVELS[current_level][1](x, y); + current_level_inst.Press(x, y); break; case LEVEL_WON: LevelSelect();