xxxxxxxxxx
202
let spritesheet;
let timer = 1000;
//let nextRun = timer;
let horses = [];
let rval=0;
let step = 0;
let speed = 15;
let counter = 0;
let bg = [];
let right = false;
let left = false;
let serial;
let run = false;
let xPos;
let zPos;
let prevVal=0;
let direction =1;
function preload() {
spritesheet = loadImage("horse_final_run.png");
soundBG = loadSound("tugan_zher.mp3");
for (let i = 1; i<21; i++){
bg.push(loadImage(str(i) + "bg.jpg"))
}
}
function setup() {
createCanvas(800, 580);
// soundBG.loop();
soundBG.setVolume(0.5);
let w = spritesheet.width;
let h = spritesheet.height / 7;
for (let y = 0; y < 7; y++) {
for (let x = 0; x < 1; x++) {
horses.push(spritesheet.get(x*w/7 , y*h, w, h));
}
}
setUpSerial();
}
function keyPressed() {
if (key == "a") {
setUpSerial(SELECT_PORT);
}
}
function draw() {
background(255);
image(bg[int(map(rval - 1,0, 1023,0,20))],0,0);
if (frameCount % speed == 0){
step = (step + 1)%7;
}
// using xPos
// if (!run) {
// if (xPos > 120) {
// run = true;
// direction = 1;
// image(horses[step],counter, 0);
// }
// }
// if (xPos<120) {
// if (direction ==-1){
// counter = (counter + 10) % 600;
// image(horses[step],counter, 0);
// if (counter >589) {
// run = false;
// }
// }
// translate(0, height/2);
// scale(direction,1);
// using zPos
// if (zPos < 30) { // hand detected
// run = true;
// }
// if (run) {
// counter = (counter + 10) % 600;
// image(horses[step],counter, 420);
// if (counter > 589) {
// run = false;
// }
// }
//
if (right) {
counter += 10;
image(horses[step],counter, 420);
// prevVal=xPos;
if (counter == 800)
right = false;
}
else if (left){
counter -=10;
scale(-1,1);
image(horses[step],-counter, 420);
if (counter == 0)
left = false;
}
else {
if (xPos > 1 && xPos < 119 && xPos != prevVal){
right = true;
counter = 0;
}
else if (xPos > 181 && xPos < 239 && xPos!=prevVal){
left =true;
counter = 800
}
prevVal = xPos;
}
// if (run) {
// counter =( (counter + 10) % 800);
// if (direction == 1){
// xPos = prevVal;
// image(horses[step],counter, 420);
// // if (xPos + 10 > prevVal){
// // }
// }
// else if (direction == -1) {
// scale(direction, 1);
// image(horses[step],counter, 420);
// }
// if (counter > 789) {
// run = false;
// }
// }
// if (xPos > 130 && xPos < 200 && !run) { // hand detected
// run = true;
// direction = 1;
// counter *=-1;
// }
// else if (xPos > 50 && xPos < 120 && !run){
// run=true;
// direction = -1;
// counter *= -1;
// }
// if (millis()>timer){
// }
// if (xPos + 10 > prevVal)
}
function readSerial(data) {
////////////////////////////////////
//READ FROM ARDUINO HERE
////////////////////////////////////
if (data != null) {
// make sure there is actually a message
// split the message
let fromArduino = split(trim(data), ",");
// if the right length, then proceed
if (fromArduino.length == 3) {
xPos = fromArduino[0]
zPos = fromArduino[1];
rval = fromArduino[2];
// print(xPos + "," + zPos + "," + rval );
}
//////////////////////////////////
//SEND TO ARDUINO HERE (handshake)
//////////////////////////////////
let sendToArduino = "\n";
writeSerial(sendToArduino);
}
}