PageRenderTime 23ms CodeModel.GetById 12ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/Player.h

http://cutefootball.googlecode.com/
C++ Header | 192 lines | 129 code | 37 blank | 26 comment | 0 complexity | ba563e8a6b4be135629d1870a1fda746 MD5 | raw file
  1/*
  2 * Copyright 2010,2011 Timothy Rochford
  3 *
  4 *    This file is part of CuteFootball.
  5 *
  6 *    CuteFootball is free software: you can redistribute it and/or modify
  7 *    it under the terms of the Lesser GNU General Public License as published by
  8 *    the Free Software Foundation, either version 3 of the License, or
  9 *    (at your option) any later version.
 10 *
 11 *    CuteFootball is distributed in the hope that it will be useful,
 12 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 13 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 14 *    Lesser GNU General Public License for more details.
 15 *
 16 *    You should have received a copy of the Lesser GNU General Public License
 17 *    along with CuteFootball.  If not, see <http://www.gnu.org/licenses/>.
 18 *
 19 */
 20#ifndef PLAYER_H
 21#define PLAYER_H
 22
 23#include <QtGui>
 24#include <QGraphicsPixmapItem>
 25#include <QStyleOptionGraphicsItem>
 26#include <QPainter>
 27#include <qmap.h>
 28
 29#include "team.h"
 30#include "pitch.h"
 31
 32class QFocusEvent;
 33
 34typedef QList<QPixmap> PixmapList;
 35
 36class Player : public QObject,
 37               public QGraphicsPixmapItem
 38{
 39    Q_OBJECT
 40
 41public:
 42    enum Role {GoalKeeper = 1,
 43               LeftDefence,
 44               LeftCentralDefence,
 45               CentralDefence,
 46               RightCentralDefence,
 47               RightDefence,
 48               LeftMidfield,
 49               CentralMidfield,
 50               RightMidfield,
 51               LeftAttack,
 52               CentralAttack,
 53               RightAttack,
 54               LastDummy };
 55
 56    Player(QString name,
 57           int number,
 58           bool computerControlled,
 59           Pitch* pitch,
 60           Team* team,
 61           qreal speed,
 62           Player::Role role,
 63           QColor hairColor,
 64           QColor skinColor);
 65    virtual ~Player();
 66
 67    enum { Type = UserType + 2 };
 68
 69    virtual int type() const
 70        { return Type;}
 71
 72    virtual void advance(int phase);
 73    virtual void createPixmaps();
 74
 75public: // From QGraphicsItem
 76    void mousePressEvent(QGraphicsSceneMouseEvent *e);
 77
 78protected: // From QGraphicsItem
 79    void focusInEvent(QFocusEvent * event);
 80    void focusOutEvent(QFocusEvent * event);
 81
 82    void keyPressEvent(QKeyEvent *event);
 83    void keyReleaseEvent(QKeyEvent *event);
 84
 85
 86public:
 87    bool ballCollisionCheck() const;
 88    bool playerCollisionCheck() const;
 89
 90    inline void setDestination(QPointF dest) { m_destination = dest; }
 91    void move(MWindow::Action action);
 92    void movePlayer(MWindow::Action action);
 93    Player* findAvailableTeamMate(QPointF myPos) const;
 94    void specialAction(MWindow::Action action);
 95    void setTackled();
 96    inline void setRequiredNextAction(MWindow::Action a) { m_requiredNextAction = a; }
 97
 98    bool withinShootingDistance() const;
 99
100    inline qreal speed() const { return m_speed; }
101    inline void setHasBall(bool hasBall) { m_hasBall = hasBall; }
102    inline bool hasBall() const { return m_hasBall; }
103    inline Team* team() const { return m_team; }
104
105    inline void setAllowedOffPitch(bool isAllowed) { m_allowedOffPitch = isAllowed; }
106    inline void setAllowedInCenterCircle(bool isAllowed) { m_allowedInCenterCircle = isAllowed; }
107
108    inline QString name() const { return m_name; }
109
110private slots:
111    void repeatKeyEvent();
112    void standupPlayer() { setPixmap(m_images[m_lastAction].at(0)); }
113    void foulEventStart(Team* t, QPointF foulLocation);
114
115protected:
116    QRectF boundingRect() const;
117    QPainterPath shape() const;
118    void paint(QPainter *painter,
119                          const QStyleOptionGraphicsItem *option,
120                          QWidget *widget);
121    QVariant itemChange(GraphicsItemChange change, const QVariant &value);
122    void pixmapInsert(MWindow::Action a,
123                      QString s1,
124                      QString s2,
125                      QString s3,
126                      QRgb shirtColor,
127                      QRgb shortColor);
128
129private:
130    void calculatePlayerDestination(MWindow::Action act);
131    void humanAdvance(int phase);
132    void computerAdvance(int phase);
133    void computerAdvanceWithBall();
134    void computerAdvanceWithoutBall();
135
136    void automove();
137
138    void stopKeyEvent();
139    void createKeyboardActions();
140    QPointF calculateBallDestination(MWindow::Action act);
141
142public:
143    bool m_hasBall;
144    Role m_role;
145    QRectF m_startPositionRectF;
146    // player will defend this zone of pitch
147    QRectF m_defendZone;
148
149private:
150    QString m_name;
151    int m_number;
152
153    QTime m_elapsedTime;
154    QMap<int,MWindow::Action> m_actions;
155    QTimer *m_keyEventTimer;
156    // last position of the player
157    QPointF m_lastPos;
158
159
160protected:
161    Team* m_team;
162    // the previous action of this player
163    MWindow::Action m_lastAction;
164    // pressing the same directional key a second time must stop the player movement
165    int m_lastKeyEvent;
166    QMap<MWindow::Action,PixmapList> m_images;
167    Pitch *m_pitch;
168    int m_step;
169    QTimer *m_outOfAction;
170
171    bool m_allowedOffPitch;
172    bool m_allowedInCenterCircle;
173
174    qreal m_speed;
175
176    QColor m_hairColor;
177    QColor m_skinColor;
178
179    // Displaying player name/number:
180    QString m_toolTipText;
181    QPen m_toolTipPen;
182    QPointF m_toolTipTextPos;
183
184    // player position fixed until this is false
185    bool m_positionLocked;
186    MWindow::Action m_requiredNextAction;
187
188    // where the player wants to go
189    QPointF m_destination;
190};
191
192#endif // PLAYER_H