| 1 | /* (c) Shereef Marzouk. See "licence DDRace.txt" and the readme.txt in the root of the distribution for more information. */ |
| 2 | #include "door.h" |
| 3 | |
| 4 | #include "character.h" |
| 5 | |
| 6 | #include <game/client/laser_data.h> |
| 7 | #include <game/collision.h> |
| 8 | #include <game/mapitems.h> |
| 9 | |
| 10 | CDoor::CDoor(CGameWorld *pGameWorld, int Id, const CLaserData *pData) : |
| 11 | CEntity(pGameWorld, CGameWorld::ENTTYPE_DOOR) |
| 12 | { |
| 13 | m_Id = Id; |
| 14 | m_Active = false; |
| 15 | |
| 16 | m_Number = pData->m_SwitchNumber; |
| 17 | m_Layer = m_Number > 0 ? LAYER_SWITCH : LAYER_GAME; |
| 18 | |
| 19 | Read(pData); |
| 20 | } |
| 21 | |
| 22 | void CDoor::ResetCollision() |
| 23 | { |
| 24 | if(Collision()->GetTile(x: m_Pos.x, y: m_Pos.y) || Collision()->GetFrontTile(x: m_Pos.x, y: m_Pos.y)) |
| 25 | return; |
| 26 | |
| 27 | m_Active = true; |
| 28 | |
| 29 | vec2 Dir = m_To - m_Pos; |
| 30 | m_Length = length(a: Dir); |
| 31 | m_Direction = normalize_pre_length(v: Dir, len: static_cast<float>(m_Length)); |
| 32 | |
| 33 | for(int i = 0; i < m_Length - 1; i++) |
| 34 | { |
| 35 | vec2 CurrentPos = m_Pos + m_Direction * i; |
| 36 | |
| 37 | CDoorTile DoorTile; |
| 38 | Collision()->GetDoorTile(Index: Collision()->GetPureMapIndex(Pos: CurrentPos), pDoorTile: &DoorTile); |
| 39 | // switch door always has priority, because it can turn off doors on all layers if they intersect |
| 40 | if(DoorTile.m_Index && DoorTile.m_Number > 0) |
| 41 | continue; |
| 42 | |
| 43 | if(Collision()->CheckPoint(Pos: CurrentPos)) |
| 44 | break; |
| 45 | else |
| 46 | Collision()->SetDoorCollisionAt(x: CurrentPos.x, y: CurrentPos.y, Type: TILE_STOPA, Flags: 0, Number: m_Number); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | void CDoor::Destroy() |
| 51 | { |
| 52 | if(m_Active) |
| 53 | { |
| 54 | for(int i = 0; i < m_Length - 1; i++) |
| 55 | { |
| 56 | vec2 CurrentPos = m_Pos + m_Direction * i; |
| 57 | if(Collision()->CheckPoint(Pos: CurrentPos)) |
| 58 | break; |
| 59 | else |
| 60 | Collision()->SetDoorCollisionAt(x: CurrentPos.x, y: CurrentPos.y, Type: TILE_AIR, Flags: 0, Number: 0); |
| 61 | } |
| 62 | } |
| 63 | delete this; |
| 64 | } |
| 65 | |
| 66 | void CDoor::Read(const CLaserData *pData) |
| 67 | { |
| 68 | // it's flipped in the laser object |
| 69 | m_Pos = pData->m_To; |
| 70 | m_To = pData->m_From; |
| 71 | } |
| 72 | |
| 73 | bool CDoor::Match(const CDoor *pDoor) const |
| 74 | { |
| 75 | return pDoor->m_Pos == m_Pos && pDoor->m_To == m_To && pDoor->m_Number == m_Number; |
| 76 | } |
| 77 | |