1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.replica.replicaisland;
18
19/**
20 * A system for testing positions against "hot spots" embedded in the level tile map data.
21 * A level may contain a layer of "hot spots," tiles that provide a hint to the game objects about
22 * how to act in that particular area of the game world.  Hot spots are commonly used to direct AI
23 * characters, or to define areas where special collision rules apply (e.g. regions that cause
24 * instant death when entered).
25 */
26public class HotSpotSystem extends BaseObject {
27    TiledWorld mWorld;
28
29    public class HotSpotType {
30        public static final int NONE = -1;
31        public static final int GO_RIGHT = 0;
32        public static final int GO_LEFT = 1;
33        public static final int GO_UP = 2;
34        public static final int GO_DOWN = 3;
35
36        public static final int WAIT_SHORT = 4;
37        public static final int WAIT_MEDIUM = 5;
38        public static final int WAIT_LONG = 6;
39
40        public static final int ATTACK = 7;
41        public static final int TALK = 8;
42        public static final int DIE = 9;
43        public static final int WALK_AND_TALK = 10;
44        public static final int TAKE_CAMERA_FOCUS = 11;
45        public static final int RELEASE_CAMERA_FOCUS = 12;
46        public static final int END_LEVEL = 13;
47        public static final int GAME_EVENT = 14;
48        public static final int NPC_RUN_QUEUED_COMMANDS = 15;
49
50        public static final int NPC_GO_RIGHT = 16;
51        public static final int NPC_GO_LEFT = 17;
52        public static final int NPC_GO_UP = 18;
53        public static final int NPC_GO_DOWN = 19;
54        public static final int NPC_GO_UP_RIGHT = 20;
55        public static final int NPC_GO_UP_LEFT = 21;
56        public static final int NPC_GO_DOWN_LEFT = 22;
57        public static final int NPC_GO_DOWN_RIGHT = 23;
58        public static final int NPC_GO_TOWARDS_PLAYER = 24;
59        public static final int NPC_GO_RANDOM = 25;
60        public static final int NPC_GO_UP_FROM_GROUND = 26;
61        public static final int NPC_GO_DOWN_FROM_CEILING = 27;
62        public static final int NPC_STOP = 28;
63        public static final int NPC_SLOW = 29;
64
65
66        public static final int NPC_SELECT_DIALOG_1_1 = 32;
67        public static final int NPC_SELECT_DIALOG_1_2 = 33;
68        public static final int NPC_SELECT_DIALOG_1_3 = 34;
69        public static final int NPC_SELECT_DIALOG_1_4 = 35;
70        public static final int NPC_SELECT_DIALOG_1_5 = 36;
71
72        public static final int NPC_SELECT_DIALOG_2_1 = 38;
73        public static final int NPC_SELECT_DIALOG_2_2 = 39;
74        public static final int NPC_SELECT_DIALOG_2_3 = 40;
75        public static final int NPC_SELECT_DIALOG_2_4 = 41;
76        public static final int NPC_SELECT_DIALOG_2_5 = 42;
77
78
79
80    }
81
82    public HotSpotSystem() {
83        super();
84    }
85
86    @Override
87    public void reset() {
88        mWorld = null;
89    }
90
91    public final void setWorld(TiledWorld world) {
92        mWorld = world;
93    }
94
95    public int getHotSpot(float worldX, float worldY) {
96        //TOOD: take a region?  how do we deal with multiple hot spot intersections?
97        int result = HotSpotType.NONE;
98        if (mWorld != null) {
99
100            final int xTile = getHitTileX(worldX);
101            final int yTile = getHitTileY(worldY);
102
103            result = mWorld.getTile(xTile, yTile);
104        }
105
106        return result;
107    }
108
109    public int getHotSpotByTile(int tileX, int tileY) {
110        //TOOD: take a region?  how do we deal with multiple hot spot intersections?
111        int result = HotSpotType.NONE;
112        if (mWorld != null) {
113            result = mWorld.getTile(tileX, tileY);
114        }
115
116        return result;
117    }
118
119    public final int getHitTileX(float worldX) {
120        int xTile = 0;
121        LevelSystem level = sSystemRegistry.levelSystem;
122        if (mWorld != null && level != null) {
123            final float worldPixelWidth = level.getLevelWidth();
124            xTile = (int)Math.floor(((worldX) / worldPixelWidth) * mWorld.getWidth());
125        }
126        return xTile;
127    }
128
129    public final int getHitTileY(float worldY) {
130        int yTile = 0;
131        LevelSystem level = sSystemRegistry.levelSystem;
132        if (mWorld != null && level != null) {
133            final float worldPixelHeight = level.getLevelHeight();
134            // TODO: it is stupid to keep doing this space conversion all over the code.  Fix this
135            // in the TiledWorld code!
136            final float flippedY = worldPixelHeight - (worldY);
137            yTile = (int)Math.floor((flippedY / worldPixelHeight) * mWorld.getHeight());
138        }
139        return yTile;
140    }
141
142    public final float getTileCenterWorldPositionX(int tileX) {
143        float worldX = 0.0f;
144    	LevelSystem level = sSystemRegistry.levelSystem;
145        if (mWorld != null && level != null) {
146            final float tileWidth = level.getLevelWidth() / mWorld.getWidth();
147            worldX = (tileX * tileWidth) + (tileWidth / 2.0f);
148        }
149        return worldX;
150    }
151
152}
153