Added mouse control Mode

This commit is contained in:
Michael Chen 2018-01-18 11:45:19 +01:00
parent 7e9eaf5861
commit 96fa0960d0
4 changed files with 61 additions and 31 deletions

View File

@ -168,34 +168,36 @@ bool BALL_CollideWithRect(Ball * obj, SDL_Rect * rect){
right = (ballCenter.x) > (rect->x) + (rect->w); right = (ballCenter.x) > (rect->x) + (rect->w);
top = (ballCenter.y) < (rect->y); top = (ballCenter.y) < (rect->y);
bottom = (ballCenter.y) > (rect->y) + (rect->h); bottom = (ballCenter.y) > (rect->y) + (rect->h);
if (top) // if (top)
corner.y = rect->y; // corner.y = rect->y;
else if (bottom) // else if (bottom)
corner.y = (rect->y) + (rect->w); // corner.y = (rect->y) + (rect->w);
else // else
yMid = true; // yMid = true;
if (left) // if (left)
corner.x = rect->x; // corner.x = rect->x;
else if (right) // else if (right)
corner.x = (rect->x) + (rect->w); // corner.x = (rect->x) + (rect->w);
else // else
xMid = true; // xMid = true;
yMid = !(top || bottom);
xMid = !(left || right);
if (yMid) if (yMid)
(obj->Momentum).x = -(obj->Momentum).x; (obj->Momentum).x = -(obj->Momentum).x;
if (xMid) if (xMid)
(obj->Momentum).y = -(obj->Momentum).y; (obj->Momentum).y = -(obj->Momentum).y;
if (yMid && xMid) printf("WARNING: Ball is completely inside block!\n"); // if (yMid && xMid) printf("WARNING: Ball is completely inside block!\n");
if (yMid || xMid) { // Ball collides with Edge // if (yMid || xMid) { // Ball collides with Edge
} else { // Ball collides with corner // } else { // Ball collides with corner
/* // /*
* perpendicular = vectorRotation(vectorSub(center, corner)); // * perpendicular = vectorRotation(vectorSub(center, corner));
* angle = fabs(perpendicular - oldMomentum); // * angle = fabs(perpendicular - oldMomentum);
* if (oldMomentum < perpendicular) // * if (oldMomentum < perpendicular)
* (obj->Momentum) = getScaledDirectionalUnitVector((oldMomentum + (2 * angle)), (obj->Speed)); // * (obj->Momentum) = getScaledDirectionalUnitVector((oldMomentum + (2 * angle)), (obj->Speed));
* else // * else
* (obj->Momentum) = getScaledDirectionalUnitVector((oldMomentum - (2 * angle)), (obj->Speed)); // * (obj->Momentum) = getScaledDirectionalUnitVector((oldMomentum - (2 * angle)), (obj->Speed));
*/ // */
} // }
return true; return true;
} /* BALL_CollideWithRect */ } /* BALL_CollideWithRect */
@ -242,7 +244,7 @@ void BALL_Update(Ball * obj, Paddle * paddle, Block * blocks, int BlockCount){
(obj->Rotation) += (obj->RotationValue); // No effect on physics (obj->Rotation) += (obj->RotationValue); // No effect on physics
(obj->Location) = vectorAdd((obj->Location), oldMomentum); (obj->Location) = vectorAdd((obj->Location), oldMomentum);
if (BALL_CollideWithRect(obj, &(paddle->TargetRect))) { if (RECT_Collide(&(obj->TargetRect), &(paddle->TargetRect))) {
(obj->Location) = oldLocation; // Maybe remove this (obj->Location) = oldLocation; // Maybe remove this
BALL_SteerMomentum(obj, paddle); // Sets it to unit vector! BALL_SteerMomentum(obj, paddle); // Sets it to unit vector!
// Following assumes that the paddle position was udated before the ball was updated // Following assumes that the paddle position was udated before the ball was updated
@ -315,7 +317,8 @@ Paddle PADDLE_CreateDefault(){
.TargetRect = (SDL_Rect) {.x = (BREAKOUT_BoxWidth - defaultpaddlewidth) / 2, .y = BREAKOUT_BoxHeight - 100, .w = defaultpaddlewidth, .h = 30 }, .TargetRect = (SDL_Rect) {.x = (BREAKOUT_BoxWidth - defaultpaddlewidth) / 2, .y = BREAKOUT_BoxHeight - 100, .w = defaultpaddlewidth, .h = 30 },
.TextureIndex = 0, .TextureIndex = 0,
.Speed = 10, .Speed = 10,
.SteeringAngle = 40.0f .SteeringAngle = 40.0f,
.Mode = MouseControl
}; // Objekt für die Eigenschaften des Balls }; // Objekt für die Eigenschaften des Balls
} }
@ -348,7 +351,28 @@ void DOUBLE_Constrain(double * variable, double min, double max){
} }
void PADDLE_Update(Paddle * obj, const Uint8 * keystate){ void PADDLE_Update(Paddle * obj, const Uint8 * keystate){
bool leftKeyPressed = KeyPressed(keystate, PADDLE_MoveLeftKeys), rightKeyPressed = KeyPressed(keystate, PADDLE_MoveRightKeys); bool leftKeyPressed = false, rightKeyPressed = false;
int paddleXMid = (obj->TargetRect).x + ((obj->TargetRect).w / 2);
int mouseX;
switch (obj->Mode) {
case MouseControl:
SDL_GetMouseState(&mouseX, NULL);
if (abs(mouseX - paddleXMid) > (obj->Speed)) {
if (mouseX > paddleXMid)
rightKeyPressed = true;
else
leftKeyPressed = true;
}
break;
case KeyboardControl:
leftKeyPressed = KeyPressed(keystate, PADDLE_MoveLeftKeys);
rightKeyPressed = KeyPressed(keystate, PADDLE_MoveRightKeys);
break;
default:
printf("Unknown Paddle Control Mode: %d!\n", obj->Mode);
break;
}
if (leftKeyPressed && (!rightKeyPressed)) { if (leftKeyPressed && (!rightKeyPressed)) {
((obj->TargetRect).x) -= (obj->Speed); ((obj->TargetRect).x) -= (obj->Speed);
@ -356,7 +380,7 @@ void PADDLE_Update(Paddle * obj, const Uint8 * keystate){
((obj->TargetRect).x) += (obj->Speed); ((obj->TargetRect).x) += (obj->Speed);
} }
INT_Constrain(&((obj->TargetRect).x), 0, (BREAKOUT_BoxWidth - ((obj->TargetRect).w))); INT_Constrain(&((obj->TargetRect).x), 0, (BREAKOUT_BoxWidth - ((obj->TargetRect).w)));
} } /* PADDLE_Update */
void PADDLE_DestroyObject(Paddle * obj){ void PADDLE_DestroyObject(Paddle * obj){
} }

View File

@ -9,6 +9,10 @@
#include "vector.h" #include "vector.h"
// Enums
typedef enum controlModeEnum {KeyboardControl = 0, MouseControl = 1} ControlMode;
// End Enums
// Structs // Structs
typedef struct ballStruct { typedef struct ballStruct {
Vector Location, Momentum; Vector Location, Momentum;
@ -23,6 +27,7 @@ typedef struct paddleStruct {
int TextureIndex; int TextureIndex;
int Speed; int Speed;
double SteeringAngle; double SteeringAngle;
ControlMode Mode;
} Paddle; // Objekt für die Eigenschaften des Paddles } Paddle; // Objekt für die Eigenschaften des Paddles
typedef struct blockStruct { typedef struct blockStruct {

7
main.c
View File

@ -101,8 +101,9 @@ void HandleSDLEvents(){
running = false; running = false;
break; break;
case SDL_KEYDOWN: case SDL_KEYDOWN:
if (event.key.keysym.scancode == SDL_SCANCODE_ESCAPE) running = false; // if (event.key.keysym.scancode == SDL_SCANCODE_ESCAPE) running = false;
else keyPress(event.key); // else
keyPress(event.key);
break; break;
case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONDOWN:
mousePress(event.button); mousePress(event.button);
@ -112,7 +113,7 @@ void HandleSDLEvents(){
break; break;
} }
} }
} } /* HandleSDLEvents */
void mousePress(SDL_MouseButtonEvent b){ // Debug prop void mousePress(SDL_MouseButtonEvent b){ // Debug prop
if (b.button == SDL_BUTTON_LEFT) { if (b.button == SDL_BUTTON_LEFT) {

0
paddle.c Normal file
View File