ECE 2035 AgarIO MiniProject

Dependencies:   4DGL-uLCD-SE ECE2035_Agar_Shell EthernetInterface Game_Synchronizer MMA8452 SDFileSystem Sound USBDevice mbed-rtos mbed wave_player

Fork of ECE2035_Agar_Shell by ECE2035 Spring 2015 TA

This program is a MiniProject given in ECE 2035. This is to design and program the multiplayer ethernet based AGAR.IO mbed game. Starting code is given by the instructor to aid student to complete the project. It utilize uLCD, Accelerometer, pushbuttons, sdFileSystem, ethernetBreakoutBoard, speaker in mBED LPC1768. It uses the similar concept to the Game http://agar.io/

Committer:
pkoirala3
Date:
Sat Mar 18 14:55:51 2017 +0000
Revision:
1:e487713a5231
Parent:
0:9d6ea88b6d14
EC 2035 MiniProject AGARIO

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jford38 0:9d6ea88b6d14 1 #include "blob.h"
jford38 0:9d6ea88b6d14 2 #include "mbed.h"
jford38 0:9d6ea88b6d14 3
jford38 0:9d6ea88b6d14 4 extern Serial pc;
jford38 0:9d6ea88b6d14 5
pkoirala3 1:e487713a5231 6 // Take in a blob and determine whether it is inside the world.
pkoirala3 1:e487713a5231 7 // If the blob has escaped the world, put it back on the edge
pkoirala3 1:e487713a5231 8 // of the world and negate its velocity so that it bounces off
pkoirala3 1:e487713a5231 9 // the boundary. Use WORLD_WIDTH and WORLD_HEIGHT defined in "misc.h"
pkoirala3 1:e487713a5231 10 void BLOB_constrain2world(BLOB* b) {
pkoirala3 1:e487713a5231 11 // ***
pkoirala3 1:e487713a5231 12 // checking for x-based constraints
pkoirala3 1:e487713a5231 13 if ((b->posx - b->rad) <= 0 || (b->posx + b->rad) >= WORLD_WIDTH) {
pkoirala3 1:e487713a5231 14 b->vx = -1 * b->vx;
pkoirala3 1:e487713a5231 15 if (b->posx < 0) {
pkoirala3 1:e487713a5231 16 b->posx = b->rad;
pkoirala3 1:e487713a5231 17 } else if (b->posx > WORLD_WIDTH) {
pkoirala3 1:e487713a5231 18 b->posx = WORLD_WIDTH - b->rad;
pkoirala3 1:e487713a5231 19 }
pkoirala3 1:e487713a5231 20 }
pkoirala3 1:e487713a5231 21 // checking for y-based constraints
pkoirala3 1:e487713a5231 22 if ((b->posy - b->rad) <= 0 || (b->posy + b->rad) >= WORLD_HEIGHT)
pkoirala3 1:e487713a5231 23 b->vy = -1 * b->vy;
pkoirala3 1:e487713a5231 24 if (b->posy < 0) {
pkoirala3 1:e487713a5231 25 b->posy = b->rad;
pkoirala3 1:e487713a5231 26 } else if (b->posy > WORLD_HEIGHT) {
pkoirala3 1:e487713a5231 27 b->posy = WORLD_HEIGHT - b->rad;
pkoirala3 1:e487713a5231 28 }
pkoirala3 1:e487713a5231 29 }
jford38 0:9d6ea88b6d14 30
jford38 0:9d6ea88b6d14 31 // Randomly initialize a blob's position, velocity, color, radius, etc.
jford38 0:9d6ea88b6d14 32 // Set the valid flag to true and the delete_now flag to false.
jford38 0:9d6ea88b6d14 33 // delete_now is basically the derivative of valid. It goes true for one
jford38 0:9d6ea88b6d14 34 // fram when the blob is deleted, and then it is reset to false in the next frame
jford38 0:9d6ea88b6d14 35 // when that blob is deleted.
jford38 0:9d6ea88b6d14 36 void BLOB_init(BLOB* b) {
jford38 0:9d6ea88b6d14 37 // ***
pkoirala3 1:e487713a5231 38 // get a random radius
pkoirala3 1:e487713a5231 39 int rad = (rand() % 5) + 1;
pkoirala3 1:e487713a5231 40 BLOB_init(b, rad);
jford38 0:9d6ea88b6d14 41 }
jford38 0:9d6ea88b6d14 42
jford38 0:9d6ea88b6d14 43 // Randomly initialize a blob. Then set the radius to the provided value.
jford38 0:9d6ea88b6d14 44 void BLOB_init(BLOB* b, int rad) {
jford38 0:9d6ea88b6d14 45 // ***
pkoirala3 1:e487713a5231 46 // using food color
pkoirala3 1:e487713a5231 47 BLOB_init(b, rad, FOOD_COL);
jford38 0:9d6ea88b6d14 48 }
jford38 0:9d6ea88b6d14 49
pkoirala3 1:e487713a5231 50 // Randomly initialize a blob. Then set the radius and color to the
jford38 0:9d6ea88b6d14 51 // provided values.
jford38 0:9d6ea88b6d14 52 void BLOB_init(BLOB* b, int rad, int color) {
jford38 0:9d6ea88b6d14 53 // ***
pkoirala3 1:e487713a5231 54 // get random (x,y,) for blob pos
pkoirala3 1:e487713a5231 55 int genSpace_x = WORLD_WIDTH - (3*rad);
pkoirala3 1:e487713a5231 56 int genSpace_y = WORLD_HEIGHT - (3*rad);
pkoirala3 1:e487713a5231 57
pkoirala3 1:e487713a5231 58 int rand_x = (rand() % genSpace_x) + rad;
pkoirala3 1:e487713a5231 59 int rand_y = (rand() % genSpace_y) + rad;
pkoirala3 1:e487713a5231 60
pkoirala3 1:e487713a5231 61 b->posx = rand_x;
pkoirala3 1:e487713a5231 62 b->posy = rand_y;
pkoirala3 1:e487713a5231 63 b->old_x = rand_x;
pkoirala3 1:e487713a5231 64 b->old_y = rand_y;
pkoirala3 1:e487713a5231 65 b->vx = ACC_THRESHOLD * 1000;
pkoirala3 1:e487713a5231 66 b->vy = ACC_THRESHOLD * 1000;
pkoirala3 1:e487713a5231 67 b->rad = rad;
pkoirala3 1:e487713a5231 68 b->color = color;
pkoirala3 1:e487713a5231 69 b->valid = 1;
pkoirala3 1:e487713a5231 70 b->delete_now = 0;
jford38 0:9d6ea88b6d14 71 }
jford38 0:9d6ea88b6d14 72
jford38 0:9d6ea88b6d14 73 // For debug purposes, you can use this to print a blob's properties to your computer's serial monitor.
jford38 0:9d6ea88b6d14 74 void BLOB_print(BLOB b) {
pkoirala3 1:e487713a5231 75 pc.printf("cur(%.3f, %.3f) old(%.3f, %.3f) vel<%.3f, %.3f> Col: 0x%x\n", b.posx, b.posy, b.old_x, b.old_y, b.vx, b.vy, b.color);
jford38 0:9d6ea88b6d14 76 }
jford38 0:9d6ea88b6d14 77
jford38 0:9d6ea88b6d14 78 // Return the square of the distance from b1 to b2
jford38 0:9d6ea88b6d14 79 float BLOB_dist2(BLOB b1, BLOB b2) {
jford38 0:9d6ea88b6d14 80 // ***
pkoirala3 1:e487713a5231 81 float x1 = b1.posx;
pkoirala3 1:e487713a5231 82 float y1 = b1.posy;
pkoirala3 1:e487713a5231 83
pkoirala3 1:e487713a5231 84 float x2 = b2.posx;
pkoirala3 1:e487713a5231 85 float y2 = b2.posy;
pkoirala3 1:e487713a5231 86
pkoirala3 1:e487713a5231 87 return ((x2-x1) *(x2-x1)) + ((y2-y1) * (y2-y1));
pkoirala3 1:e487713a5231 88 }