Hold item !
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
import { System } from "$/common/ecs/mod.ts";
|
||||
import { point_inside_rec } from "$/common/utils.ts";
|
||||
import { get_sprite_region, point_inside_rec } from "$/common/utils.ts";
|
||||
import { SLOT_SIZE } from "$/common/constants.ts";
|
||||
import { Container, PlayerInventory } from "$/client/components/inventory.ts";
|
||||
import { InputManager } from "$/client/input_manager.ts";
|
||||
import { ClientWorld } from "$/client/client_world.ts";
|
||||
import { Position } from "../../common/components/position.ts";
|
||||
import { Sprite } from "../components/sprite.ts";
|
||||
|
||||
export class InventorySystem extends System {
|
||||
update(world: ClientWorld, _delta: number): void {
|
||||
@@ -15,41 +17,46 @@ export class InventorySystem extends System {
|
||||
const player_inventory = entity.get(PlayerInventory);
|
||||
|
||||
if (player_inventory) {
|
||||
player_inventory.hovering_slot = -1;
|
||||
const container = player_inventory.container;
|
||||
if (player_inventory.is_open) {
|
||||
for (const [index, slot] of player_inventory.layout.slots.entries()) {
|
||||
const mouse = InputManager.get_mouse_position();
|
||||
const slot_x = slot.x + player_inventory.layout.offset_x;
|
||||
const slot_y = slot.y + player_inventory.layout.offset_y;
|
||||
const hovering = point_inside_rec(mouse.x, mouse.y, slot_x, slot_y, SLOT_SIZE, SLOT_SIZE);
|
||||
if (hovering) {
|
||||
player_inventory.hovering_slot = index;
|
||||
this.handle_interaction(player_inventory);
|
||||
this.handle_hotbar(world, player_inventory);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (InputManager.is_mouse_pressed(0)) {
|
||||
InputManager.consume_mouse(0);
|
||||
this.handle_left_click(player_inventory, container, index);
|
||||
return;
|
||||
}
|
||||
handle_interaction(player_inventory: PlayerInventory) {
|
||||
player_inventory.hovering_slot = -1;
|
||||
const container = player_inventory.container;
|
||||
if (player_inventory.is_open) {
|
||||
for (const [index, slot] of player_inventory.layout.slots.entries()) {
|
||||
const mouse = InputManager.get_mouse_position();
|
||||
const slot_x = slot.x + player_inventory.layout.offset_x;
|
||||
const slot_y = slot.y + player_inventory.layout.offset_y;
|
||||
const hovering = point_inside_rec(mouse.x, mouse.y, slot_x, slot_y, SLOT_SIZE, SLOT_SIZE);
|
||||
if (hovering) {
|
||||
player_inventory.hovering_slot = index;
|
||||
|
||||
if (InputManager.is_mouse_pressed(2)) {
|
||||
InputManager.consume_mouse(2);
|
||||
this.handle_right_click(player_inventory, container, index);
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (InputManager.is_mouse_pressed(0)) {
|
||||
InputManager.consume_mouse(0);
|
||||
this.handle_left_click(player_inventory, container, index);
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
if (player_inventory.holding_item) {
|
||||
container.add_item(player_inventory.holding_item);
|
||||
player_inventory.holding_item = undefined;
|
||||
}
|
||||
}
|
||||
|
||||
if (player_inventory.holding_item?.amount === 0) {
|
||||
player_inventory.holding_item = undefined;
|
||||
if (InputManager.is_mouse_pressed(2)) {
|
||||
InputManager.consume_mouse(2);
|
||||
this.handle_right_click(player_inventory, container, index);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (player_inventory.holding_item) {
|
||||
container.add_item(player_inventory.holding_item);
|
||||
player_inventory.holding_item = undefined;
|
||||
}
|
||||
}
|
||||
|
||||
if (player_inventory.holding_item?.amount === 0) {
|
||||
player_inventory.holding_item = undefined;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -142,4 +149,34 @@ export class InventorySystem extends System {
|
||||
slot.set_item(undefined);
|
||||
}
|
||||
}
|
||||
|
||||
handle_hotbar(world: ClientWorld, player_inventory: PlayerInventory) {
|
||||
const [player, player_hand] = world.get_tag("player")!;
|
||||
|
||||
const player_position = player.get(Position);
|
||||
const hand_position = player_hand.get(Position);
|
||||
const hand_sprite = player_hand.get(Sprite);
|
||||
|
||||
if (!player_position || !hand_position || !hand_sprite) {
|
||||
return;
|
||||
}
|
||||
|
||||
// sync position
|
||||
hand_position.x = player_position.x;
|
||||
hand_position.y = player_position.y;
|
||||
|
||||
const maybe_item = player_inventory.container.get_item(player_inventory.hotbar_selected);
|
||||
if (maybe_item) {
|
||||
const region = get_sprite_region(maybe_item.type_id);
|
||||
hand_sprite.source_x = region.x * 16;
|
||||
hand_sprite.source_y = region.y * 16;
|
||||
hand_sprite.source_width = 16;
|
||||
hand_sprite.source_height = 16;
|
||||
hand_sprite.width = 32;
|
||||
hand_sprite.height = 32;
|
||||
} else {
|
||||
hand_sprite.width = 0;
|
||||
hand_sprite.height = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ export class PlayerControlsSystem extends System {
|
||||
|
||||
const velocity = this.player.get(Velocity)!;
|
||||
const controls = this.player.get(PlayerControls)!;
|
||||
const player_inventory = this.player.get(PlayerInventory)!;
|
||||
|
||||
if (InputManager.is_key_down(controls.move_left)) {
|
||||
velocity.vx = -controls.move_speed;
|
||||
@@ -72,6 +73,13 @@ export class PlayerControlsSystem extends System {
|
||||
camera.x = Math.round(position.x);
|
||||
camera.y = Math.round(position.y);
|
||||
|
||||
const scroll = InputManager.get_wheel_delta();
|
||||
if (scroll > 0) {
|
||||
player_inventory.hotbar_selected = Math.min(8, player_inventory.hotbar_selected + 1);
|
||||
} else if (scroll < 0) {
|
||||
player_inventory.hotbar_selected = Math.max(0, player_inventory.hotbar_selected - 1);
|
||||
}
|
||||
|
||||
// --- APPLY BOUNDS ---
|
||||
/*if (camera.bounds) {
|
||||
camera.x = Math.max(
|
||||
|
||||
Reference in New Issue
Block a user