Initial bullet implementation

This commit is contained in:
Furkan Mudanyali 2021-12-22 15:13:13 +03:00
parent 702da1f29b
commit 287f366972
4 changed files with 97 additions and 21 deletions

BIN
assets/player/bullet.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 378 B

View File

@ -0,0 +1,48 @@
/*
* Copyright (c) 2021 Furkan Mudanyali
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.fmudanyali.bullets;
import com.fmudanyali.FileLoader;
import com.fmudanyali.Render;
import com.fmudanyali.Time;
import org.libsdl.api.rect.SDL_Rect;
import org.libsdl.api.render.SDL_Texture;
import org.libsdl.api.surface.SDL_Surface;
import static org.libsdl.api.surface.SdlSurface.*;
import static org.libsdl.api.render.SdlRender.*;
public class PlayerBullet {
public static SDL_Surface tempSurface = SDL_LoadBMP(FileLoader.getFilePath("player/bullet.bmp"));
public static SDL_Texture texture = SDL_CreateTextureFromSurface(Render.renderer, tempSurface);
public SDL_Rect position = new SDL_Rect();
public int width = 5;
public int height = 12;
public PlayerBullet(SDL_Rect playerPos, int xOffset, int yOffset){
position.w = width;
position.h = height;
position.x = Math.min(Math.max(playerPos.x + xOffset, 300), 628 + 27);
position.y = playerPos.y - 6 - 3 - yOffset;
}
public void fly(){
position.y = position.y - (int)(Time.deltaTime * 0.5);
}
}

View File

@ -22,6 +22,8 @@ import com.fmudanyali.Render;
import com.fmudanyali.Screen; import com.fmudanyali.Screen;
import com.fmudanyali.Keyboard; import com.fmudanyali.Keyboard;
import com.fmudanyali.Time; import com.fmudanyali.Time;
import com.fmudanyali.bullets.PlayerBullet;
import com.fmudanyali.scenes.Game;
import org.libsdl.api.rect.SDL_Rect; import org.libsdl.api.rect.SDL_Rect;
import org.libsdl.api.render.SDL_Texture; import org.libsdl.api.render.SDL_Texture;
@ -37,6 +39,7 @@ public class Player extends Character {
public SDL_Texture shooter; public SDL_Texture shooter;
public int frame = 0; public int frame = 0;
public int roll = 0; public int roll = 0;
public int cooldown = 0;
public SDL_Rect propellerPos = new SDL_Rect(); public SDL_Rect propellerPos = new SDL_Rect();
public SDL_Rect shooterPos = new SDL_Rect(); public SDL_Rect shooterPos = new SDL_Rect();
@ -167,21 +170,12 @@ public class Player extends Character {
if(roll > 0){ if(roll > 0){
shipFrame.x = shipFrame.y = 0; shipFrame.x = shipFrame.y = 0;
} if(roll > 8){
shipFrame.x = 32;
} if(roll > 15){ } if(roll > 15){
shipFrame.x = 0;
shipFrame.y = 32;
} if(roll > 23){
shipFrame.x = 32; shipFrame.x = 32;
} if(roll > 30){ } if(roll > 30){
shipFrame.x = shipFrame.y = 0;
} if(roll > 38){
shipFrame.x = 32;
} if(roll > 45){
shipFrame.x = 0; shipFrame.x = 0;
shipFrame.y = 32; shipFrame.y = 32;
} if (roll > 53){ } if(roll > 45){
shipFrame.x = 32; shipFrame.x = 32;
} }
@ -192,20 +186,20 @@ public class Player extends Character {
if(Keyboard.getKeyState(SDL_SCANCODE_LSHIFT)){ if(Keyboard.getKeyState(SDL_SCANCODE_LSHIFT)){
speed = 1.5; speed = 1.5;
}else{ }else{
speed = 3; speed = 2;
} }
if(Keyboard.getKeyState(SDL_SCANCODE_A) | Keyboard.getKeyState(SDL_SCANCODE_LEFT)){ if(Keyboard.getKeyState(SDL_SCANCODE_A) | Keyboard.getKeyState(SDL_SCANCODE_LEFT)){
position.x = Math.max(position.x - (int)(speed * Time.deltaTime * 0.1), Screen.canvasPos.x); position.x = Math.max(position.x - (int)(speed * Time.deltaTime * 0.1), Screen.canvasPos.x - 5);
shooterPos.x = Math.max(shooterPos.x - (int)(speed * Time.deltaTime * 0.1), Screen.canvasPos.x + 16 - 4); shooterPos.x = Math.max(shooterPos.x - (int)(speed * Time.deltaTime * 0.1), Screen.canvasPos.x + 16 - 4 - 5);
propellerPos.x = Math.max(propellerPos.x - (int)(speed * Time.deltaTime * 0.1), Screen.canvasPos.x + 16 - 5); propellerPos.x = Math.max(propellerPos.x - (int)(speed * Time.deltaTime * 0.1), Screen.canvasPos.x + 16 - 5 - 5);
roll = Math.floorMod(roll + (int)(Time.deltaTime * 0.1),60); roll = Math.floorMod(roll + (int)(speed * Time.deltaTime * 0.1),60);
} }
if(Keyboard.getKeyState(SDL_SCANCODE_D) | Keyboard.getKeyState(SDL_SCANCODE_RIGHT)){ if(Keyboard.getKeyState(SDL_SCANCODE_D) | Keyboard.getKeyState(SDL_SCANCODE_RIGHT)){
position.x = Math.min(position.x + (int)(speed * Time.deltaTime * 0.1), Screen.canvasPos.x + Screen.canvas.w - 32); position.x = Math.min(position.x + (int)(speed * Time.deltaTime * 0.1), Screen.canvasPos.x + Screen.canvas.w - 27);
shooterPos.x = Math.min(shooterPos.x + (int)(speed * Time.deltaTime * 0.1), Screen.canvasPos.x + Screen.canvas.w - 16 - 4); shooterPos.x = Math.min(shooterPos.x + (int)(speed * Time.deltaTime * 0.1), Screen.canvasPos.x + Screen.canvas.w - 16 - 4 + 5);
propellerPos.x = Math.min(propellerPos.x + (int)(speed * Time.deltaTime * 0.1), Screen.canvasPos.x + Screen.canvas.w - 16 - 5); propellerPos.x = Math.min(propellerPos.x + (int)(speed * Time.deltaTime * 0.1), Screen.canvasPos.x + Screen.canvas.w - 16 - 5 + 5);
roll = Math.floorMod(roll - (int)(Time.deltaTime * 0.1),60); roll = Math.floorMod(roll - (int)(speed * Time.deltaTime * 0.1),60);
} }
if(Keyboard.getKeyState(SDL_SCANCODE_W) | Keyboard.getKeyState(SDL_SCANCODE_UP)){ if(Keyboard.getKeyState(SDL_SCANCODE_W) | Keyboard.getKeyState(SDL_SCANCODE_UP)){
position.y = Math.max(position.y - (int)(speed * Time.deltaTime * 0.1), Screen.canvasPos.y + 6); position.y = Math.max(position.y - (int)(speed * Time.deltaTime * 0.1), Screen.canvasPos.y + 6);
@ -217,5 +211,24 @@ public class Player extends Character {
shooterPos.y = Math.min(shooterPos.y + (int)(speed * Time.deltaTime * 0.1), Screen.canvasPos.y + Screen.canvas.h - 32 - 8); shooterPos.y = Math.min(shooterPos.y + (int)(speed * Time.deltaTime * 0.1), Screen.canvasPos.y + Screen.canvas.h - 32 - 8);
propellerPos.y = Math.min(propellerPos.y + (int)(speed * Time.deltaTime * 0.1), Screen.canvasPos.y + Screen.canvas.h - 8); propellerPos.y = Math.min(propellerPos.y + (int)(speed * Time.deltaTime * 0.1), Screen.canvasPos.y + Screen.canvas.h - 8);
} }
if(Keyboard.getKeyState(SDL_SCANCODE_SPACE) && cooldown == 0 && !Keyboard.getKeyState(SDL_SCANCODE_LSHIFT)){
Game.playerBullets.add(new PlayerBullet(position, -2, 0));
Game.playerBullets.add(new PlayerBullet(position, 4, 0));
Game.playerBullets.add(new PlayerBullet(position, 10, 0));
Game.playerBullets.add(new PlayerBullet(position, 17, 0));
Game.playerBullets.add(new PlayerBullet(position, 23, 0));
Game.playerBullets.add(new PlayerBullet(position, 29, 0));
cooldown = 1;
}
if(Keyboard.getKeyState(SDL_SCANCODE_SPACE) && cooldown == 0 && Keyboard.getKeyState(SDL_SCANCODE_LSHIFT)){
Game.playerBullets.add(new PlayerBullet(position, 2, 9));
Game.playerBullets.add(new PlayerBullet(position, 6, 6));
Game.playerBullets.add(new PlayerBullet(position, 10, 0));
Game.playerBullets.add(new PlayerBullet(position, 17, 0));
Game.playerBullets.add(new PlayerBullet(position, 21, 6));
Game.playerBullets.add(new PlayerBullet(position, 25, 9));
cooldown = 1;
}
cooldown = Math.max(cooldown -1, 0);
} }
} }

View File

@ -18,22 +18,26 @@
package com.fmudanyali.scenes; package com.fmudanyali.scenes;
import com.fmudanyali.FileLoader; import com.fmudanyali.FileLoader;
import com.fmudanyali.Render;
import com.fmudanyali.Main; import com.fmudanyali.Main;
import com.fmudanyali.Screen; import com.fmudanyali.Screen;
import com.fmudanyali.characters.Player; import com.fmudanyali.characters.Player;
import com.fmudanyali.bullets.PlayerBullet;
import static com.fmudanyali.Audio.*; import static com.fmudanyali.Audio.*;
import static org.libsdl.api.event.SdlEvents.*; import static org.libsdl.api.event.SdlEvents.*;
import static com.fmudanyali.Render.*; import static com.fmudanyali.Render.*;
import static org.libsdl.api.keycode.SDL_Keycode.*; import static org.libsdl.api.keycode.SDL_Keycode.*;
import static org.libsdl.api.render.SdlRender.*; import static org.libsdl.api.render.SdlRender.*;
import static org.libsdl.api.surface.SdlSurface.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Game extends Scene { public class Game extends Scene {
public static boolean escPressed = false; public static boolean escPressed = false;
int kek = 0; int kek = 0;
private Player player = new Player(); private Player player = new Player();
public static List<PlayerBullet> playerBullets = new ArrayList<>();
private static Thread thread; private static Thread thread;
private static Runnable runnable = new Runnable() { private static Runnable runnable = new Runnable() {
public void run(){ public void run(){
@ -101,6 +105,17 @@ public class Game extends Scene {
SDL_RenderCopy(renderer, player.texture, player.shipFrame, player.position); SDL_RenderCopy(renderer, player.texture, player.shipFrame, player.position);
SDL_RenderCopy(renderer, player.propeller, player.propellerFrame, player.propellerPos); SDL_RenderCopy(renderer, player.propeller, player.propellerFrame, player.propellerPos);
SDL_RenderCopy(renderer, player.shooter, player.shooterFrame, player.shooterPos); SDL_RenderCopy(renderer, player.shooter, player.shooterFrame, player.shooterPos);
for(Iterator<PlayerBullet> bulletIterator = playerBullets.iterator(); bulletIterator.hasNext();){
PlayerBullet b = bulletIterator.next();
b.fly();
if(b.position.y < 30){
bulletIterator.remove();
} else {
SDL_RenderCopy(renderer, PlayerBullet.texture, null, b.position);
}
}
SDL_RenderPresent(renderer); SDL_RenderPresent(renderer);
player.shiftFrame(); player.shiftFrame();
} }