Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <FastLED.h>;
- //////////////////////////////////////// DEFINES
- #define BUTTONPINP1LEFTY 27
- #define BUTTONPINP1RIGHTY 26
- #define BUTTONPINP2LEFTY 9
- #define BUTTONPINP2RIGHTY 8
- #define BUTTONPINP3LEFTY 11
- #define BUTTONPINP3RIGHTY 10
- #define BUTTONPINP4LEFTY 25
- #define BUTTONPINP4RIGHTY 24
- #define LED1 5
- #define LED2 4
- #define LED3 3
- #define LED4 2
- #define LEFTY 0
- #define RIGHTY 1
- #define LASTLEFTY 2
- #define LASTRIGHTY 3
- #define VELOCITY 0
- #define FLOATPOS 1
- #define SPEED 2
- #define ACCELERATION 3
- #define LANE 0
- #define POS 1
- #define OLDPOS 2
- ///////////////////////////////////////// redOrbInfo
- int redOrbAmount = 8;
- float redOrbMov[8][2]; // 0 VELOCITY 1 FLOATPOS
- int redOrbPos[8][2]; // 0 LANE 1 POS
- int direction = 1;
- const int orbminspeed= 15;
- const int orbmaxspeed= 30;
- const int NUM_LEDS = 50;
- //////////////////////////////////////// playerInfo
- const int playerButtonPins[4][2] = {
- {BUTTONPINP1LEFTY, BUTTONPINP1RIGHTY},
- {BUTTONPINP2LEFTY, BUTTONPINP2RIGHTY},
- {BUTTONPINP3LEFTY, BUTTONPINP3RIGHTY},
- {BUTTONPINP4LEFTY, BUTTONPINP4RIGHTY}};
- int playerAmount = 4;
- boolean playerButtonState[4][4]; // 0 LEFTY 1 RIGHTY 2 LASTLEFTY 3 LASTRIGHTY
- float playerMov[4][4]; // 0 VELOCITY 1 FLOATPOS 2 SPEED 3 ACCELERATION
- int playerPos[4][3]; // 0 LANE 1 POS 2 OLDPOS
- float friction = 0.99; // Not needed when fixed speed..
- float knockback = -1;
- float defaultSpeed = 0.25;
- float defaultAcceleration = 0.025; //can be used for variable speed
- float boost = 0.05;
- ///////////////////////////////////////// updateInfo
- unsigned long previousMillis = 0;
- const int interval = 5;
- ///////////////////////////////////////// ledInfo
- CRGB leds1[NUM_LEDS];
- CRGB leds2[NUM_LEDS];
- CRGB leds3[NUM_LEDS];
- CRGB leds4[NUM_LEDS];
- /////////////////////////////////////////////////////////////////// SETUP
- void setup()
- {
- delay(3000);
- //Serial.begin(9600);
- setupLEDS();
- setupInput();
- createPlayers();
- createOrbs();
- }
- /////////////////////////////////////////////////////////////////// LOOP
- void loop()
- {
- FastLED.clear();
- //readInput(); //Can also be added to gameLoop
- updateGame();
- FastLED.show();
- }
- /////////////////////////////////////////////////////////////////// gameLoop
- void updateGame()
- {
- unsigned long currentMillis = micros();
- if (currentMillis - previousMillis >= interval)
- {
- gameLoop();
- previousMillis = currentMillis;
- }
- }
- void gameLoop() //MOVE DETECT RE-MOVE?
- {
- for (int redOrbsX = 0; redOrbsX < redOrbAmount; redOrbsX++)
- {
- moveOrb(redOrbsX);
- for (int redOrbsY = 0; redOrbsY < redOrbAmount; redOrbsY++)
- {
- hitDetectionOrb(redOrbsX, redOrbsY);
- }
- updateOrb(redOrbsX);
- }
- for (int playerX = 0; playerX < playerAmount; playerX++)
- {
- movePlayer(playerX);
- for (int playerY = 0; playerY < playerAmount; playerY++)
- {
- hitDetectionPlayer(playerX, playerY);
- }
- for (int redOrbs = 0; redOrbs < redOrbAmount; redOrbs++)
- {
- hitDetectionPlayerOrb(playerX, redOrbs);
- }
- updatePlayer(playerX);
- }
- }
- /////////////////////////////////////////////////////////////////// INPUT
- void setupInput()
- {
- for (int i = 0; i < playerAmount; i++)
- {
- for (int j = 0; j < 2; j++)
- {
- pinMode(playerButtonPins[i][j], INPUT);
- }
- }
- }
- void readInput()
- {
- laneChangeInput();
- }
- void laneChangeInput()
- {
- for (int i = 0; i < playerAmount; i++) //0 LEFTY 1 RIGHTY 2 LASTLEFTY 3 LASTRIGHTY
- {
- playerButtonState[i][LEFTY] = digitalRead(playerButtonPins[i][LEFTY]);
- playerButtonState[i][RIGHTY] = digitalRead(playerButtonPins[i][RIGHTY]);
- if (playerButtonState[i][LEFTY] != playerButtonState[i][LASTLEFTY])
- {
- if (playerButtonState[i][LEFTY] == HIGH)
- {
- changePlayerLane(i, LEFTY);
- }
- }
- playerButtonState[i][LASTLEFTY] = playerButtonState[i][LEFTY];
- if (playerButtonState[i][RIGHTY] != playerButtonState[i][LASTRIGHTY])
- {
- if (playerButtonState[i][RIGHTY] == HIGH && playerPos[i][LANE] != 3)
- {
- changePlayerLane(i, RIGHTY);
- }
- }
- playerButtonState[i][LASTRIGHTY] = playerButtonState[i][RIGHTY];
- }
- }
- /////////////////////////////////////////////////////////////////// MOVEMENT
- void moveOrb(int orbIndex)
- {
- redOrbMov[orbIndex][FLOATPOS] += redOrbMov[orbIndex][VELOCITY];
- if (redOrbMov[orbIndex][FLOATPOS] > NUM_LEDS)
- {
- redOrbMov[orbIndex][FLOATPOS] = 0.0;
- };
- redOrbPos[orbIndex][POS] = (int)(redOrbMov[orbIndex][FLOATPOS] + 0.5);
- }
- void movePlayer(int index) // not perfect, debug and rework
- {
- playerPos[index][OLDPOS] = playerPos[index][POS];
- if (playerMov[index][VELOCITY] < playerMov[index][SPEED])
- {
- playerMov[index][VELOCITY] += playerMov[index][ACCELERATION]; //Can use fixed acceleration to save memory
- }
- playerMov[index][VELOCITY] = playerMov[index][VELOCITY] * friction;
- playerMov[index][FLOATPOS] += playerMov[index][VELOCITY];
- if (playerMov[index][FLOATPOS] > NUM_LEDS)
- {
- playerMov[index][FLOATPOS] -= NUM_LEDS;
- //playerMov[index][FLOATPOS] = 0.0;
- }
- else if (playerMov[index][FLOATPOS] < 0.0)
- {
- playerMov[index][FLOATPOS] += NUM_LEDS;
- }
- playerPos[index][POS] = (int)(playerMov[index][FLOATPOS] + 0.5);
- }
- void changePlayerLane(int player, int direction) // fcking moe <- check again please
- {
- if (direction == RIGHTY && playerPos[player][LANE] != 3)
- {
- playerPos[player][LANE]++;
- }
- else if (direction == LEFTY && playerPos[player][LANE] != 0)
- {
- playerPos[player][LANE]--;
- }
- };
- int isLaneEmpty(int player, int direction) //fcking moe <- check again please
- {
- for (int i = 0; i < playerAmount; i++)
- {
- if (player != i)
- {
- if ((playerPos[player][LANE] + 1) != playerPos[i][LANE])
- {
- return 4;
- }else{
- return i;
- }
- }
- }
- }
- void changeOrbLane(int orb)
- {
- if (redOrbPos[orb][LANE] == 0)
- {
- redOrbPos[orb][LANE]++;
- }
- else if (redOrbPos[orb][LANE] == 3)
- {
- redOrbPos[orb][LANE]--;
- }
- else
- {
- direction = direction * -1;
- redOrbPos[orb][LANE] += direction;
- }
- };
- /////////////////////////////////////////////////////////////////// HIT DETECTION
- void hitDetectionOrb(int orbX, int orbY)
- {
- if (orbX != orbY)
- {
- if (redOrbPos[orbX][LANE] == redOrbPos[orbY][LANE] && redOrbPos[orbY][POS] == (redOrbPos[orbX][POS] - 1))
- {
- if (redOrbMov[orbX][VELOCITY] > redOrbMov[orbY][VELOCITY])
- {
- redOrbPos[orbX][POS]++; //but why?
- }
- else
- {
- changeOrbLane(orbY); //please debug this
- }
- }
- }
- }
- void hitDetectionPlayer(int pX, int pY)
- {
- if (pX != pY)
- {
- if (playerPos[pX][OLDPOS] <= playerPos[pY][POS] && playerPos[pX][POS] > playerPos[pY][POS])
- {
- if (playerPos[pX][LANE] == playerPos[pY][LANE])
- {
- playerMov[pX][VELOCITY] += knockback;
- playerMov[pX][SPEED] = defaultSpeed;
- playerMov[pY][SPEED] = defaultSpeed;
- movePlayer(pX); //?
- }
- }
- }
- }
- void hitDetectionPlayerOrb(int player, int orb) //Add detection from behind.
- {
- if (playerPos[player][OLDPOS] <= redOrbPos[orb][POS] && playerPos[player][POS] > redOrbPos[orb][POS])
- {
- if (playerPos[player][LANE] != redOrbPos[orb][LANE])
- {
- playerMov[player][SPEED] += boost;
- }
- else
- {
- playerMov[player][VELOCITY] += knockback;
- playerMov[player][SPEED] = defaultSpeed;
- movePlayer(player); //?
- }
- }
- }
- /////////////////////////////////////////////////////////////////// UPDATE LEDS
- void updatePlayer(int index)
- {
- int currentColor;
- switch (index)
- {
- case 0:
- currentColor = CRGB::Blue;
- break;
- case 1:
- currentColor = CRGB::Purple;
- break;
- case 2:
- currentColor = CRGB::Green;
- break;
- case 3:
- currentColor = CRGB::Yellow;
- break;
- }
- switch (playerPos[index][LANE])
- {
- case 0:
- leds1[playerPos[index][POS]] = currentColor;
- break;
- case 1:
- leds2[playerPos[index][POS]] = currentColor;
- break;
- case 2:
- leds3[playerPos[index][POS]] = currentColor;
- break;
- case 3:
- leds4[playerPos[index][POS]] = currentColor;
- break;
- }
- }
- void updateOrb(int index)
- {
- switch (redOrbPos[index][LANE])
- {
- case 0:
- leds1[redOrbPos[index][POS]] = CRGB::Red;
- break;
- case 1:
- leds2[redOrbPos[index][POS]] = CRGB::Red;
- break;
- case 2:
- leds3[redOrbPos[index][POS]] = CRGB::Red;
- break;
- case 3:
- leds4[redOrbPos[index][POS]] = CRGB::Red;
- break;
- }
- }
- /////////////////////////////////////////////////////////////////// CREATE
- void createPlayers()
- {
- for (int i = 0; i < playerAmount; i++)
- {
- playerMov[i][VELOCITY] = 0.0;
- playerMov[i][FLOATPOS] = 0.0;
- playerMov[i][SPEED] = defaultSpeed;
- playerMov[i][ACCELERATION] = defaultAcceleration;
- playerPos[i][LANE] = i;
- playerPos[i][POS] = 0;
- playerPos[i][OLDPOS] = 0;
- }
- }
- void createOrbs()
- {
- for (int i = 0; i < redOrbAmount; i++)
- {
- int lane = random(0, 4);
- float posFloat = random(0, NUM_LEDS);
- float velocity = random(orbminspeed, orbmaxspeed);
- velocity = velocity / 100;
- redOrbPos[i][LANE] = lane;
- redOrbMov[i][VELOCITY] = velocity;
- redOrbMov[i][FLOATPOS] = posFloat;
- }
- }
- /////////////////////////////////////////////////////////////////// SETUP
- void setupLEDS()
- {
- FastLED.addLeds<NEOPIXEL, LED1>(leds1, NUM_LEDS);
- FastLED.addLeds<NEOPIXEL, LED2>(leds2, NUM_LEDS);
- FastLED.addLeds<NEOPIXEL, LED3>(leds3, NUM_LEDS);
- FastLED.addLeds<NEOPIXEL, LED4>(leds4, NUM_LEDS);
- }
Advertisement
Add Comment
Please, Sign In to add comment