File: Compass.cpp

package info (click to toggle)
sear 0.5.0-5
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,408 kB
  • ctags: 2,580
  • sloc: cpp: 14,902; sh: 10,890; makefile: 172
file content (88 lines) | stat: -rw-r--r-- 1,982 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// This file may be redistributed and modified only under the terms of
// the GNU General Public License (See COPYING for details).
// Copyright (C) 2000-2003 Alistair Riddoch

#include "Compass.h"
#include "renderers/RenderSystem.h"
#include "src/Render.h"

#include <Eris/World.h>
#include <Eris/Entity.h>

#include <iostream>

namespace Sear
{

Compass::Compass(float pos_x, float pos_y) :  
    m_x(pos_x), 
    m_y(pos_y),
    m_angle(45)
{
}

Compass::~Compass()
{
}

void Compass::setup()
{    
    m_compassCase = Sprite("compass_case");
    m_compassNeedle = Sprite("compass_needle");
    m_needleShadow = Sprite("compass_needle_shadow");
}

void Compass::update(double cameraRotation)
{
    Eris::Entity* focus = Eris::World::Instance()->getFocusedEntity();
    if (!focus) return;
    
    WFMath::Quaternion q = focus->getOrientation();
    
    Eris::Entity* ent = focus->getContainer();
    while (ent && (ent != Eris::World::Instance()->getRootEntity())) {
        q *= ent->getOrientation();
        ent = ent->getContainer();
    }
    
    WFMath::Vector<3> northAxis(0.0, 1.0, 0.0),
        facing(1.0, 0.0, 0.0);
    facing.rotate(q); // orient in direction avatar is facing
    
    double radAngle = WFMath::Angle<3>(northAxis, facing);
    if (facing.x() < 0) radAngle = (M_PI * 2) - radAngle;
    
    radAngle += cameraRotation;
    m_angle = (radAngle * 180) / M_PI;
}

void Compass::draw(Render*r, bool select)
{
    if (select) return;
        
    r->setViewMode(ORTHOGRAPHIC);
    
    r->translateObject(m_x, m_y, 0.0f);
    m_compassCase.draw(r);
    
// draw the shadow, offset a little bit    
    r->store();
    
    r->translateObject(-3.0f, -3.0f, 0.01f);
    r->rotate(m_angle, 0.0f, 0.0f, 1.0f);
    m_needleShadow.draw(r);
    
    r->restore();
    
// finally draw the needle
    r->rotate(m_angle, 0.0f, 0.0f, 1.0f);
    m_compassNeedle.draw(r);
        
    r->setViewMode(PERSPECTIVE);
}

void Compass::click()
{
}

} // namespace Sear