-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.cpp
150 lines (110 loc) · 3.57 KB
/
Player.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
/*
Author: Esmit Perez (@esmitperez)
Fecha: Jul 10 2013
Inspirado en http://playground.arduino.cc/Code/StopWatchClass y http://arduino.cc/en/Hacking/LibraryTutorial
*/
#include <LiquidCrystal.h>
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include "Animation.h"
#include "Player.h"
#include "constants.h"
Player::Player(int buzzer, int numRows, int numCols, int howManyActors, Vector<Actor> actrs) {
buzzerPin = buzzer;
lcdRows = numRows;
lcdCols = numCols;
numActors = howManyActors;
/*
int l = sizeof(Actor*) * numActors;
actors = (Actor*) malloc(l);
*/
actors = actrs;
}
void Player::showFrameFor(Actor *actor) {
/*
if (DEBUG_ON) {
Serial.print("METHOD: showFrameFor");
Serial.print("Actor: ");
Serial.print(actor.name);
Serial.println();
}<
*/
lcd->setCursor(actor->pos_curr_x, actor->pos_curr_y);
lcd->write(byte(actor->actorCharNumber));
}
void Player::clearBubble(Actor *actor) {
lcd->setCursor(actor->bubble_x,actor->bubble_y);
for (int i=0; i<actor->bubble_size; i++) {
lcd->print(" ");
}
lcd->setCursor(actor->bubble_x,actor->bubble_y);
}
void Player::gotoPos(Actor *actor,int x,int y) {
lcd->setCursor(actor->pos_curr_x, actor->pos_curr_y);
lcd->print(" ");
actor->pos_prev_x = actor->pos_curr_x;
actor->pos_prev_y = actor->pos_curr_y;
actor->pos_curr_x = x%lcdCols;
actor->pos_curr_y = y%lcdRows;
lcd->setCursor(actor->pos_curr_x, actor->pos_curr_y);
}
void Player::info() {
#if DEBUG_ON == true
Serial.print("Num actors");
Serial.println(actors.size());
#endif
for (int i=0; i<numActors; i++) {
Actor actor = actors[i];
Animation *clip = actor.currentAnimation;
#if DEBUG_ON == true
actor.info();
#endif
}
}
void Player::render() {
for (int i=0; i<numActors; i++) {
Actor *actor = &actors[i];
Animation *clip = actor->currentAnimation;
#if DEBUG_ON == true
actor->info();
Serial.print(" Movie: ");
Serial.print(clip->name);
Serial.print(" Num Frames:");
Serial.print(clip->numFrames);
Serial.println();
#endif
for (int j = 0; j < clip->numFrames; j++) {
int byteOffset= (j * FRAME_SIZE);
/* inefficient since it allocates memory for data we already &have loadaed
byte* frame = (byte*) malloc(sizeof(byte) * FRAME_SIZE);
int byteOffset= (i * FRAME_SIZE);
for (int i=0; i<FRAME_SIZE;i++){
frame[i] = clip.frames[byteOffset+i];
}
*/
//&clip->frames[byteOffset]
lcd->createChar(actor->actorCharNumber, &clip->frames[byteOffset]);
showFrameFor(actor);
#if DEBUG_ON == true
// Serial.print("Frame: ");
// Serial.println(i);
#endif
if (clip->makeSound) {
tone(buzzerPin, clip->melody[i%clip->melodySize], clip->melodySpeed);
delay(clip->melodyDelay);
noTone(buzzerPin);
}
delay(clip->animationPause);
if (clip->direction == 0 || i==0 ) continue;
int posMultiplier = clip->direction * 1;
int x = actor->pos_curr_x+posMultiplier;
int y = actor->pos_curr_y;
actor->info();
gotoPos(actor,x, y);
actor->info();
}
}
}