1 //  Key.cpp -- A key the player has to collect.
2 //  Copyright (C) 2008  Nick Gasson
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 //
17 
18 #include "Key.hpp"
19 #include "OpenGL.hpp"
20 #include "Viewport.hpp"
21 #include "ObjectGrid.hpp"
22 #include "Ship.hpp"
23 
24 #include <string>
25 #include <cassert>
26 
Key(bool active,int xpos,int ypos,ArrowColour acol)27 Key::Key(bool active, int xpos, int ypos, ArrowColour acol)
28    : StaticObject(xpos, ypos, 1, 1),
29      active(active),
30      image(KeyFileName(acol), 32, 32, KEY_FRAMES),
31      arrow(ArrowFileName(acol))
32 {
33    alpha = active ? 1.0 : 0.0;
34 
35    rotcount = KEY_ROTATION_SPEED;
36 }
37 
DrawKey(Viewport * viewport)38 void Key::DrawKey(Viewport* viewport)
39 {
40    int draw_x = xpos*OBJ_GRID_SIZE - viewport->GetXAdjust();
41    int draw_y = ypos*OBJ_GRID_SIZE - viewport->GetYAdjust() + OBJ_GRID_TOP;
42    image.Draw(draw_x, draw_y, 0.0, 1.0, alpha);
43    if (--rotcount == 0) {
44       image.NextFrame();
45       rotcount = KEY_ROTATION_SPEED;
46    }
47 
48    if (!active && alpha > 0.0f)
49       alpha -= 0.02f;
50 }
51 
DrawArrow(Viewport * viewport) const52 void Key::DrawArrow(Viewport* viewport) const
53 {
54    if (active && !ObjectInScreen(viewport))	{
55       int ax = xpos*OBJ_GRID_SIZE - viewport->GetXAdjust();
56       int ay = ypos*OBJ_GRID_SIZE + OBJ_GRID_TOP - viewport->GetYAdjust();
57       float angle = 0.0;
58 
59       int screenWidth = OpenGL::GetInstance().GetWidth();
60       int screenHeight = OpenGL::GetInstance().GetHeight();
61 
62       if (ax < 0) {
63          ax = 0;
64          angle = 90;
65       }
66       else if (ax + ARROW_SIZE > screenWidth) {
67          ax = screenWidth - ARROW_SIZE;
68          angle = 270;
69       }
70       if (ay < 0) {
71          ay = 0;
72          angle = 180;
73       }
74       else if (ay + ARROW_SIZE > screenHeight) {
75          ay = screenHeight - ARROW_SIZE;
76          angle = 0;
77       }
78 
79       arrow.Draw(ax, ay, angle);
80    }
81 }
82 
DrawIcon(int offset,float minAlpha) const83 void Key::DrawIcon(int offset, float minAlpha) const
84 {
85    double drawAlpha = alpha > minAlpha ? alpha : minAlpha;
86    image.DrawFrame(5, offset, 10, 0.0, 1.0, drawAlpha);
87 }
88 
CheckCollision(Ship & ship) const89 bool Key::CheckCollision(Ship& ship) const
90 {
91    bool collide = ship.BoxCollision
92       (xpos*OBJ_GRID_SIZE + 3,
93        ypos*OBJ_GRID_SIZE + OBJ_GRID_TOP + 3,
94        OBJ_GRID_SIZE - 6,
95        OBJ_GRID_SIZE - 6);
96    return active && collide;
97 }
98 
KeyFileName(ArrowColour col)99 string Key::KeyFileName(ArrowColour col)
100 {
101    switch (col) {
102    case acBlue:
103       return "images/keyblue.png";
104    case acRed:
105       return "images/keyred.png";
106    case acYellow:
107       return "images/keyyellow.png";
108    case acPink:
109       return "images/keypink.png";
110    case acGreen:
111       return "images/keygreen.png";
112    default:
113       assert(false);
114    }
115 }
116 
ArrowFileName(ArrowColour col)117 string Key::ArrowFileName(ArrowColour col)
118 {
119    switch (col) {
120    case acBlue:
121       return "images/arrowblue.png";
122    case acRed:
123       return "images/arrowred.png";
124    case acYellow:
125       return "images/arrowyellow.png";
126    case acPink:
127       return "images/arrowpink.png";
128    case acGreen:
129       return "images/arrowgreen.png";
130    default:
131       assert(false);
132    }
133 }
134