~keith-penguin/kdegames/trunk

« back to all changes in this revision

Viewing changes to kbattleship/src/battlefieldview.cpp

  • Committer: Keith Worrell
  • Date: 2009-03-18 05:35:28 UTC
  • Revision ID: keith.worrell@gmail.com-20090318053528-mx6x9c0ngmg0kg6p
imported project

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  Copyright (c) 2007 Paolo Capriotti <p.capriotti@gmail.com>
 
3
            
 
4
  This program is free software; you can redistribute it and/or modify
 
5
  it under the terms of the GNU General Public License as published by
 
6
  the Free Software Foundation; either version 2 of the License, or
 
7
  (at your option) any later version.
 
8
*/
 
9
 
 
10
#include "battlefieldview.h"
 
11
 
 
12
#include <kdebug.h>
 
13
#include <kicon.h>
 
14
 
 
15
#include "kbsrenderer.h"
 
16
#include "sprite.h"
 
17
#include "animator.h"
 
18
#include "animation.h"
 
19
#include "welcomescreen.h"
 
20
 
 
21
BattleFieldView::BattleFieldView(KGameCanvasWidget* parent, KBSRenderer* renderer, const QString& bgID, int gridSize)
 
22
: KGameCanvasGroup(parent)
 
23
, m_renderer(renderer)
 
24
, m_factory(this, renderer)
 
25
, m_bgID(bgID)
 
26
, m_gridSize(gridSize)
 
27
, m_impact(0)
 
28
, m_last_hit(0)
 
29
, m_drawGrid(true)
 
30
{
 
31
    m_background_lower = new KGameCanvasPixmap(
 
32
        m_renderer->render(bgID + "-layer1", false, m_gridSize, m_gridSize), this);
 
33
    m_background_lower->moveTo(0, 0);
 
34
    m_background_lower->setOpacity(250);
 
35
    m_background_lower->show();
 
36
    
 
37
    m_background = new KGameCanvasPixmap(
 
38
        m_renderer->render(bgID + "-layer2", false, m_gridSize, m_gridSize), this);
 
39
    m_background->moveTo(0, 0);
 
40
    m_background->setOpacity(250);
 
41
    m_background->stackOver(m_background_lower);
 
42
    m_background->show();
 
43
    
 
44
    m_screen = new WelcomeScreen(this, parent->font());
 
45
    m_screen->stackOver(m_background);
 
46
//     m_screen->show();
 
47
    
 
48
    for (Sprites::iterator i = m_sprites.begin();
 
49
            i != m_sprites.end();
 
50
            ++i) {
 
51
        i.value() = 0;
 
52
    }
 
53
}
 
54
 
 
55
QSize BattleFieldView::size() const
 
56
{
 
57
    return m_renderer->size() * m_gridSize;
 
58
}
 
59
 
 
60
void BattleFieldView::drawGrid(bool show)
 
61
{
 
62
    m_drawGrid = show;
 
63
    update();
 
64
}
 
65
 
 
66
void BattleFieldView::update()
 
67
{
 
68
    // update welcome screen
 
69
    m_screen->moveTo(0, 0);
 
70
    m_screen->resize(size());
 
71
 
 
72
    // update background
 
73
    if(m_drawGrid) {
 
74
        qreal width, height;
 
75
        qreal distw, disth;
 
76
        width = size().width();
 
77
        height = size().height();
 
78
        distw = width / m_gridSize;
 
79
        disth = height / m_gridSize;
 
80
        QPixmap pixgrid = m_renderer->render(m_bgID + "-layer1", false, m_gridSize, m_gridSize);
 
81
        QPainter p;
 
82
        p.begin(&pixgrid);
 
83
        for(int i = 0; i < m_gridSize - 1; i++) {
 
84
            // vertical lines
 
85
            p.drawLine(QPointF((i + 1) * distw, 0.0), QPointF(( i + 1) * distw, height));
 
86
            // horizontal lines
 
87
            p.drawLine(QPointF(0.0, (i + 1) * disth), QPointF(width, (i + 1) * disth));
 
88
        }
 
89
        p.end();
 
90
        m_background_lower->setPixmap(pixgrid);
 
91
    }
 
92
    else {
 
93
        m_background_lower->setPixmap(
 
94
            m_renderer->render(m_bgID + "-layer1", false, m_gridSize, m_gridSize));
 
95
    }
 
96
    m_background_lower->moveTo(0, 0);
 
97
    m_background->setPixmap(
 
98
        m_renderer->render(m_bgID + "-layer2", false, m_gridSize, m_gridSize));
 
99
    m_background->moveTo(0, 0);
 
100
    
 
101
    // update preview
 
102
    if (m_preview.sprite) {
 
103
        m_preview.sprite->update(m_renderer);
 
104
        m_preview.sprite->moveTo(m_renderer->toReal(m_preview.pos));
 
105
    }
 
106
    
 
107
    // update sprites
 
108
    for (Sprites::const_iterator i = m_sprites.constBegin(); 
 
109
            i != m_sprites.constEnd();
 
110
            ++i) {
 
111
        i.value()->update(m_renderer);
 
112
        i.value()->moveTo(m_renderer->toReal(i.key()));
 
113
    }
 
114
}
 
115
 
 
116
void BattleFieldView::setPreview(const QPoint& pos, Ship* ship)
 
