boni.vala 3.29 KB
Newer Older
1 2
/* -*- Mode: vala; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 * Gnome Nibbles: Gnome Worm Game
Iulian Radu's avatar
Iulian Radu committed
3
 * Copyright (C) 2015 Iulian-Gabriel Radu <iulian.radu67@gmail.com>
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

Iulian Radu's avatar
Iulian Radu committed
19 20 21
// This is a fairly literal translation of the LGPLv2+ original by
// Sean MacIsaac, Ian Peters, Guillaume Béland.

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
public enum BonusType
{
    REGULAR,
    HALF,
    DOUBLE,
    LIFE,
    REVERSE,
    CUT,
    SWITCH,
    WARP
}

public class Bonus : Object
{
    public int x;
    public int y;
    public BonusType type;
    public bool fake;
    public int countdown;

    public Bonus (int x, int y, BonusType type, bool fake, int countdown)
    {
        this.x = x;
        this.y = y;
        this.type = type;
        this.fake = fake;
        this.countdown = countdown;
    }
}

52 53
public class Boni : Object
{
54
    public Gee.LinkedList<Bonus> bonuses;
55

56
    public int missed;
Iulian Radu's avatar
Iulian Radu committed
57
    public int numleft;
58
    public int numboni;
59 60 61 62 63 64
    public int numbonuses;

    public const int MAX_BONUSES = 100;
    public const int MAX_MISSED = 2;

    public signal void bonus_added ();
65
    public signal void bonus_removed (Bonus bonus);
66 67 68

    public Boni (int numworms)
    {
69
        bonuses = new Gee.LinkedList<Bonus> ();
70
        missed = 0;
71 72
        numboni = 8 + numworms;
        numbonuses = 0;
Iulian Radu's avatar
Iulian Radu committed
73
        numleft = numboni;
74 75
    }

Iulian Radu's avatar
Iulian Radu committed
76
    public void add_bonus (int[,] board, int x, int y, BonusType type, bool fake, int countdown)
77 78 79 80 81 82
    {
        if (numbonuses == MAX_BONUSES)
            return;

        var bonus = new Bonus (x, y, type, fake, countdown);
        bonuses.add (bonus);
Iulian Radu's avatar
Iulian Radu committed
83 84 85 86
        board[x, y] = type + 'A';
        board[x + 1, y] = type + 'A';
        board[x, y + 1] = type + 'A';
        board[x + 1, y + 1] = type + 'A';
87 88 89
        bonus_added ();
        numbonuses++;
    }
90

Iulian Radu's avatar
Iulian Radu committed
91
    public void remove_bonus (int[,] board, Bonus bonus)
92
    {
Iulian Radu's avatar
Iulian Radu committed
93 94 95 96
        board[bonus.x, bonus.y] = NibblesGame.EMPTYCHAR;
        board[bonus.x + 1, bonus.y] = NibblesGame.EMPTYCHAR;
        board[bonus.x, bonus.y + 1] = NibblesGame.EMPTYCHAR;
        board[bonus.x + 1, bonus.y + 1] = NibblesGame.EMPTYCHAR;
97 98 99

        bonus_removed (bonus);
    }
100

101 102 103 104 105 106 107 108 109
    public void reset (int numworms)
    {
        bonuses.clear ();
        missed = 0;
        numboni = 8 + numworms;
        numbonuses = 0;
        numleft = numboni;
    }

Iulian Radu's avatar
Iulian Radu committed
110
    public Bonus? get_bonus (int[,] board, int x, int y)
111 112 113 114 115 116 117 118 119 120 121 122 123 124
    {
        foreach (var bonus in bonuses)
        {
            if ((x == bonus.x && y == bonus.y)
                || (x == bonus.x + 1 && y == bonus.y)
                || (x == bonus.x && y == bonus.y + 1)
                || (x == bonus.x + 1 && y == bonus.y + 1))
            {
                return bonus;
            }
        }

        return null;
    }
125
}