-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adds initial mini-game impl - spawning character for players upon joi…
…n + allows basic movement
- Loading branch information
Showing
12 changed files
with
289 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<bal-card class="mt-medium"> | ||
<bal-card-content class="is-flex fg-small"> | ||
<bal-heading level="medium">Mini-Game</bal-heading> | ||
<bal-button (click)="joinGame()"><bal-icon name="check"></bal-icon></bal-button> | ||
<bal-button (click)="leaveGame()"><bal-icon name="close"></bal-icon></bal-button> | ||
</bal-card-content> | ||
</bal-card> |
Empty file.
74 changes: 74 additions & 0 deletions
74
angular-frontend/src/app/components/game/game.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import {Component, HostListener, Input, OnDestroy, OnInit} from '@angular/core'; | ||
|
||
import { Client } from '@stomp/stompjs'; | ||
|
||
@Component({ | ||
selector: 'app-game', | ||
templateUrl: './game.component.html', | ||
styleUrls: ['./game.component.scss'] | ||
}) | ||
export class GameComponent implements OnInit, OnDestroy { | ||
|
||
@Input() client!: Client; | ||
@Input() currentUser!: String; | ||
|
||
connected : boolean = false; | ||
|
||
ngOnInit(): void { | ||
if (this.client.connected) { | ||
//this.client.subscribe("/topic/chat", payload => this.addMessage(payload)); | ||
} | ||
} | ||
|
||
ngOnDestroy(): void { | ||
if (this.client.connected) { | ||
//this.client.unsubscribe("/topic/chat") | ||
} | ||
} | ||
|
||
joinGame() { | ||
const payload = "joining_game"; | ||
this.client?.publish({ destination: "/app/game/match_join", body: JSON.stringify(payload)}); | ||
this.connected = true; | ||
} | ||
|
||
leaveGame() { | ||
const payload = "leaving_game" | ||
this.client?.publish({ destination: "/app/game/match_leave", body: JSON.stringify(payload)}); | ||
this.connected = false; | ||
} | ||
|
||
@HostListener('window:keyup', ['$event']) | ||
keyUpEvent(event: KeyboardEvent) { | ||
if(this.client.connected){ | ||
const keyCode = event.code | ||
console.log("KeyEvent: " + keyCode); | ||
|
||
if(keyCode === "ArrowLeft" | ||
|| keyCode === "ArrowRight" | ||
|| keyCode === "ArrowDown" | ||
|| keyCode === "ArrowUp") { | ||
const payload = {keyCode : keyCode, pressed : false} | ||
this.client?.publish({destination: "/app/game/character_control", body: JSON.stringify(payload)}) | ||
} | ||
} | ||
} | ||
|
||
@HostListener('window:keydown', ['$event']) | ||
keyDownEvent(event: KeyboardEvent) { | ||
if(this.client.connected){ | ||
const keyCode = event.code | ||
console.log("KeyEvent: " + keyCode); | ||
|
||
if(keyCode === "ArrowLeft" | ||
|| keyCode === "ArrowRight" | ||
|| keyCode === "ArrowDown" | ||
|| keyCode === "ArrowUp") { | ||
const payload = {keyCode : keyCode, pressed : true} | ||
this.client?.publish({destination: "/app/game/character_control", body: JSON.stringify(payload)}) | ||
} | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
spring-backend/src/main/java/com/baloise/collab/springbackend/game/Character.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package com.baloise.collab.springbackend.game; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.extern.java.Log; | ||
|
||
import java.util.*; | ||
|
||
@Data | ||
@Builder | ||
@Log | ||
public class Character { | ||
|
||
private String name; | ||
private String color; | ||
private long posX; | ||
private long posY; | ||
private Set<String> pressedKeys; | ||
|
||
private final float speed = 0.1f; | ||
|
||
public void addToPressedKeys(String keyCode) { | ||
pressedKeys.add(keyCode); | ||
} | ||
|
||
public void removeFromPressedKeys(String keyCode) { | ||
pressedKeys.remove(keyCode); | ||
} | ||
|
||
public int getPosXRounded() { | ||
return Math.round(posX); | ||
} | ||
|
||
public int getPosYRounded() { | ||
return Math.round(posY); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o){ | ||
return o.getClass() == this.getClass() | ||
&& Objects.equals(((Character) o).name, this.name); | ||
} | ||
|
||
@AllArgsConstructor | ||
private static class MovementDirection { | ||
|
||
public int dirX = 0; | ||
public int dirY = 0; | ||
|
||
public static MovementDirection add(MovementDirection a, MovementDirection b) { | ||
return new MovementDirection(a.dirX + b.dirX, a.dirY + b.dirY); | ||
} | ||
} | ||
|
||
public void move(Long frameTime) { | ||
MovementDirection movementDirection; | ||
if (pressedKeys != null && !pressedKeys.isEmpty()) { | ||
var directions = pressedKeys.stream().map(this::getMovementDirforKey); | ||
movementDirection = directions.reduce(new MovementDirection(0, 0), MovementDirection::add); | ||
} else { | ||
movementDirection = new MovementDirection(0, 0); | ||
} | ||
this.posX = Math.round(posX + movementDirection.dirX * frameTime * speed); | ||
this.posY = Math.round(posY + movementDirection.dirY * frameTime * speed); | ||
} | ||
|
||
private MovementDirection getMovementDirforKey(String key) { | ||
return switch (key) { | ||
case "ArrowLeft" -> new MovementDirection(-1, 0); | ||
case "ArrowRight" -> new MovementDirection(1, 0); | ||
case "ArrowDown" -> new MovementDirection(0, 1); | ||
case "ArrowUp" -> new MovementDirection(0, -1); | ||
default -> new MovementDirection(0, 0); | ||
}; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
spring-backend/src/main/java/com/baloise/collab/springbackend/game/GameLoopExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.baloise.collab.springbackend.game; | ||
|
||
|
||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.scheduling.annotation.Async; | ||
import org.springframework.stereotype.Service; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
public class GameLoopExecutor { | ||
|
||
private final CharacterController characterController; | ||
private Long lastFrameTime; | ||
|
||
@Getter | ||
private boolean running; | ||
|
||
@Async | ||
public void run() throws InterruptedException { | ||
running = true; | ||
while (running) { | ||
var characters = characterController.getActiveCharacters(); | ||
var time = System.currentTimeMillis(); | ||
if(!characters.isEmpty()){ | ||
characters.forEach(character -> characterController.moveCharacter(character, time-lastFrameTime)); | ||
} | ||
lastFrameTime = System.currentTimeMillis(); | ||
Thread.sleep(50); | ||
} | ||
} | ||
|
||
@Async | ||
public void quit(){ | ||
running = false; | ||
lastFrameTime = 0L; | ||
} | ||
|
||
|
||
|
||
} |
Oops, something went wrong.