117
{
 
118
    if (!m_preview.sprite) {
 
119
        m_preview.ship = ship;
 
120
        m_preview.sprite = m_factory.createShip(ship);
 
121
        kDebug() << "created preview: dir =" << ship->direction();
 
122
        m_preview.sprite->setOpacity(PREVIEW_OPACITY);
 
123
        m_preview.sprite->show();
 
124
    }
 
125
    
 
126
    m_preview.pos = m_renderer->toLogical(pos);
 
127
    m_preview.sprite->moveTo(m_renderer->toReal(m_preview.pos));
 
128
}
 
129
 
 
130
void BattleFieldView::cancelPreview()
 
131
{
 
132
    delete m_preview.sprite;
 
133
    m_preview.sprite = 0;
 
134
    m_preview.ship = 0;
 
135
}
 
136
 
 
137
void BattleFieldView::addSprite(const Coord& c, Sprite* sprite)
 
138
{
 
139
    m_sprites.insert(c, sprite);
 
140
    
 
141
    sprite->moveTo(m_renderer->toReal(c));
 
142
    sprite->show();
 
143
}
 
144
 
 
145
void BattleFieldView::add(const Coord& c, Ship* ship)
 
146
{
 
147
    Sprite* sprite = m_factory.createShip(ship);
 
148
    addSprite(c, sprite);
 
149
    
 
150
    sprite->stackOver(m_background);
 
151
    
 
152
    // fading preview in
 
153
    if (ship == m_preview.ship) {
 
154
        Animation* a = new FadeAnimation(sprite, PREVIEW_OPACITY, 255, 1000);
 
155
        Animator::instance()->add(a);
 
156
        cancelPreview();
 
157
    }
 
158
    else if (!ship->alive()) {
 
159
        Animation* a = new FadeAnimation(sprite, 0, 200, 1000);
 
160
        Animator::instance()->add(a);
 
161
    }
 
162
}
 
163
 
 
164
void BattleFieldView::sink(const Coord& c, Ship* ship)
 
165
{
 
166
    m_last_hit = 0;
 
167
    Sprite* ship_sprite = 0;
 
168
    
 
169
    Coord p = c;
 
170
    for (unsigned int i = 0; 
 
171
         i < ship->size(); 
 
172
         i++, p += ship->increment()) {
 
173
        foreach (Sprite* s, m_sprites.values(p)) {
 
174
            if (s->name().startsWith("ship")) {
 
175
                ship_sprite = s;
 
176
            }
 
177
            else if (s->name().startsWith("hit")) {
 
178
                s->setName("hit-end");
 
179
                s->update(m_renderer);
 
180
                s->stackOver(m_background_lower);
 
181
            }
 
182
        }
 
183
    }
 
184
    if (ship_sprite) {
 
185
        ship_sprite->stackOver(m_background_lower);
 
186
    }
 
187
}
 
188
 
 
189
void BattleFieldView::hit(const Coord& c)
 
190
{
 
191
    removeImpact();
 
192
    m_last_hit = m_factory.createHit();
 
193
    addSprite(c, m_last_hit);
 
194
}
 
195
 
 
196
void BattleFieldView::miss(const Coord& c)
 
197
{
 
198
    removeImpact();
 
199
    m_impact = m_factory.createImpact();
 
200
    addSprite(c, m_impact);
 
201
}
 
202
 
 
203
void BattleFieldView::removeImpact() {
 
204
    if (m_impact) {
 
205
        m_impact->setName("water");
 
206
        m_impact->update(m_renderer);
 
207
        m_impact = 0;
 
208
    }
 
209
    if (m_last_hit) {
 
210
        m_last_hit->setName("hit-after");
 
211
        m_last_hit->update(m_renderer);
 
212
        m_last_hit = 0;
 
213
    }
 
214
}
 
215
 
 
216
void BattleFieldView::clear()
 
217
{
 
218
    delete m_preview.sprite;
 
219
    m_preview.sprite = 0;
 
220
    m_preview.ship = 0;
 
221
    
 
222
    m_impact = 0;
 
223
    m_last_hit = 0;
 
224
    
 
225
    foreach (Sprite* s, m_sprites) {
 
226
        delete s;
 
227
    }
 
228
    m_sprites.clear();
 
229
}
 
230
 
 
231
void BattleFieldView::onMousePress(const QPoint& p)
 
232
{
 
233
    if (m_screen->active()) {
 
234
        m_screen->onMousePress(p);
 
235
    }
 
236
}
 
237
 
 
238
void BattleFieldView::onMouseRelease(const QPoint& p)
 
239
{
 
240
    if (m_screen->active()) {
 
241
        m_screen->onMouseRelease(p);
 
242
    }
 
243
}
 
244
 
 
245
void BattleFieldView::onMouseMove(const QPoint& p)
 
246
{
 
247
    if (m_screen->active()) {
 
248
        m_screen->onMouseMove(p);
 
249
    }
 
250
}
 
251
 
 
252
void BattleFieldView::onMouseLeave()
 
253
{
 
254
    if (m_screen->active()) {
 
255
        m_screen->onMouseLeave();
 
256
    }
 
257
}
 
258
 
 
259
WelcomeScreen* BattleFieldView::screen() const
 
260
{
 
261
    return m_screen;
 
262
}