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);
top = (ballCenter.y) < (rect->y);
bottom = (ballCenter.y) > (rect->y) + (rect->h);
if (top)
corner.y = rect->y;
else if (bottom)
corner.y = (rect->y) + (rect->w);
else
yMid = true;
if (left)
corner.x = rect->x;
else if (right)
corner.x = (rect->x) + (rect->w);
else
xMid = true;
// if (top)
// corner.y = rect->y;
// else if (bottom)
// corner.y = (rect->y) + (rect->w);
// else
// yMid = true;
// if (left)
// corner.x = rect->x;
// else if (right)
// corner.x = (rect->x) + (rect->w);
// else
// xMid = true;
yMid = !(top || bottom);
xMid = !(left || right);
if (yMid)
(obj->Momentum).x = -(obj->Momentum).x;
if (xMid)
(obj->Momentum).y = -(obj->Momentum).y;
if (yMid && xMid) printf("WARNING: Ball is completely inside block!\n");
if (yMid || xMid) { // Ball collides with Edge
} else { // Ball collides with corner
/*
* perpendicular = vectorRotation(vectorSub(center, corner));
* angle = fabs(perpendicular - oldMomentum);
* if (oldMomentum < perpendicular)
* (obj->Momentum) = getScaledDirectionalUnitVector((oldMomentum + (2 * angle)), (obj->Speed));
* else
* (obj->Momentum) = getScaledDirectionalUnitVector((oldMomentum - (2 * angle)), (obj->Speed));
*/
}
// if (yMid && xMid) printf("WARNING: Ball is completely inside block!\n");
// if (yMid || xMid) { // Ball collides with Edge
// } else { // Ball collides with corner
// /*
// * perpendicular = vectorRotation(vectorSub(center, corner));
// * angle = fabs(perpendicular - oldMomentum);
// * if (oldMomentum < perpendicular)
// * (obj->Momentum) = getScaledDirectionalUnitVector((oldMomentum + (2 * angle)), (obj->Speed));
// * else
// * (obj->Momentum) = getScaledDirectionalUnitVector((oldMomentum - (2 * angle)), (obj->Speed));
// */
// }
return true;
} /* 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->Location) = vectorAdd((obj->Location), oldMomentum);
if (BALL_CollideWithRect(obj, &(paddle->TargetRect))) {
if (RECT_Collide(&(obj->TargetRect), &(paddle->TargetRect))) {
(obj->Location) = oldLocation; // Maybe remove this
BALL_SteerMomentum(obj, paddle); // Sets it to unit vector!
// 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 },
.TextureIndex = 0,
.Speed = 10,
.SteeringAngle = 40.0f
.SteeringAngle = 40.0f,
.Mode = MouseControl
}; // 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){
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)) {
((obj->TargetRect).x) -= (obj->Speed);
@ -356,7 +380,7 @@ void PADDLE_Update(Paddle * obj, const Uint8 * keystate){
((obj->TargetRect).x) += (obj->Speed);
}
INT_Constrain(&((obj->TargetRect).x), 0, (BREAKOUT_BoxWidth - ((obj->TargetRect).w)));
}
} /* PADDLE_Update */
void PADDLE_DestroyObject(Paddle * obj){
}

View File

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

7
main.c
View File

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

0
paddle.c Normal file
View File