Kahden pelaajan "Minibiljardi"

Dependencies:   PokittoLib

Committer:
Pokitto
Date:
Wed Oct 10 08:00:57 2018 +0000
Revision:
0:89d0cb201659
Alku

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Pokitto 0:89d0cb201659 1 #include "Pokitto.h"
Pokitto 0:89d0cb201659 2 #include "GameObject.h"
Pokitto 0:89d0cb201659 3
Pokitto 0:89d0cb201659 4 Pokitto::Display gobj_d;
Pokitto 0:89d0cb201659 5 int flipper = 0;
Pokitto 0:89d0cb201659 6
Pokitto 0:89d0cb201659 7
Pokitto 0:89d0cb201659 8 GameObject::GameObject() {
Pokitto 0:89d0cb201659 9 x = random(-20,20);
Pokitto 0:89d0cb201659 10 y = random(-15,15);
Pokitto 0:89d0cb201659 11 vx = 0;//random(-100,100)/100.0f;
Pokitto 0:89d0cb201659 12 vy = 0;//random(-100,100)/100.0f;
Pokitto 0:89d0cb201659 13 width = 5;
Pokitto 0:89d0cb201659 14 height = 5;
Pokitto 0:89d0cb201659 15 if (flipper) color = 7; //random(3,12);
Pokitto 0:89d0cb201659 16 else color = 4;
Pokitto 0:89d0cb201659 17 flipper = 1 - flipper;
Pokitto 0:89d0cb201659 18 if (color == 5) color = 2;
Pokitto 0:89d0cb201659 19 orig_color = color;
Pokitto 0:89d0cb201659 20 bounce = 0.9f;
Pokitto 0:89d0cb201659 21 acceleration = 0.15f;
Pokitto 0:89d0cb201659 22 friction = 0.98f;
Pokitto 0:89d0cb201659 23 mass = 1.0f;
Pokitto 0:89d0cb201659 24 centerX = 55+x;
Pokitto 0:89d0cb201659 25 centerY = 44+y;
Pokitto 0:89d0cb201659 26 movable=true;
Pokitto 0:89d0cb201659 27 visible=true;
Pokitto 0:89d0cb201659 28 active=true;
Pokitto 0:89d0cb201659 29 }
Pokitto 0:89d0cb201659 30
Pokitto 0:89d0cb201659 31 void GameObject::draw() {
Pokitto 0:89d0cb201659 32 if (!visible) return;
Pokitto 0:89d0cb201659 33 gobj_d.setColor(color);
Pokitto 0:89d0cb201659 34 gobj_d.fillRect(55-width/2+x,44-height/2+y,width,height);
Pokitto 0:89d0cb201659 35 centerX = 55+x;
Pokitto 0:89d0cb201659 36 centerY = 44+y;
Pokitto 0:89d0cb201659 37 }
Pokitto 0:89d0cb201659 38
Pokitto 0:89d0cb201659 39 void GameObject::move() {
Pokitto 0:89d0cb201659 40 if (!active) return;
Pokitto 0:89d0cb201659 41 if (!movable) return;
Pokitto 0:89d0cb201659 42 x += vx;
Pokitto 0:89d0cb201659 43 y += vy;
Pokitto 0:89d0cb201659 44 centerX = 55+x;
Pokitto 0:89d0cb201659 45 centerY = 44+y;
Pokitto 0:89d0cb201659 46 vx *= friction;
Pokitto 0:89d0cb201659 47 vy *= friction;
Pokitto 0:89d0cb201659 48 if (abs(vx*10) < 1 && abs(vy*10) <1) vx = vy = 0; // practically stopped
Pokitto 0:89d0cb201659 49 }
Pokitto 0:89d0cb201659 50
Pokitto 0:89d0cb201659 51 bool GameObject::isMoving() {
Pokitto 0:89d0cb201659 52 if (vx == 0 && vy == 0) return false;
Pokitto 0:89d0cb201659 53 return true;
Pokitto 0:89d0cb201659 54 }
Pokitto 0:89d0cb201659 55
Pokitto 0:89d0cb201659 56 void GameObject::remove() {
Pokitto 0:89d0cb201659 57 active=false;
Pokitto 0:89d0cb201659 58 visible=false;
Pokitto 0:89d0cb201659 59 vx = 0;
Pokitto 0:89d0cb201659 60 vy = 0;
Pokitto 0:89d0cb201659 61 }
Pokitto 0:89d0cb201659 62
Pokitto 0:89d0cb201659 63 void GameObject::moveX(float n) {
Pokitto 0:89d0cb201659 64 x += n;
Pokitto 0:89d0cb201659 65 centerX = 55+x;
Pokitto 0:89d0cb201659 66 }
Pokitto 0:89d0cb201659 67
Pokitto 0:89d0cb201659 68 void GameObject::moveY(float n) {
Pokitto 0:89d0cb201659 69 y += n;
Pokitto 0:89d0cb201659 70 centerY = 44+y;
Pokitto 0:89d0cb201659 71 }
Pokitto 0:89d0cb201659 72
Pokitto 0:89d0cb201659 73 void GameObject::accelerateX(float a) {
Pokitto 0:89d0cb201659 74 vx += a*acceleration;
Pokitto 0:89d0cb201659 75 }
Pokitto 0:89d0cb201659 76
Pokitto 0:89d0cb201659 77 void GameObject::accelerateY(float a) {
Pokitto 0:89d0cb201659 78 vy += a*acceleration;
Pokitto 0:89d0cb201659 79 }
Pokitto 0:89d0cb201659 80
Pokitto 0:89d0cb201659 81 void GameObject::bounceY() {
Pokitto 0:89d0cb201659 82 vy = -vy * bounce;
Pokitto 0:89d0cb201659 83 }
Pokitto 0:89d0cb201659 84
Pokitto 0:89d0cb201659 85 void GameObject::bounceX() {
Pokitto 0:89d0cb201659 86 vx = -vx * bounce;
Pokitto 0:89d0cb201659 87 }
Pokitto 0:89d0cb201659 88
Pokitto 0:89d0cb201659 89 void GameObject::bounceY(float bouncefactor) {
Pokitto 0:89d0cb201659 90 vy = -vy * (bounce*bouncefactor);
Pokitto 0:89d0cb201659 91 }
Pokitto 0:89d0cb201659 92
Pokitto 0:89d0cb201659 93 void GameObject::bounceX(float bouncefactor) {
Pokitto 0:89d0cb201659 94 vx = -vx * (bounce*bouncefactor);
Pokitto 0:89d0cb201659 95 }
Pokitto 0:89d0cb201659 96
Pokitto 0:89d0cb201659 97 bool GameObject::checkCollision(GameObject& other) {
Pokitto 0:89d0cb201659 98 if (active == false || other.active == false) return false;
Pokitto 0:89d0cb201659 99 other.color = 2;
Pokitto 0:89d0cb201659 100 if ((x + width/2 >= other.x - other.width/2) && (x - width/2 < other.x + other.width/2) && (y + height/2 >= other.y - other.height/2) && (y - height/2 < other.y + other.height/2)) return true;
Pokitto 0:89d0cb201659 101 other.color = other.orig_color;
Pokitto 0:89d0cb201659 102 return false;
Pokitto 0:89d0cb201659 103 }
Pokitto 0:89d0cb201659 104
Pokitto 0:89d0cb201659 105 void GameObject::doCollision(GameObject& other) {
Pokitto 0:89d0cb201659 106 if (active == false || other.active == false) return;
Pokitto 0:89d0cb201659 107 if (!other.movable) {
Pokitto 0:89d0cb201659 108 //movable vs. immovable collision
Pokitto 0:89d0cb201659 109 if (x - width/2 < other.x - other.width/2 || x + width/2 > other.x + other.width/2) {
Pokitto 0:89d0cb201659 110 // collision in vertical wall
Pokitto 0:89d0cb201659 111 bounceX();
Pokitto 0:89d0cb201659 112 } else {
Pokitto 0:89d0cb201659 113 // collision in horizontal wall
Pokitto 0:89d0cb201659 114 bounceY();
Pokitto 0:89d0cb201659 115 }
Pokitto 0:89d0cb201659 116 } else {
Pokitto 0:89d0cb201659 117 // movable vs. movable
Pokitto 0:89d0cb201659 118 //vx = ((mass-other.mass)/sum_mass)*vx + ((other.mass*2)/sum_mass)*other.vx;
Pokitto 0:89d0cb201659 119 //other.vx = ((mass*2)/sum_mass)*vx + ((other.mass-mass)/sum_mass)*other.vx;
Pokitto 0:89d0cb201659 120 //vy = ((mass-other.mass)/sum_mass)*vy + ((other.mass*2)/sum_mass)*other.vy;
Pokitto 0:89d0cb201659 121 //other.vy = ((mass*2)/sum_mass)*vy + ((other.mass-mass)/sum_mass)*other.vy;
Pokitto 0:89d0cb201659 122 float tvx, tvy;
Pokitto 0:89d0cb201659 123 tvx = vx; tvy = vy;
Pokitto 0:89d0cb201659 124 vx = other.vx*bounce; vy = other.vy*bounce;
Pokitto 0:89d0cb201659 125 other.vx = tvx*bounce; other.vy = tvy*bounce;
Pokitto 0:89d0cb201659 126 }
Pokitto 0:89d0cb201659 127 // move the object out of each other to avoid more collisions
Pokitto 0:89d0cb201659 128 int i=0;
Pokitto 0:89d0cb201659 129 while (checkCollision(other) && ++i < 10) {move(); if (other.movable) other.move();}
Pokitto 0:89d0cb201659 130 }
Pokitto 0:89d0cb201659 131
Pokitto 0:89d0cb201659 132 void GameObject::setColor(int c) {
Pokitto 0:89d0cb201659 133 color = c;
Pokitto 0:89d0cb201659 134 orig_color = c;
Pokitto 0:89d0cb201659 135 }