JNR
laActiveObject.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 
31 
34 #include "stdafx.h"
35 #include "Core-Level-JR.h"
36 
38 {
39  getPivot()->bSimulateGravitation = M_FALSE;
40 }
41 
42 laActiveObject::~laActiveObject(void)
43 {
44 }
45 
46 // Load the object from file
47 //
48 void laActiveObject::load(class laFileParser *fp)
49 {
50  laStateObject::load(fp);
51 }
52 
53 // Dynamicly create a new object
54 //
55 //virtual void create(laPoint3 pos);
56 
57 // Respawn. Revert object to its original state
58 //
59 void laActiveObject::respawn()
60 {
61  laStateObject::respawn();
62 }
63 
64 void laActiveObject::_execute_ai(laTimer &t)
65 {
66  laPlayer *pPlayer = (laPlayer*)( getLevelObject()->getPlayer() );
67  laLine2 ln(pPlayer->getPosition(), _ptPivot);
68  double dPlayer = ln.lenght();
69 
70  // Perceive player actions only if on the same level along Y
71  //
72  if( M_ABS( pPlayer->getPosition().y() - _ptPivot.y() ) < M_UNIT*3 )
73  {
74  // Check if the player is in contact with the monster
75  //
76  /*if( pPlayer->boundingRect().intersecting(boundingRect()) )
77  {
78  _ai.perceive(P__PLAYER_TOUCHED);
79  }*/
80 
81  // Check if the player is commencing an action
82  //
83  if( (dPlayer<=M_PLAYER_ATTACK_RANGE * 1) && (pPlayer->isActionActive()) &&
84  (( (pPlayer->getFaceDirection()<0) && (pPlayer->getPosition().x()>_ptPivot.x()) )
85  || ( (pPlayer->getFaceDirection()>0) && (pPlayer->getPosition().x()<_ptPivot.x()) )) )
86  {
87  _ai.perceive(P__PLAYER_ACTION);
88  }
89 
90  // Check if the player is in our act range
91  //
92  if( (dPlayer < M_PLAYER_ATTACK_RANGE * 1) )
93  {
94  _ai.perceive(P__PLAYER_IN_RANGE);
95  }
96  }
97 
98  //Default
99  laStateObject::_execute_ai(t);
100 }
101 
102 unsigned laActiveObject::_next_state(unsigned nCurrentState )
103 {
104  return laStateObject::_next_state(nCurrentState);
105 }
Playable Character.
Definition: laPlayer.h:46
#define M_UNIT
Unit of 1 meter.
laActiveObject(void)
Active Object.
2D line segment
Definition: laLine2.h:40
#define M_ABS(a)
Return abs(a)
File Parser.
Definition: laFileParser.h:41