Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
KGoldrunner
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Games
KGoldrunner
Commits
4078d305
Commit
4078d305
authored
Mar 07, 2009
by
Ian Wadham
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Starting to handle enemy-to-enemy collisions. Added some API documentation.
svn path=/branches/work/kgoldrunner/; revision=936246
parent
ce952bf4
Changes
11
Hide whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
446 additions
and
144 deletions
+446
-144
src/kgrcanvas.cpp
src/kgrcanvas.cpp
+5
-5
src/kgrgame.cpp
src/kgrgame.cpp
+4
-0
src/kgrgame.h
src/kgrgame.h
+4
-0
src/kgrglobals.h
src/kgrglobals.h
+6
-7
src/kgrlevelgrid.cpp
src/kgrlevelgrid.cpp
+1
-0
src/kgrlevelgrid.h
src/kgrlevelgrid.h
+9
-0
src/kgrlevelplayer.cpp
src/kgrlevelplayer.cpp
+83
-27
src/kgrlevelplayer.h
src/kgrlevelplayer.h
+173
-8
src/kgrrunner.cpp
src/kgrrunner.cpp
+149
-90
src/kgrrunner.h
src/kgrrunner.h
+8
-4
src/kgrtimer.cpp
src/kgrtimer.cpp
+4
-3
No files found.
src/kgrcanvas.cpp
View file @
4078d305
...
...
@@ -477,7 +477,7 @@ void KGrCanvas::updateLives (int lives)
void
KGrCanvas
::
mousePressEvent
(
QMouseEvent
*
mouseEvent
)
{
kDebug
()
<<
"Button"
<<
mouseEvent
->
button
();
//
kDebug() << "Button" << mouseEvent->button();
emit
mouseClick
(
mouseEvent
->
button
());
}
...
...
@@ -560,8 +560,8 @@ int KGrCanvas::makeSprite (const char type, int i, int j)
sprite
->
setZ
(
z
);
sprite
->
show
();
kDebug
()
<<
"Sprite ID"
<<
spriteId
<<
"sprite type"
<<
type
<<
"at"
<<
i
<<
j
;
//
kDebug() << "Sprite ID" << spriteId << "sprite type" << type
//
<< "at" << i << j;
return
spriteId
;
}
...
...
@@ -626,7 +626,7 @@ void KGrCanvas::startAnimation (const int id, const bool repeating,
default:
break
;
}
kDebug
()
<<
"id"
<<
id
<<
"data"
<<
i
<<
j
<<
dx
*
bgw
<<
dy
*
bgw
<<
frame
<<
time
;
//
kDebug() << "id" << id << "data" << i << j << dx * bgw << dy * bgw << frame << time;
// TODO - Generalise nFrameChanges = 4, also the tick time = 20 new sprite.
sprites
->
at
(
id
)
->
setAnimation
(
repeating
,
(
i
-
1
)
*
bgw
,
(
j
-
1
)
*
bgh
,
frame
,
nFrames
,
dx
*
bgw
,
dy
*
bgh
,
time
,
nFrameChanges
);
...
...
@@ -673,7 +673,7 @@ void KGrCanvas::deleteSprite (const int spriteId)
// Dug-brick sprite erased: restore the tile that was at that location.
paintCell
((
loc
.
x
()
/
bgw
)
+
1
,
(
loc
.
y
()
/
bgh
)
+
1
,
BRICK
,
0
);
}
kDebug
()
<<
"Sprite ID"
<<
spriteId
<<
"emptySprites"
<<
emptySprites
;
//
kDebug() << "Sprite ID" << spriteId << "emptySprites" << emptySprites;
}
void
KGrCanvas
::
deleteAllSprites
()
...
...
src/kgrgame.cpp
View file @
4078d305
...
...
@@ -142,6 +142,10 @@ KGrGame::~KGrGame()
// delete collections.takeFirst();
}
// Flags to control author's debugging aids.
bool
KGrGame
::
bugFix
=
false
;
// Start game with dynamic bug-fix OFF.
bool
KGrGame
::
logging
=
false
;
// Start game with dynamic logging OFF.
void
KGrGame
::
gameActions
(
int
action
)
{
switch
(
action
)
{
...
...
src/kgrgame.h
View file @
4078d305
...
...
@@ -78,6 +78,10 @@ public:
inline
bool
isFrozen
()
{
return
gameFrozen
;
}
// Flags to control author's debugging aids.
static
bool
bugFix
;
static
bool
logging
;
public
slots
:
void
kbControl
(
int
dirn
);
// xxxxxxxxxxxxxx
...
...
src/kgrglobals.h
View file @
4078d305
...
...
@@ -36,7 +36,7 @@ public:
QByteArray
hint
;
///< Level hint (optional).
};
enum
GameAction
{
HINT
,
KILL_HERO
};
enum
GameAction
{
HINT
,
KILL_HERO
};
const
int
ConcreteWall
=
1
;
...
...
@@ -44,10 +44,7 @@ typedef char DirectionFlag;
typedef
char
AccessFlag
;
typedef
char
Flags
;
enum
Axis
{
X
,
Y
,
nAxes
};
typedef
int
Vector2D
[
nAxes
];
enum
Direction
{
STAND
,
RIGHT
,
LEFT
,
UP
,
DOWN
,
nDirections
,
enum
Direction
{
STAND
,
RIGHT
,
LEFT
,
UP
,
DOWN
,
nDirections
,
DIG_RIGHT
=
nDirections
,
DIG_LEFT
};
const
DirectionFlag
dFlag
[
nDirections
]
=
{
...
...
@@ -59,6 +56,8 @@ const DirectionFlag dFlag [nDirections] = {
const
AccessFlag
ENTERABLE
=
0x20
;
enum
Axis
{
X
,
Y
,
nAxes
};
const
int
movement
[
nDirections
][
nAxes
]
=
{
{
0
,
0
},
// Standing still.
{
+
1
,
0
},
// Movement right.
...
...
@@ -76,13 +75,13 @@ enum AnimationType {
const
AnimationType
aType
[
nDirections
]
=
{
FALL_L
,
RUN_R
,
RUN_L
,
CLIMB_U
,
CLIMB_D
};
enum
DebugCodes
{
enum
DebugCodes
{
DO_STEP
,
BUG_FIX
,
LOGGING
,
S_POSNS
,
S_HERO
,
S_OBJ
,
ENEMY_0
,
ENEMY_1
,
ENEMY_2
,
ENEMY_3
,
ENEMY_4
,
ENEMY_5
,
ENEMY_6
};
const
int
TickTime
=
20
;
enum
HeroStatus
{
enum
HeroStatus
{
NORMAL
,
WON_LEVEL
,
DEAD
};
#endif // KGRGLOBALS_H
src/kgrlevelgrid.cpp
View file @
4078d305
...
...
@@ -37,6 +37,7 @@ KGrLevelGrid::KGrLevelGrid (QObject * parent, const KGrLevelData * theLevelData)
// Initialise the flags for each cell.
heroAccess
.
fill
(
0
,
size
);
enemyAccess
.
fill
(
0
,
size
);
enemyHere
.
fill
(
-
1
,
size
);
// Copy the cells of the layout, but enclosed within the concrete wall.
int
inRow
=
0
;
...
...
src/kgrlevelgrid.h
View file @
4078d305
...
...
@@ -48,6 +48,14 @@ public:
layout
[
i
+
j
*
width
]
=
(
runnerHasGold
)
?
FREE
:
NUGGET
;
}
inline
int
enemyOccupied
(
int
i
,
int
j
)
{
return
enemyHere
[
i
+
j
*
width
];
}
inline
void
setEnemyOccupied
(
int
i
,
int
j
,
const
int
spriteId
)
{
enemyHere
[
i
+
j
*
width
]
=
spriteId
;
}
void
calculateAccess
(
bool
pRunThruHole
);
void
changeCellAt
(
const
int
i
,
const
int
j
,
const
char
type
);
...
...
@@ -72,6 +80,7 @@ private:
QVector
<
char
>
layout
;
QVector
<
Flags
>
heroAccess
;
QVector
<
Flags
>
enemyAccess
;
QVector
<
int
>
enemyHere
;
QList
<
int
>
hiddenLadders
;
QList
<
int
>
hiddenEnemies
;
...
...
src/kgrlevelplayer.cpp
View file @
4078d305
...
...
@@ -22,6 +22,9 @@
#include <QTimer>
#include <QList>
// Include kgrgame.h only to access flags KGrGame::bugFix and KGrGame::logging.
#include "kgrgame.h"
#include "kgrcanvas.h"
#include "kgrlevelplayer.h"
#include "kgrrulebook.h"
...
...
@@ -47,8 +50,6 @@ KGrLevelPlayer::KGrLevelPlayer (QObject * parent)
digKillingTime
(
2
)
// Cycle at which enemy/hero gets killed.
{
t
.
start
();
// IDW
gameLogging
=
false
;
bugFixed
=
false
;
}
KGrLevelPlayer
::~
KGrLevelPlayer
()
...
...
@@ -66,15 +67,14 @@ KGrLevelPlayer::~KGrLevelPlayer()
void
KGrLevelPlayer
::
init
(
KGrCanvas
*
view
,
const
Control
mode
,
const
char
rulesCode
,
const
KGrLevelData
*
levelData
)
{
// TODO - Should not really remember the view: needed for setMousePos.
mView
=
view
;
// Create the internal model of the level-layout.
grid
=
new
KGrLevelGrid
(
this
,
levelData
);
controlMode
=
mode
;
controlMode
=
mode
;
// Set mouse/keyboard/laptop control.
levelWidth
=
levelData
->
width
;
levelHeight
=
levelData
->
height
;
reappearIndex
=
levelWidth
;
reappearIndex
=
levelWidth
;
// Initialise the enemy-rebirth code.
reappearPos
.
fill
(
1
,
levelWidth
);
// Set the rules of this game.
...
...
@@ -89,9 +89,11 @@ void KGrLevelPlayer::init (KGrCanvas * view, const Control mode,
rules
=
new
KGrScavengerRules
(
this
);
break
;
}
rules
->
printRules
();
// TODO - Remove.
rules->printRules();
view
->
setGoldEnemiesRule
(
rules
->
enemiesShowGold
());
// Determine the access for hero and enemies to and from each grid-cell.
grid
->
calculateAccess
(
rules
->
runThruHole
());
// Connect to code that paints grid cells and start-positions of sprites.
...
...
@@ -143,7 +145,6 @@ void KGrLevelPlayer::init (KGrCanvas * view, const Control mode,
rules
->
setTiming
(
enemyCount
);
rules
->
getDigTimes
(
digCycleTime
,
digCycleCount
);
// TODO - Do hero in pass 1 and enemies in pass 2, to ensure the hero has id 0.
// Create the hero (always sprite 0), with the proper timing.
for
(
int
j
=
wall
;
j
<
levelHeight
+
wall
;
j
++
)
{
for
(
int
i
=
wall
;
i
<
levelWidth
+
wall
;
i
++
)
{
...
...
@@ -172,6 +173,7 @@ void KGrLevelPlayer::init (KGrCanvas * view, const Control mode,
enemy
=
new
KGrEnemy
(
this
,
grid
,
i
,
j
,
id
,
rules
);
enemies
.
append
(
enemy
);
grid
->
changeCellAt
(
i
,
j
,
FREE
);
// Enemy now a sprite.
grid
->
setEnemyOccupied
(
i
,
j
,
id
);
}
}
}
...
...
@@ -183,7 +185,7 @@ void KGrLevelPlayer::init (KGrCanvas * view, const Control mode,
// Connect mouse-clicks from KGrCanvas to digging slot.
connect
(
view
,
SIGNAL
(
mouseClick
(
int
)),
SLOT
(
doDig
(
int
)));
// Connect the
new
hero and enemies (if any) to the animation code.
// Connect the hero and enemies (if any) to the animation code.
connect
(
hero
,
SIGNAL
(
startAnimation
(
int
,
bool
,
int
,
int
,
int
,
Direction
,
AnimationType
)),
view
,
SLOT
(
startAnimation
(
int
,
bool
,
int
,
int
,
int
,
...
...
@@ -214,7 +216,6 @@ void KGrLevelPlayer::init (KGrCanvas * view, const Control mode,
connect
(
timer
,
SIGNAL
(
tick
(
bool
,
int
)),
this
,
SLOT
(
tick
(
bool
,
int
)));
connect
(
this
,
SIGNAL
(
animation
(
bool
)),
view
,
SLOT
(
animate
(
bool
)));
// timer->start (TickTime); // Interval = TickTime, defined in kgrglobals.h.
}
void
KGrLevelPlayer
::
startDigging
(
Direction
diggingDirection
)
...
...
@@ -255,7 +256,6 @@ void KGrLevelPlayer::processDugBricks (const int scaledTime)
dugBrick
=
iterator
.
next
();
dugBrick
->
cycleTimeLeft
-=
scaledTime
;
if
(
dugBrick
->
cycleTimeLeft
<
scaledTime
)
{
int
id
=
dugBrick
->
id
;
// IDW testing
dugBrick
->
cycleTimeLeft
+=
digCycleTime
;
if
(
--
dugBrick
->
countdown
==
digClosingCycles
)
{
// Start the brick-closing animation (non-repeating).
...
...
@@ -273,8 +273,8 @@ void KGrLevelPlayer::processDugBricks (const int scaledTime)
grid
->
changeCellAt
(
dugBrick
->
digI
,
dugBrick
->
digJ
,
BRICK
);
}
if
(
dugBrick
->
countdown
<=
0
)
{
kDebug
()
<<
"DIG"
<<
id
<<
dugBrick
->
countdown
<<
"time"
<<
(
t
.
elapsed
()
-
dugBrick
->
startTime
);
// kDebug() << "DIG" << dugBrick->
id << dugBrick->countdown
//
<< "time" << (t.elapsed() - dugBrick->startTime);
// Dispose of the dug brick and remove it from the list.
emit
deleteSprite
(
dugBrick
->
id
);
// TODO - Remove. emit paintCell (dugBrick->digI, dugBrick->digJ, BRICK);
...
...
@@ -444,9 +444,66 @@ bool KGrLevelPlayer::standOnEnemy (const int spriteId, const int x, const int y)
return
false
;
}
bool
KGrLevelPlayer
::
bumpingFriend
(
const
int
id
,
const
int
x
,
const
int
y
)
bool
KGrLevelPlayer
::
bumpingFriend
(
const
int
spriteId
,
const
Direction
dirn
,
const
int
gridI
,
const
int
gridJ
)
{
// TODO - Write this.
// if (spriteId != grid->enemyOccupied (gridI, gridJ)) {
// kDebug() << spriteId << "CANNOT MOVE FROM" << gridI << gridJ
// << grid->enemyOccupied (gridI, gridJ) << "MUST GO FIRST";
// return true;
// }
int
dI
=
0
;
int
dJ
=
0
;
switch
(
dirn
)
{
case
LEFT
:
dI
=
-
1
;
break
;
case
RIGHT
:
dI
=
+
1
;
break
;
case
UP
:
dJ
=
-
1
;
break
;
case
DOWN
:
dJ
=
+
1
;
break
;
default:
break
;
}
int
otherEnemy
;
if
(
dI
!=
0
)
{
otherEnemy
=
grid
->
enemyOccupied
(
gridI
+
dI
,
gridJ
);
kDebug
()
<<
otherEnemy
<<
"at"
<<
(
gridI
+
dI
)
<<
gridJ
<<
"dirn"
<<
((
otherEnemy
>
0
)
?
(
enemies
.
at
(
otherEnemy
-
1
)
->
direction
())
:
0
)
<<
"me"
<<
spriteId
<<
"dirn"
<<
dirn
;
if
(
otherEnemy
>
0
)
{
if
(
enemies
.
at
(
otherEnemy
-
1
)
->
direction
()
!=
dirn
)
{
kDebug
()
<<
spriteId
<<
"wants"
<<
dirn
<<
":"
<<
otherEnemy
<<
"at"
<<
(
gridI
+
dI
)
<<
gridJ
<<
"wants"
<<
(
enemies
.
at
(
otherEnemy
-
1
)
->
direction
());
return
true
;
}
}
}
if
(
dJ
!=
0
)
{
otherEnemy
=
grid
->
enemyOccupied
(
gridI
,
gridJ
+
dJ
);
kDebug
()
<<
otherEnemy
<<
"at"
<<
gridI
<<
(
gridJ
+
dJ
)
<<
"dirn"
<<
((
otherEnemy
>
0
)
?
(
enemies
.
at
(
otherEnemy
-
1
)
->
direction
())
:
0
)
<<
"me"
<<
spriteId
<<
"dirn"
<<
dirn
;
if
(
otherEnemy
>
0
)
{
if
(
enemies
.
at
(
otherEnemy
-
1
)
->
direction
()
!=
dirn
)
{
kDebug
()
<<
spriteId
<<
"wants"
<<
dirn
<<
":"
<<
otherEnemy
<<
"at"
<<
gridI
<<
(
gridJ
+
dJ
)
<<
"wants"
<<
(
enemies
.
at
(
otherEnemy
-
1
)
->
direction
());
return
true
;
}
}
}
return
false
;
}
...
...
@@ -520,13 +577,12 @@ void KGrLevelPlayer::makeReappearanceSequence()
for
(
int
k
=
0
;
k
<
levelWidth
;
k
++
)
{
reappearPos
[
k
]
=
k
+
1
;
}
kDebug
()
<<
"Width"
<<
levelWidth
<<
"Non-randoms"
<<
reappearPos
;
int
z
;
int
left
=
levelWidth
;
int
temp
;
// Shuffle the
x co-ords of reappearance positions, for x = 1 to levelWidth
.
// Shuffle the
co-ordinates of reappearance positions (1 to levelWidth)
.
for
(
int
k
=
0
;
k
<
levelWidth
;
k
++
)
{
// Pick a random element from those that are left.
z
=
(
int
)((
left
*
(
float
)
rand
())
/
RAND_MAX
);
...
...
@@ -593,8 +649,7 @@ void KGrLevelPlayer::dbgControl (int code)
{
switch
(
code
)
{
case
DO_STEP
:
// tick (false); // Do one timer step only.
timer
->
step
();
timer
->
step
();
// Do one timer step only.
break
;
case
BUG_FIX
:
bugFix
();
// Turn a bug fix on/off dynamically.
...
...
@@ -658,17 +713,17 @@ void KGrLevelPlayer::restart()
void
KGrLevelPlayer
::
bugFix
()
{
// Toggle a bug fix on/off dynamically.
bugFixed
=
(
bugFixed
)
?
false
:
true
;
printf
(
"%s"
,
(
bugFixed
)
?
"
\n
"
:
""
);
printf
(
">>> Bug fix is %s
\n
"
,
(
bugFixed
)
?
"ON"
:
"OFF
\n
"
);
KGrGame
::
bugFix
=
(
KGrGame
::
bugFix
)
?
false
:
true
;
printf
(
"%s"
,
(
KGrGame
::
bugFix
)
?
"
\n
"
:
""
);
printf
(
">>> Bug fix is %s
\n
"
,
(
KGrGame
::
bugFix
)
?
"ON"
:
"OFF
\n
"
);
}
void
KGrLevelPlayer
::
startLogging
()
{
// Toggle logging on/off dynamically.
gameLogging
=
(
gameL
ogging
)
?
false
:
true
;
printf
(
"%s"
,
(
gameL
ogging
)
?
"
\n
"
:
""
);
printf
(
">>> Logging is %s
\n
"
,
(
gameL
ogging
)
?
"ON"
:
"OFF
\n
"
);
KGrGame
::
logging
=
(
KGrGame
::
l
ogging
)
?
false
:
true
;
printf
(
"%s"
,
(
KGrGame
::
l
ogging
)
?
"
\n
"
:
""
);
printf
(
">>> Logging is %s
\n
"
,
(
KGrGame
::
l
ogging
)
?
"ON"
:
"OFF
\n
"
);
}
void
KGrLevelPlayer
::
showFigurePositions
()
...
...
@@ -685,6 +740,7 @@ void KGrLevelPlayer::showObjectState()
int
j
=
targetJ
;
char
here
=
grid
->
cellType
(
i
,
j
);
Flags
access
=
grid
->
heroMoves
(
i
,
j
);
int
enemyId
=
grid
->
enemyOccupied
(
i
,
j
);
int
enter
=
(
access
&
ENTERABLE
)
?
1
:
0
;
int
stand
=
(
access
&
dFlag
[
STAND
])
?
1
:
0
;
...
...
@@ -693,8 +749,8 @@ void KGrLevelPlayer::showObjectState()
int
l
=
(
access
&
dFlag
[
LEFT
])
?
1
:
0
;
int
r
=
(
access
&
dFlag
[
RIGHT
])
?
1
:
0
;
fprintf
(
stderr
,
"[%02d,%02d] [%c] %02x E %d S %d U %d D %d L %d R %d
\n
"
,
i
,
j
,
here
,
access
,
enter
,
stand
,
u
,
d
,
l
,
r
);
"[%02d,%02d] [%c] %02x E %d S %d U %d D %d L %d R %d
occ %02d
\n
"
,
i
,
j
,
here
,
access
,
enter
,
stand
,
u
,
d
,
l
,
r
,
enemyId
);
Flags
eAccess
=
grid
->
enemyMoves
(
i
,
j
);
if
(
eAccess
!=
access
)
{
...
...
src/kgrlevelplayer.h
View file @
4078d305
...
...
@@ -35,35 +35,207 @@ class KGrCanvas;
class
KGrHero
;
class
KGrEnemy
;
/**
* This class constructs and plays a single level of a KGoldrunner game. A
* KGrLevelPlayer object is created as each level begins and is destroyed as
* the level finishes, whether the human player wins the level or loses it.
*
* The KGrLevelPlayer object in turn creates all the objects needed to play
* the level, such as a hero, enemies, a play-area grid and a rule-book, which
* are all derived from data in a KGrLevelData structure. After the level and
* all its objects are set up, most of the work is done by the private slot
* tick(), including a signal to update the graphics animation. Tick() is
* activated periodically by time-signals from the KGrTimer object, which also
* handles the standard Pause action and variations in the overall speed of
* KGoldrunner, from Beginner to Champion. KGrLevelPlayer is controlled by
* inputs from the mouse, keyboard or touchpad, which in turn control the
* movement of the hero and the digging of bricks. The enemies are controlled
* by algorithms in the polymorphic KGrRuleBook object. Each set of rules has
* its own distinct algorithm.
*
* KGrLevelPlayer and friends are the internal model and game-engine of
* KGoldrunner and they communicate with the view, KGrCanvas and friends,
* solely via signals that indicate what is moving and what has to be painted.
*/
class
KGrLevelPlayer
:
public
QObject
{
Q_OBJECT
public:
/**
* The constructor of KGrLevelPlayer.
*
* @param parent The object that owns the level-player and will destroy it
* if the KGoldrunner application is terminated during play.
*/
KGrLevelPlayer
(
QObject
*
parent
);
~
KGrLevelPlayer
();
/**
* The main initialisation of KGrLevelPlayer. This method establishes the
* playing rules to be used, creates the internal playing-grid, creates the
* hero and enemies and connects the various signals and slots together.
* This is the only place where KGrLevelPlayer references the view directly.
* All other references are via signals and slots.
*
* @param view Points to the KGrCanvas object, which provides graphics.
* @param mode The input-mode used to control the hero: mouse, keyboard
* or hybrid touchpad and keyboard mode (for laptops).
* @param rulesCode The variant of the rules to be followed: Traditional,
* KGoldrunner or Scavenger.
* @param levelData Points to a data-object that contains all the data for
* the level, including the layout of the maze and the
* starting positions of hero, enemies and gold.
*/
void
init
(
KGrCanvas
*
view
,
const
Control
mode
,
const
char
rulesCode
,
const
KGrLevelData
*
levelData
);
/**
* Indicate that setup is complete. The human player can start playing
* at any time, by moving the pointer device or pressing a key.
*/
void
prepareToPlay
();
/**
* Allow the input-mode to change during play.
*
* @param mode The new input-mode to use to control the hero: mouse,
* keyboard or hybrid touchpad and keyboard mode.
*/
inline
void
setControlMode
(
const
Control
mode
)
{
controlMode
=
mode
;
}
/**
* Set a point for the hero to aim at when using mouse or touchpad control.
*
* @param pointerI The required column-number on the playing-grid (>=1).
* @param pointerJ The required row-number on the playing-grid (>=1).
*/
void
setTarget
(
int
pointerI
,
int
pointerJ
);
/**
* Set a direction for the hero to move or dig when using keyboard control.
*
* @param dirn The required direction (values defined by enum Direction
* in file kgrglobals.h).
*/
void
setDirectionByKey
(
Direction
dirn
);
/**
* Helper function for the hero to find his next direction when using mouse
* or touchpad control. Uses the point from setTarget() as a guide.
*
* @param heroI The column-number where the hero is now (>=1).
* @param heroJ The row-number where the hero is now (>=1).
*
* @return The required direction (values defined by enum Direction
* in file kgrglobals.h).
*/
Direction
getDirection
(
int
heroI
,
int
heroJ
);
/**
* Helper function for an enemy to find his next direction, based on where
* the hero is and the search algorithm implemented in the level's rules.
*
* @param enemyI The column-number where the enemy is now (>=1).
* @param enemyJ The row-number where the enemy is now (>=1).
*
* @return The required direction (values defined by enum Direction
* in file kgrglobals.h).
*/
Direction
getEnemyDirection
(
int
enemyI
,
int
enemyJ
);
/**
* Helper function for an enemy to pick up or drop gold or the hero to
* collect gold. Records the presence or absence of the gold on the
* internal grid and on the screen. Also pops up the hidden ladders (if
* any) when there is no gold left.
*
* @param spriteId The identifier of the hero or enemy.
* @param i The column-number where the gold is (>=1).
* @param j The row-number where the gold is (>=1).
* @param hasGold True if gold was picked up: false if it was dropped.
*
* @return The number of pieces of gold remaining in this level.
*/
int
runnerGotGold
(
const
int
spriteId
,
const
int
i
,
const
int
j
,
const
bool
hasGold
);
/**
* Helper function to determine whether the hero has collided with an enemy
* and must lose a life (unless he is standing on the enemy's head).
*
* @param heroX The X grid-position of the hero (within a cell).
* @param heroY The Y grid-position of the hero (within a cell).
*
* @return True if the hero is touching an enemy.
*/
bool
heroCaught
(
const
int
heroX
,
const
int
heroY
);
/**
* Helper function to determine whether the hero or an enemy is standing on
* an enemy's head.
*
* @param spriteId The identifier of the hero or enemy.
* @param x The X grid-position of the sprite (within a cell).
* @param y The Y grid-position of the sprite (within a cell).
*
* @return True if the hero or enemy is standing on an enemy.
*/
bool
standOnEnemy
(
const
int
spriteId
,
const
int
x
,
const
int
y
);
bool
bumpingFriend
(
const
int
spriteId
,
const
int
x
,
const
int
y
);
/**
* Helper function to determine whether an enemy is colliding with another
* enemy. This to prevent enemies occupying the same cell, depending on
* what the rules for this level allow.
*
* @param spriteId The identifier of the enemy.
* @param dirn The direction in which the enemy wants to go.
* @param gridI The column-position of the enemy.
* @param gridJ The row-position of the enemy.
*
* @return True if the enemy is too close to another enemy.
*/
bool
bumpingFriend
(
const
int
spriteId
,
const
Direction
dirn
,
const
int
gridI
,
const
int
gridJ
);
/**
* Helper function to determine where an enemy should reappear after being
* trapped in a brick. This applies with Traditional and Scavenger rules
* only. The procedure chooses a random place in row 2 or row 1.
*
* @param gridI A randomly-chosen column (return by reference).
* @param gridJ Row 2 Traditional or 1 Scavenger (return by reference).
*/
void
enemyReappear
(
int
&
gridI
,
int
&
gridJ
);
/**
* Pauses or resumes the gameplay in this level.
*
* @param stop If true, pause: if false, resume.
*/
void
pause
(
bool
stop
);
/**
* Implement author's debugging aids, which are activated only if the level
* is paused and the KConfig file contains group Debugging with setting
* DebuggingShortcuts=true. The main actions are to do timer steps one at
* a time, activate/deactivate a bug-fix or new-feature patch dynamically,
* activate/deactivate logging output from fprintf or kDebug() dynamically,
* print the status of a cell pointed to by the mouse and print the status
* of the hero or an enemy. See the code in file kgoldrunner.cpp, at the
* end of KGoldrunner::setupActions() for details of codes and keystrokes.
*
* To use the BUG_FIX or LOGGING options, first patch in and compile some
* code to achieve the effect required, with tests of static bool flags
* KGrGame::bugFix or KGrGame::logging surrounding that code. The relevant
* keystrokes then toggle those flags, so as to execute or skip the code
* dynamically as the game runs.
*
* @param code A code to indicate the action required (see enum
* DebugCodes in file kgrglobals.h).
*/
void
dbgControl
(
int
code
);
// Authors' debugging aids.
signals:
...
...
@@ -84,9 +256,6 @@ private slots:
void
doDig
(
int
button
);
// Dig using mouse-buttons.
private:
// TODO - Eliminate mView ...
KGrCanvas
*
mView
;
KGrLevelGrid
*
grid
;
KGrRuleBook
*
rules
;
KGrHero
*
hero
;
...
...
@@ -133,16 +302,12 @@ private:
int
reappearIndex
;
QVector
<
int
>
reappearPos
;
// TODO - Remove. int reappearPos[100];
void
makeReappearanceSequence
();
/******************************************************************************/
/************************** AUTHORS' DEBUGGING AIDS **************************/
/******************************************************************************/
bool
gameLogging
;
// If true, do logging printout (debug).
bool
bugFixed
;
// If true, enable bug fix code (debug).
void
bugFix
();
// Turn a bug fix on/off dynamically.
void
startLogging
();
// Turn logging on/off.
void
showFigurePositions
();
// Show everybody's co-ordinates.
...
...
src/kgrrunner.cpp
View file @
4078d305
...
...
@@ -33,6 +33,9 @@ KGrRunner::KGrRunner (KGrLevelPlayer * pLevelPlayer, KGrLevelGrid * pGrid,
spriteId
(
pSpriteId
),
gridI
(
i
),
gridJ
(
j
),
deltaX
(
0
),
deltaY
(
0
),
pointCtr
(
0
),
currDirection
(
STAND
),
currAnimation
(
FALL_L
),
...
...
@@ -41,12 +44,8 @@ KGrRunner::KGrRunner (KGrLevelPlayer * pLevelPlayer, KGrLevelGrid * pGrid,
{
getRules
();
gridX
=
i
*
pointsPerCell
;
gridY
=
j
*
pointsPerCell
;
pointCtr
=
0
;
vector
[
X
]
=
0
;
vector
[
Y
]
=
0
;
gridX
=
i
*
pointsPerCell
;
gridY
=
j
*
pointsPerCell
;
}
KGrRunner
::~
KGrRunner
()
...
...
@@ -68,8 +67,8 @@ bool KGrRunner::notTimeYet (const int scaledTime)
return
true
;
}
gridX
+=
vector
[
X
]
;
gridY
+=
vector
[
Y
]
;
gridX
+=
deltaX
;
gridY
+=
deltaY
;
pointCtr
++
;
// TODO - Count one extra tick when turning to L or R from another dirn.
...
...
@@ -130,43 +129,44 @@ HeroStatus KGrHero::run (const int scaledTime)
nuggets
=
levelPlayer
->
runnerGotGold
(
spriteId
,
gridI
,
gridJ
,
true
);
}
Direction
dir
n
=
levelPlayer
->
getDirection
(
gridI
,
gridJ
);
Direction
nextDirectio
n
=
levelPlayer
->
getDirection
(
gridI
,
gridJ
);
interval
=
runTime
;
Flags
OK
=
grid
->
heroMoves
(
gridI
,
gridJ
);
bool
onEnemy
=
levelPlayer
->
standOnEnemy
(
spriteId
,
gridX
,
gridY
);