/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. #include <QtGui>
  23. #include <QGraphicsPixmapItem>
  24. #include <QStyleOptionGraphicsItem>
  25. #include <QPainter>
  26. #include <qmap.h>
  27. #include "team.h"
  28. #include "pitch.h"
  29. class QFocusEvent;
  30. typedef QList<QPixmap> PixmapList;
  31. class Player : public QObject,
  32. public QGraphicsPixmapItem
  33. {
  34. Q_OBJECT
  35. public:
  36. enum Role {GoalKeeper = 1,
  37. LeftDefence,
  38. LeftCentralDefence,
  39. CentralDefence,
  40. RightCentralDefence,
  41. RightDefence,
  42. LeftMidfield,
  43. CentralMidfield,
  44. RightMidfield,
  45. LeftAttack,
  46. CentralAttack,
  47. RightAttack,
  48. LastDummy };
  49. Player(QString name,
  50. int number,
  51. bool computerControlled,
  52. Pitch* pitch,
  53. Team* team,
  54. qreal speed,
  55. Player::Role role,
  56. QColor hairColor,
  57. QColor skinColor);
  58. virtual ~Player();
  59. enum { Type = UserType + 2 };
  60. virtual int type() const
  61. { return Type;}
  62. virtual void advance(int phase);
  63. virtual void createPixmaps();
  64. public: // From QGraphicsItem
  65. void mousePressEvent(QGraphicsSceneMouseEvent *e);
  66. protected: // From QGraphicsItem
  67. void focusInEvent(QFocusEvent * event);
  68. void focusOutEvent(QFocusEvent * event);
  69. void keyPressEvent(QKeyEvent *event);
  70. void keyReleaseEvent(QKeyEvent *event);
  71. public:
  72. bool ballCollisionCheck() const;
  73. bool playerCollisionCheck() const;
  74. inline void setDestination(QPointF dest) { m_destination = dest; }
  75. void move(MWindow::Action action);
  76. void movePlayer(MWindow::Action action);
  77. Player* findAvailableTeamMate(QPointF myPos) const;
  78. void specialAction(MWindow::Action action);
  79. void setTackled();
  80. inline void setRequiredNextAction(MWindow::Action a) { m_requiredNextAction = a; }
  81. bool withinShootingDistance() const;
  82. inline qreal speed() const { return m_speed; }
  83. inline void setHasBall(bool hasBall) { m_hasBall = hasBall; }
  84. inline bool hasBall() const { return m_hasBall; }
  85. inline Team* team() const { return m_team; }
  86. inline void setAllowedOffPitch(bool isAllowed) { m_allowedOffPitch = isAllowed; }
  87. inline void setAllowedInCenterCircle(bool isAllowed) { m_allowedInCenterCircle = isAllowed; }
  88. inline QString name() const { return m_name; }
  89. private slots:
  90. void repeatKeyEvent();
  91. void standupPlayer() { setPixmap(m_images[m_lastAction].at(0)); }
  92. void foulEventStart(Team* t, QPointF foulLocation);
  93. protected:
  94. QRectF boundingRect() const;
  95. QPainterPath shape() const;
  96. void paint(QPainter *painter,
  97. const QStyleOptionGraphicsItem *option,
  98. QWidget *widget);
  99. QVariant itemChange(GraphicsItemChange change, const QVariant &value);
  100. void pixmapInsert(MWindow::Action a,
  101. QString s1,
  102. QString s2,
  103. QString s3,
  104. QRgb shirtColor,
  105. QRgb shortColor);
  106. private:
  107. void calculatePlayerDestination(MWindow::Action act);
  108. void humanAdvance(int phase);
  109. void computerAdvance(int phase);
  110. void computerAdvanceWithBall();
  111. void computerAdvanceWithoutBall();
  112. void automove();
  113. void stopKeyEvent();
  114. void createKeyboardActions();
  115. QPointF calculateBallDestination(MWindow::Action act);
  116. public:
  117. bool m_hasBall;
  118. Role m_role;
  119. QRectF m_startPositionRectF;
  120. // player will defend this zone of pitch
  121. QRectF m_defendZone;
  122. private:
  123. QString m_name;
  124. int m_number;
  125. QTime m_elapsedTime;
  126. QMap<int,MWindow::Action> m_actions;
  127. QTimer *m_keyEventTimer;
  128. // last position of the player
  129. QPointF m_lastPos;
  130. protected:
  131. Team* m_team;
  132. // the previous action of this player
  133. MWindow::Action m_lastAction;
  134. // pressing the same directional key a second time must stop the player movement
  135. int m_lastKeyEvent;
  136. QMap<MWindow::Action,PixmapList> m_images;
  137. Pitch *m_pitch;
  138. int m_step;
  139. QTimer *m_outOfAction;
  140. bool m_allowedOffPitch;
  141. bool m_allowedInCenterCircle;
  142. qreal m_speed;
  143. QColor m_hairColor;
  144. QColor m_skinColor;
  145. // Displaying player name/number:
  146. QString m_toolTipText;
  147. QPen m_toolTipPen;
  148. QPointF m_toolTipTextPos;
  149. // player position fixed until this is false
  150. bool m_positionLocked;
  151. MWindow::Action m_requiredNextAction;
  152. // where the player wants to go
  153. QPointF m_destination;
  154. };
  155. #endif // PLAYER_H