aboutsummaryrefslogtreecommitdiff
path: root/src/Hero.cpp
blob: 5567827142589d094dd5ee9d327601f8ab8c7bd6 (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
#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsPixmapItem>
#include <QSettings>
#include <QPainter>
#include <QDir>
#include <QColor>
#include <QDebug>

#include "Box2D/Dynamics/b2World.h"
#include "Box2d/Dynamics/b2Body.h"
#include "Box2d/Dynamics/b2Fixture.h"
#include "Box2d/Collision/Shapes/b2PolygonShape.h"

#include "common.h"

#include "Hero.h"
#include "GameScene.h"

Hero::Hero(QGraphicsScene* scene, QPointF pos, QGraphicsItem* parent)
    : Character(parent, scene)
{
    m_state = STATE_IDLE;

    // something small for testing purposes
    setHealthPoints(15);

    setPos(pos);
    setZValue(2);

    setShapeMode(QGraphicsPixmapItem::MaskShape);

    m_mode = "neutral";

    m_neutral = new QPixmap(32, 64);
    m_neutral->fill(Qt::transparent);
    setPixmap(*m_neutral);

    m_red = new QPixmap(32, 64);
    m_red->fill(Qt::transparent);
    QPainter p;
    p.setPen(Qt::NoPen);
    QBrush b(Qt::transparent);
    p.begin(m_red);
    b.setColor(QColor(127, 0, 0, 127));
    p.setBrush(b);
    p.drawRoundedRect(QRect(0, 0, 31, 63), 5, 5);
    p.end();

    m_green = new QPixmap(32, 64);
    m_green->fill(Qt::transparent);
    p.begin(m_green);
    b.setColor(QColor(0, 127, 0, 127));
    p.setBrush(b);
    p.drawRoundedRect(QRect(0, 0, 31, 63), 5, 5);
    p.end();

    m_blue = new QPixmap(32, 64);
    m_blue->fill(Qt::transparent);
    p.begin(m_blue);
    b.setColor(QColor(0, 0, 127, 127));
    p.setBrush(b);
    p.drawRoundedRect(QRect(0, 0, 31, 63), 5, 5);
    p.end();
}

Hero::~Hero()
{
    if (m_red)
        delete m_red;
    if (m_green)
        delete m_green;
    if (m_blue)
        delete m_blue;
    if (m_neutral)
        delete m_neutral;
}

void Hero::bindToWorld(b2World *world)
{
    b2BodyDef bd;
    bd.type = b2_dynamicBody;
    bd.position.Set(P2M(16), P2M(32));

    m_body = world->CreateBody(&bd);

    b2PolygonShape polygonShape;
    polygonShape.SetAsBox(P2M(32.0f / 2),
                          P2M(64.0f / 2),
                          b2Vec2(P2M(32.0f / 2),
                                 P2M(64.0f / 2)),
                          0.0f);

    b2FixtureDef fixtureDef;
    fixtureDef.shape = &polygonShape;
    fixtureDef.density = 15;
    fixtureDef.friction = 0.05f;
    fixtureDef.restitution = 0.25f;
    fixtureDef.userData = this;

    m_fixture = m_body->CreateFixture(&fixtureDef);

    b2MassData md;
    m_body->GetMassData(&md);
    md.mass = 0.7f;
    m_fixture->GetBody()->SetMassData(&md);

    qDebug() << "mass: " << m_body->GetMass() << "kg";
}

void Hero::loadAnimations(const QString &animationsDirectory)
{
    QDir animDirs(animationsDirectory);

    foreach(QString animName, animDirs.entryList(QDir::Dirs | QDir::NoDotAndDotDot))
    {
        QDir anim(animationsDirectory + "/" + animName);

        foreach(QString frame, anim.entryList(QDir::Files | QDir::NoDotAndDotDot))
        {
            addFrame(animName, QPixmap(animationsDirectory + "/" + animName + "/" + frame));
        }
    }
    setAnimation("idle");
}

void Hero::advance(int phase)
{
    Character::advance(phase);

    if (phase == 0)
    {
        return;
    }
    else if (phase == 1)
    {
        b2Vec2 pos = m_body->GetPosition();
        setPos(M2P(pos.x)-16, M2P(pos.y)-32);
    }

    // err, no good.
    if(m_state == STATE_DEAD)
    {
        gameOver();
        //removeMe();
    }
}

void Hero::updatePosition(qreal x, qreal y)
{
    m_body->SetTransform(b2Vec2(P2M(x), P2M(y)), 0);
    setPos(x-16, y-32);
}

void Hero::toggleRedColor()
{
    if (m_mode == "neutral")
    {
        setPixmap(*m_red);
        m_mode = "red";
    }
    else if (m_mode == "red")
    {
        setPixmap(*m_neutral);
        m_mode = "neutral";
    }
}

void Hero::toggleGreenColor()
{
    if (m_mode == "neutral")
    {
        setPixmap(*m_green);
        m_mode = "green";
    }
    else if (m_mode == "green")
    {
        setPixmap(*m_neutral);
        m_mode = "neutral";
    }
}

void Hero::toggleBlueColor()
{
    if (m_mode == "neutral")
    {
        setPixmap(*m_blue);
        m_mode = "blue";
    }
    else if (m_mode == "blue")
    {
        setPixmap(*m_neutral);
        m_mode = "neutral";
    }
}


b2Fixture* Hero::getFixture()
{
    return m_fixture;
}