JNR
aiNPCState_Approach.cpp
1 /*
2 
3 Jump'n'Run Engine
4 http://www.atanaslaskov.com/jnr/
5 
6 BSD LICENSE
7 Copyright (c) 2007-2013, Atanas Laskov
8 All rights reserved.
9 
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions are met:
12 1. Redistributions of source code must retain the above copyright notice,
13 this list of conditions and the following disclaimer.
14 2. Redistributions in binary form must reproduce the above copyright notice,
15 this list of conditions and the following disclaimer in the documentation
16 and/or other materials provided with the distribution.
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL ATANAS LASKOV BE LIABLE FOR ANY
21 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 */
29 
30 #include "stdafx.h"
31 #include "ai.h"
32 
33 aiNPCState_Approach::~aiNPCState_Approach(void)
34 {
35 }
36 
37 void aiNPCState_Approach::load(laFileParser *fp)
38 {
39  //bPlayerDetected = M_FALSE;
40 
41  char strDialog[128];
42  fp->readText(strDialog);
43 
44  laFileParser fp_d(strDialog);
45  dialog.load(&fp_d);
46 
47  pSound_SeePlayer = ::laSystemIntegrator::getSound()->soundCreate("collect(sonar).mp3");
48 }
49 
50 
51 void aiNPCState_Approach::perceive_active( std::string strID_Old )
52 {
53  laMonster* pMonster = (laMonster*) self();
54  laPlayer* pPlayer = (laPlayer*) player();
55 
56  pMonster->move(0);
57  pMonster->agressive(M_FALSE);
58  pMonster->enableBattleGUI(M_FALSE);
59  pMonster->enableNPCGUI(M_FALSE);
60 }
61 
62 void aiNPCState_Approach::perceive_inactive( std::string strID_New )
63 {
64 }
65 
66 void aiNPCState_Approach::perceive_InTerritory(unsigned state)
67 {
68  laMonster* pMonster = (laMonster*) self();
69  laPlayer* pPlayer = (laPlayer*) player();
70 
71  if( (state!=PERCEPT_RESET) && (!dialog.isFinished()) )
72  {
73  pPlayer->ctlBattleMode();
74 
75  if(state==PERCEPT_SET)
76  {
77  pSound_SeePlayer->play();
78  pMonster->setOutline( laOutline(M_TRUE, laColor(255,255,255), 1.5) );
79  pMonster->enableNPCGUI();
80  pPlayer->enableControl(M_FALSE);
81 
82  stageJRGame* ps = (stageJRGame*)stage();
83  ps->enableCinematicMode();
84  }
85 
86  // Move towards the player until colse enough
87  if( ! agent()->isPerceiving(P__PLAYER_IN_RANGE) )
88  {
89  pMonster->enableNPCGUI();
90 
91  if( pPlayer->getPosition().x() < pMonster->getPosition().x()) pMonster->move(-1);
92  else pMonster->move(+1);
93  }
94  else
95  {
96  pMonster->enableNPCGUI(M_FALSE);
97  pMonster->move(0);
98  }
99  }
100  else
101  {
102  //Dialog passed
103  pMonster->setOutline( laOutline(M_TRUE, laColor(0,0,0), 1.5) );
104  pMonster->enableNPCGUI(M_FALSE);
105  pMonster->move(0);
106  }
107 }
108 
109 // The player is in "attack" (talk) range
110 //
111 void aiNPCState_Approach::perceive_InRange(unsigned state)
112 {
113  laMonster* pMonster = (laMonster*) self();
114  laPlayer* pPlayer = (laPlayer*) player();
115 
116  // Show the dialog
117  if( (state==PERCEPT_SET) && (!dialog.isFinished()) )
118  {
119  pPlayer->setFaceDirection( pPlayer->getPosition().x() > pMonster->getPosition().x() ? -1 : +1 );
120 
121  dialog.pPlayer = pPlayer;
122  dialog.pMonster = pMonster;
123  pPlayer->enableDialog( &dialog );
124 
125  }
126 }
127 
128 void aiNPCState_Approach::perceive_Touched(unsigned state)
129 {
130  laMonster* pMonster = (laMonster*) self();
131  laPlayer* pPlayer = (laPlayer*) player();
132 
133  pPlayer->reflect( pMonster->boundingRect() );
134 }
135 
136 void aiNPCState_Approach::perceive_UnderAttack(unsigned state)
137 {
138 }
139 
140 
141 void aiNPCState_Approach::perceive(unsigned state, M_BOOL global, unsigned id, aiPerceptData data)
142 {
143  aiMobState::perceive(state, global, id, data);
144 
145  laMonster* pMonster = (laMonster*) self();
146  laPlayer* pPlayer = (laPlayer*) player();
147 
148  //Conversation completed, remove any effects
149  if( dialog.isFinished() )
150  {
151  pPlayer->enableDialog( NULL );
152  pPlayer->enableControl();
153 
154  stageJRGame* ps = (stageJRGame*)stage();
155  ps->enableCinematicMode(M_FALSE);
156  }
157 
158  pMonster->setFaceDirection( pPlayer->getPosition().x() > pMonster->getPosition().x() ? +1 : -1 );
159 }
Playable Character.
Definition: laPlayer.h:46
Object outline properties.
Definition: laRenderer.h:66
JR Gameplay Stage.
Definition: stageJRGame.h:41
Adds capabilities and percepts specific to monster creatures.
Definition: laMonster.h:45
File Parser.
Definition: laFileParser.h:41