Skip to content

Commit

Permalink
show dragged items in realtime for all users.
Browse files Browse the repository at this point in the history
also updates cursor with dragging coordinates while dragging ( mosuemove + pointermove events dont fire during item drag.
fixes mini-game performance by spawning at most 20 pickups
  • Loading branch information
l-brendle committed Oct 22, 2023
1 parent 3fbf636 commit 6a93279
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 21 deletions.
10 changes: 5 additions & 5 deletions angular-frontend/src/app/app.component.html
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<bal-app class="has-sticky-footer" style="position: relative;" (mousemove)="mouseMoved($event)">
<bal-app class="has-sticky-footer" style="position: relative;" (pointermove)="pointerMoved($event)">
<div>
<app-dragged-item *ngFor="let draggedItem of draggedItems" style="z-index: 1200; position: absolute;"
[itemDragInfo]="draggedItem" [ngStyle]="{'top': draggedItem.posY + 'px', 'left': draggedItem.posX + 'px'}"></app-dragged-item>
</div>
<div>
<bal-text class="has-text-shadow" *ngFor="let cursor of cursors" style="z-index: 1100; position: absolute;"
[ngStyle]="{'top': cursor.posY + 'px', 'left': cursor.posX + 25 + 'px'}"
[color]="cursor.color">{{cursor.name}}</bal-text>
<bal-icon *ngFor="let cursor of cursors" name="edit" style="z-index: 1000; position: absolute;"
[ngStyle]="{'top': cursor.posY + 'px', 'left': cursor.posX + 'px'}" [color]="cursor.color"></bal-icon>

<bal-icon *ngFor="let draggedItem of draggedItems" name="edit" style="z-index: 1200; position: absolute;"
[ngStyle]="{'top': draggedItem.posY + 'px', 'left': draggedItem.posX + 'px'}"></bal-icon>

</div>
<header>
<!-- Header content -->
Expand Down
34 changes: 23 additions & 11 deletions angular-frontend/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export class AppComponent implements OnDestroy {
}
}

mouseMoved(event: MouseEvent) {
pointerMoved(event: PointerEvent) {
if(this.client.connected) {
const payload = {posX: event.pageX, posY: event.pageY};
this.client?.publish({destination: "/app/cursor", body: JSON.stringify(payload)});
Expand Down Expand Up @@ -217,19 +217,31 @@ export class AppComponent implements OnDestroy {
}

private updateDraggedItems(itemDrag: ItemDragInfo) {
console.log("Dragging received" + itemDrag.id)
const index = this.draggedItems.findIndex(item => item.id === itemDrag.id)
if(index >= 0){
if(itemDrag.finished){
this.draggedItems.splice(index, 1);
if (itemDrag.draggingPlayer != this.currentUser) {
console.log("Dragging received" + itemDrag.id)
const index = this.draggedItems.findIndex(item => item.id === itemDrag.id)
if (index >= 0) {
if (itemDrag.finished) {
this.draggedItems.splice(index, 1);
} else {
this.draggedItems[index] = itemDrag;
this.updateCursorOfDraggingUserIfNeeded(itemDrag);
}
} else {
this.draggedItems[index] = itemDrag;
}
} else {
if(!itemDrag.finished){
this.draggedItems.push(itemDrag);
if (!itemDrag.finished) {
this.draggedItems.push(itemDrag);
this.updateCursorOfDraggingUserIfNeeded(itemDrag);
}
}
}
}

private updateCursorOfDraggingUserIfNeeded(itemDragInfo : ItemDragInfo) {
let cursor = this.cursors.find(cursor => cursor.name === itemDragInfo.draggingPlayer);
if(cursor != undefined){
cursor.posY = itemDragInfo.posY;
cursor.posX = itemDragInfo.posX;
}
}

}
3 changes: 2 additions & 1 deletion angular-frontend/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ import { GameComponent } from './components/game/game.component';
import { balIconAccount, balIconSend, balIconStarShape, balIconStarFull, balIconWeb, balIconX} from '@baloise/design-system-icons'
import { ItemBaseSelectionComponent } from './components/item-base-selection/item-base-selection.component'
import {HttpClientModule} from "@angular/common/http";
import {DraggedItemComponent} from "./components/dragged-item/dragged-item.component";

@NgModule({
declarations: [AppComponent, CheckInComponent, InventoryComponent, InventoryItemComponent, ChatComponent, GameComponent, StockComponent, UserBarComponent, ItemBaseSelectionComponent],
declarations: [AppComponent, CheckInComponent, InventoryComponent, InventoryItemComponent, ChatComponent, GameComponent, StockComponent, UserBarComponent, ItemBaseSelectionComponent, DraggedItemComponent],
imports: [
BrowserModule,
HttpClientModule,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div class="dragged-item" [ngClass]="'ghost-item'">
<img [alt]="itemDragInfo" [src]="'assets/items/' + itemDragInfo.name + '.png'"/>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.ghost-item {
width:50px;
height:50px;
border-radius:9px;
margin:3px;
border-width:2px;
border-style: dashed;
border-color: gray;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {Component, Input} from '@angular/core';
import {ItemDragInfo} from "../../../model";

@Component({
selector: 'app-dragged-item',
templateUrl: './dragged-item.component.html',
styleUrls: ['./dragged-item.component.scss']
})
export class DraggedItemComponent {
@Input()
itemDragInfo!: ItemDragInfo
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export class InventoryItemComponent implements OnInit {
posX: event.pageX,
posY : event.pageY,
id:this.item.id,
draggingPlayer: this.item.userLock,
finished: finished
}
this.client?.publish({destination: "/app/item-dragging", body: JSON.stringify(itemDragInfo)
Expand Down
1 change: 1 addition & 0 deletions angular-frontend/src/model/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,5 @@ export interface ItemDragInfo {
posX: number;
posY: number;
finished : boolean;
draggingPlayer : string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public void run() throws InterruptedException {
var scoreUpdate = pickUpHandler.collectPickups(character);
character.setScore(character.getScore() + scoreUpdate);
});
} else {
quit();
}
pickUpHandler.spawnPickupsAndSendToClients();
lastFrameTime = System.currentTimeMillis();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public int collectPickups(Character character) {

public void spawnPickupsAndSendToClients() {
var currentTime = System.currentTimeMillis();
if (currentTime - lastSpawn > 3000) {
if (currentTime - lastSpawn > 3000 && pickups.size() < 20) {
lastSpawn = currentTime;
pickups.add(
new Pickup(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ public record ItemDragDTO (
Long id,
int posX,
int posY,

boolean finished){
}
boolean finished,
String draggingPlayer
){ }

0 comments on commit 6a93279

Please sign in to comment.