Kahden pelaajan "Minibiljardi"
Dependencies: PokittoLib
GameObject.cpp
00001 #include "Pokitto.h" 00002 #include "GameObject.h" 00003 00004 Pokitto::Display gobj_d; 00005 int flipper = 0; 00006 00007 00008 GameObject::GameObject() { 00009 x = random(-20,20); 00010 y = random(-15,15); 00011 vx = 0;//random(-100,100)/100.0f; 00012 vy = 0;//random(-100,100)/100.0f; 00013 width = 5; 00014 height = 5; 00015 if (flipper) color = 7; //random(3,12); 00016 else color = 4; 00017 flipper = 1 - flipper; 00018 if (color == 5) color = 2; 00019 orig_color = color; 00020 bounce = 0.9f; 00021 acceleration = 0.15f; 00022 friction = 0.98f; 00023 mass = 1.0f; 00024 centerX = 55+x; 00025 centerY = 44+y; 00026 movable=true; 00027 visible=true; 00028 active=true; 00029 } 00030 00031 void GameObject::draw() { 00032 if (!visible) return; 00033 gobj_d.setColor(color); 00034 gobj_d.fillRect(55-width/2+x,44-height/2+y,width,height); 00035 centerX = 55+x; 00036 centerY = 44+y; 00037 } 00038 00039 void GameObject::move() { 00040 if (!active) return; 00041 if (!movable) return; 00042 x += vx; 00043 y += vy; 00044 centerX = 55+x; 00045 centerY = 44+y; 00046 vx *= friction; 00047 vy *= friction; 00048 if (abs(vx*10) < 1 && abs(vy*10) <1) vx = vy = 0; // practically stopped 00049 } 00050 00051 bool GameObject::isMoving() { 00052 if (vx == 0 && vy == 0) return false; 00053 return true; 00054 } 00055 00056 void GameObject::remove() { 00057 active=false; 00058 visible=false; 00059 vx = 0; 00060 vy = 0; 00061 } 00062 00063 void GameObject::moveX(float n) { 00064 x += n; 00065 centerX = 55+x; 00066 } 00067 00068 void GameObject::moveY(float n) { 00069 y += n; 00070 centerY = 44+y; 00071 } 00072 00073 void GameObject::accelerateX(float a) { 00074 vx += a*acceleration; 00075 } 00076 00077 void GameObject::accelerateY(float a) { 00078 vy += a*acceleration; 00079 } 00080 00081 void GameObject::bounceY() { 00082 vy = -vy * bounce; 00083 } 00084 00085 void GameObject::bounceX() { 00086 vx = -vx * bounce; 00087 } 00088 00089 void GameObject::bounceY(float bouncefactor) { 00090 vy = -vy * (bounce*bouncefactor); 00091 } 00092 00093 void GameObject::bounceX(float bouncefactor) { 00094 vx = -vx * (bounce*bouncefactor); 00095 } 00096 00097 bool GameObject::checkCollision(GameObject& other) { 00098 if (active == false || other.active == false) return false; 00099 other.color = 2; 00100 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; 00101 other.color = other.orig_color; 00102 return false; 00103 } 00104 00105 void GameObject::doCollision(GameObject& other) { 00106 if (active == false || other.active == false) return; 00107 if (!other.movable) { 00108 //movable vs. immovable collision 00109 if (x - width/2 < other.x - other.width/2 || x + width/2 > other.x + other.width/2) { 00110 // collision in vertical wall 00111 bounceX(); 00112 } else { 00113 // collision in horizontal wall 00114 bounceY(); 00115 } 00116 } else { 00117 // movable vs. movable 00118 //vx = ((mass-other.mass)/sum_mass)*vx + ((other.mass*2)/sum_mass)*other.vx; 00119 //other.vx = ((mass*2)/sum_mass)*vx + ((other.mass-mass)/sum_mass)*other.vx; 00120 //vy = ((mass-other.mass)/sum_mass)*vy + ((other.mass*2)/sum_mass)*other.vy; 00121 //other.vy = ((mass*2)/sum_mass)*vy + ((other.mass-mass)/sum_mass)*other.vy; 00122 float tvx, tvy; 00123 tvx = vx; tvy = vy; 00124 vx = other.vx*bounce; vy = other.vy*bounce; 00125 other.vx = tvx*bounce; other.vy = tvy*bounce; 00126 } 00127 // move the object out of each other to avoid more collisions 00128 int i=0; 00129 while (checkCollision(other) && ++i < 10) {move(); if (other.movable) other.move();} 00130 } 00131 00132 void GameObject::setColor(int c) { 00133 color = c; 00134 orig_color = c; 00135 }
Generated on Wed Jul 13 2022 19:11:34 by 1.7.2