-
Notifications
You must be signed in to change notification settings - Fork 0
/
room.cpp
151 lines (129 loc) · 4.54 KB
/
room.cpp
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <avr/pgmspace.h>
//#include <TVout.h>
#include <string.h>
#include "room.h"
#include "room_data.h"
#include "monster.h"
#include "item.h"
#include "elf.h"
//Arduino IDE strikes again
typedef struct ElfStruct
{
unsigned char facing;
unsigned char step;
unsigned char x;
unsigned char y;
unsigned char hearts;
unsigned char items[4];
unsigned char state;
} Elf;
void resetElf(bool reset_items);
void showElf();
void moveElf(unsigned char facing);
void throwSword();
RoomElement hitElf(RoomElement element);
Elf getElf();
void addElfItem(char type);
bool elfHasItem(char type);
char getElfState();
RoomElement roomElements[MAX_ELEMENT_RECORDS];
char element_count = 0;
void addRoomElement(char type, char x, char y, char state, char counter)
{
roomElements[element_count].id = element_count;
roomElements[element_count].type = type;
roomElements[element_count].x = x;
roomElements[element_count].y = y;
roomElements[element_count].step = 1;
roomElements[element_count].state = state;
roomElements[element_count].counter = counter;
element_count++;
}
//load room elements (if present)
void loadRoomElemments(char room)
{
//clear the array of the previous contents
memset(roomElements, 0, sizeof(roomElements));
element_count = 0;
//add in the item (sword) room element automatically
addRoomElement(ITEM_SWORD, 0, 0, STATE_HIDDEN, 0);
int index_ptr = 0;
//determine the index start for room elements
index_ptr = pgm_read_byte_near(room_element_index_data + room);
//read until we have reached the max number of element records
if (index_ptr < 255)
{
//set to the actual data position
index_ptr *= 4;
while (element_count < MAX_ELEMENT_RECORDS)
{
//only add the item to the room if the elf does not already have this in inventory
if (elfHasItem(pgm_read_byte_near(room_element_data + index_ptr)) == false) addRoomElement(pgm_read_byte_near(room_element_data + index_ptr), pgm_read_byte_near(room_element_data + (index_ptr+1)), pgm_read_byte_near(room_element_data + (index_ptr+2)), STATE_VISIBLE, 0);
//look to see if we have reached the end of data for the room
if (pgm_read_byte_near(room_element_data + (index_ptr+3)) == 255) break;
//otherwise, we continue advancing through the data
index_ptr += 4;
}
}
}
//handles the room elements
void handleRoomElements()
{
//retrieve elf information
Elf elf = getElf();
for (char i=0; i < element_count; i++)
{
//if the element is hidden, skip past it
if (roomElements[i].state > STATE_HIDDEN)
{
//test room elements for a collision with the elf
if (testRoomElement(roomElements[i], elf.x, elf.y, 16)) roomElements[i] = hitElf(roomElements[i]);
//determine the type of element and handle the behaviors
if (roomElements[i].type < 50)
{
//test for monster being hit by the weapon (if it is active)
//room element 0 is always the weapon
if (roomElements[0].state > STATE_HIDDEN) {
if (testRoomElement(roomElements[i], roomElements[0].x, roomElements[0].y, 8))
{
roomElements[i] = hitMonster(roomElements[i]);
roomElements[0] = hitItem(roomElements[0]);
}
}
//handle monster movement
roomElements[i] = moveMonster(roomElements[i]);
} else {
roomElements[i] = moveItem(roomElements[i]);
//hide the heart if the timer has run out
if ((roomElements[i].type == ITEM_HEART) && (roomElements[i].counter == 0)) roomElements[i] = hitItem(roomElements[i]);
}
}
}
}
//test for collision in room elements
bool testRoomElement (RoomElement element, char testX, char testY, char ySize)
{
bool is_hit = false;
if (((element.x >= testX) && (element.x <= testX + 8)) || ((element.x + 8 >= testX) && (element.x + 8 <= testX + 8)))
{
//determine the type of element and handle the behaviors
if (element.type < 50)
{
if (((element.y >= testY) && (element.y <= testY + ySize )) || ((element.y + ySize >= testY) && (element.y + ySize <= testY + ySize ))) is_hit = true;
} else {
//Why was this testing for items 16 tall when all >50 items seem to be only 8 tall?
if (((element.y >= testY) && (element.y <= testY + 8 )) || ((element.y + 8 >= testY) && (element.y + 8 <= testY + 8 ))) is_hit = true; //Changed element.y + 16 to element.y + 8
}
}
return is_hit;
}
//handles updating the room element
void updateRoomElement (RoomElement element)
{
roomElements[element.id] = element;
}
//handles retrieving the room element
RoomElement getRoomElement (char id)
{
return roomElements[id];
}