Compare commits
No commits in common. "910597e92dc66a5bbcf5a4377d75ff662834ff5c" and "d159a88ca44ffb2e6c7123fb5336dac78f07a16e" have entirely different histories.
910597e92d
...
d159a88ca4
3 changed files with 12 additions and 39 deletions
|
|
@ -3,7 +3,6 @@
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from mudlib.commands import CommandDefinition, register
|
from mudlib.commands import CommandDefinition, register
|
||||||
from mudlib.entity import Entity
|
|
||||||
from mudlib.player import Player, players
|
from mudlib.player import Player, players
|
||||||
|
|
||||||
# World instance will be injected by the server
|
# World instance will be injected by the server
|
||||||
|
|
@ -70,7 +69,8 @@ async def move_player(player: Player, dx: int, dy: int, direction_name: str) ->
|
||||||
|
|
||||||
# Check if the target is passable
|
# Check if the target is passable
|
||||||
if not world.is_passable(target_x, target_y):
|
if not world.is_passable(target_x, target_y):
|
||||||
await player.send("You can't go that way.\r\n")
|
player.writer.write("You can't go that way.\r\n")
|
||||||
|
await player.writer.drain()
|
||||||
return
|
return
|
||||||
|
|
||||||
# Send departure message to players in the old area
|
# Send departure message to players in the old area
|
||||||
|
|
@ -94,11 +94,11 @@ async def move_player(player: Player, dx: int, dy: int, direction_name: str) ->
|
||||||
await cmd_look(player, "")
|
await cmd_look(player, "")
|
||||||
|
|
||||||
|
|
||||||
async def send_nearby_message(entity: Entity, x: int, y: int, message: str) -> None:
|
async def send_nearby_message(player: Player, x: int, y: int, message: str) -> None:
|
||||||
"""Send a message to all players near a location, excluding the entity.
|
"""Send a message to all players near a location, excluding the player.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
entity: The entity who triggered the message (excluded from receiving it)
|
player: The player who triggered the message (excluded from receiving it)
|
||||||
x: X coordinate of the location
|
x: X coordinate of the location
|
||||||
y: Y coordinate of the location
|
y: Y coordinate of the location
|
||||||
message: The message to send
|
message: The message to send
|
||||||
|
|
@ -107,7 +107,7 @@ async def send_nearby_message(entity: Entity, x: int, y: int, message: str) -> N
|
||||||
viewport_range = 10
|
viewport_range = 10
|
||||||
|
|
||||||
for other in players.values():
|
for other in players.values():
|
||||||
if other.name == entity.name:
|
if other.name == player.name:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# Check if other player is within viewport range (wrapping)
|
# Check if other player is within viewport range (wrapping)
|
||||||
|
|
@ -116,7 +116,8 @@ async def send_nearby_message(entity: Entity, x: int, y: int, message: str) -> N
|
||||||
dx_dist = min(dx_dist, world.width - dx_dist)
|
dx_dist = min(dx_dist, world.width - dx_dist)
|
||||||
dy_dist = min(dy_dist, world.height - dy_dist)
|
dy_dist = min(dy_dist, world.height - dy_dist)
|
||||||
if dx_dist <= viewport_range and dy_dist <= viewport_range:
|
if dx_dist <= viewport_range and dy_dist <= viewport_range:
|
||||||
await other.send(message)
|
other.writer.write(message)
|
||||||
|
await other.writer.drain()
|
||||||
|
|
||||||
|
|
||||||
# Define individual movement command handlers
|
# Define individual movement command handlers
|
||||||
|
|
|
||||||
|
|
@ -1,24 +0,0 @@
|
||||||
"""Base entity class for characters in the world."""
|
|
||||||
|
|
||||||
from dataclasses import dataclass
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
|
||||||
class Entity:
|
|
||||||
"""Base class for anything with position and identity in the world."""
|
|
||||||
|
|
||||||
name: str
|
|
||||||
x: int
|
|
||||||
y: int
|
|
||||||
|
|
||||||
async def send(self, message: str) -> None:
|
|
||||||
"""Send a message to this entity. Base implementation is a no-op."""
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
|
||||||
class Mob(Entity):
|
|
||||||
"""Represents a non-player character (NPC) in the world."""
|
|
||||||
|
|
||||||
description: str = ""
|
|
||||||
alive: bool = True
|
|
||||||
|
|
@ -3,13 +3,14 @@
|
||||||
from dataclasses import dataclass, field
|
from dataclasses import dataclass, field
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from mudlib.entity import Entity
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class Player(Entity):
|
class Player:
|
||||||
"""Represents a connected player."""
|
"""Represents a connected player."""
|
||||||
|
|
||||||
|
name: str
|
||||||
|
x: int # position in world
|
||||||
|
y: int
|
||||||
writer: Any # telnetlib3 TelnetWriter for sending output
|
writer: Any # telnetlib3 TelnetWriter for sending output
|
||||||
reader: Any # telnetlib3 TelnetReader for reading input
|
reader: Any # telnetlib3 TelnetReader for reading input
|
||||||
flying: bool = False
|
flying: bool = False
|
||||||
|
|
@ -20,11 +21,6 @@ class Player(Entity):
|
||||||
"""Current mode is the top of the stack."""
|
"""Current mode is the top of the stack."""
|
||||||
return self.mode_stack[-1]
|
return self.mode_stack[-1]
|
||||||
|
|
||||||
async def send(self, message: str) -> None:
|
|
||||||
"""Send a message to the player via their telnet writer."""
|
|
||||||
self.writer.write(message)
|
|
||||||
await self.writer.drain()
|
|
||||||
|
|
||||||
|
|
||||||
# Global registry of connected players
|
# Global registry of connected players
|
||||||
players: dict[str, Player] = {}
|
players: dict[str, Player] = {}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue