-
Notifications
You must be signed in to change notification settings - Fork 54
feat: add vanillajs + phaser example #249
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add vanillajs + phaser example #249
Conversation
Warning Rate limit exceeded@MartianGreed has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 11 minutes and 4 seconds before requesting another review. How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. ## Walkthrough
This update introduces a vanilla Phaser.js example, featuring a character navigating an infinite map. It includes essential configuration files and scripts, enhancing modularity and maintainability while integrating the Dojo engine effectively. This setup streamlines the development process and lays the groundwork for an engaging interactive experience.
## Changes
| Files | Change Summary |
|-------|----------------|
| `examples/vanilla/phaser/.gitignore` | New file to exclude unnecessary files from version control. |
| `examples/vanilla/phaser/dojoConfig.ts`, `examples/vanilla/phaser/src/dojo/*.ts`, `examples/vanilla/phaser/src/models.ts`, `examples/vanilla/phaser/src/utils.ts` | New files defining Dojo configuration, components, models, and utility functions for game logic. |
| `examples/vanilla/phaser/index.html` | New foundational HTML file linking Dojo and Phaser. |
| `examples/vanilla/phaser/package.json`, `examples/vanilla/phaser/tsconfig.json`, `examples/vanilla/phaser/vite.config.ts` | New configuration files for project metadata, TypeScript options, and build setup. |
| `examples/vanilla/phaser/src/entities.ts`, `examples/vanilla/phaser/src/scenes/scene-main.ts`, `examples/vanilla/phaser/src/main.ts` | New files introducing classes for managing game entities, scene management, and game initialization. |
| `package.json` | Addition of a new build script for the vanilla Phaser example. |
| `pnpm-workspace.yaml` | Updated to include new paths for workspace packages. |
| `scripts/build-examples.sh` | Modified script to include building the vanilla Phaser application. |
## Sequence Diagram(s)
```mermaid
sequenceDiagram
participant User
participant SceneMain as Scene
participant GameConfig as Game
participant DojoConfig as Config
participant Models as Model
User->>Scene: Start Game
Scene->>Config: Load Configuration
Config->>Scene: Return Config
Scene->>Game: Initialize Game with Config
Game->>Model: Setup Models
Model->>Scene: Return Models
Scene-->>User: Game Ready Assessment against linked issues
Poem
--> Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (invoked as PR comments)
Additionally, you can add CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 13
Outside diff range, codebase verification and nitpick comments (1)
examples/vanilla/phaser/src/dojo/setup.ts (1)
16-17
: Unused Type ExportsThe types
SetupResult
andIDojo
are defined but not used anywhere else in the codebase. Consider removing these unused types or ensure they are utilized appropriately.
SetupResult
defined atexamples/vanilla/phaser/src/dojo/setup.ts
IDojo
defined atexamples/vanilla/phaser/src/dojo/setup.ts
Analysis chain
Ensure type exports are correctly defined and used.
Verify that the exported types
SetupResult
andIDojo
are correctly defined and used in the file.Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify that the exported types are correctly defined and used in the file. # Test: Search for the usage of each exported type. Expect: Each type should be used at least once. rg --type typescript -A 5 'export type' examples/vanilla/phaser/src/dojo/setup.tsLength of output: 118
Script:
#!/bin/bash # Locate the file and search for type exports # Find the file file_path=$(fd setup.ts | grep 'examples/vanilla/phaser/src/dojo/setup.ts') # Search for type exports in the located file if [ -n "$file_path" ]; then rg 'export type' "$file_path" else echo "File not found" fiLength of output: 350
Script:
#!/bin/bash # Search for the usage of the exported types `SetupResult` and `IDojo` within the codebase # Search for `SetupResult` usage rg 'SetupResult' examples/vanilla/phaser/src/dojo/ # Search for `IDojo` usage rg 'IDojo' examples/vanilla/phaser/src/dojo/Length of output: 295
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files ignored due to path filters (5)
examples/vanilla/phaser/assets/sprGrass.png
is excluded by!**/*.png
examples/vanilla/phaser/assets/sprSand.png
is excluded by!**/*.png
examples/vanilla/phaser/assets/sprWater.png
is excluded by!**/*.png
examples/vanilla/phaser/public/favicon.ico
is excluded by!**/*.ico
pnpm-lock.yaml
is excluded by!**/pnpm-lock.yaml
Files selected for processing (20)
- examples/vanilla/phaser/.gitignore (1 hunks)
- examples/vanilla/phaser/dojoConfig.ts (1 hunks)
- examples/vanilla/phaser/index.html (1 hunks)
- examples/vanilla/phaser/package.json (1 hunks)
- examples/vanilla/phaser/src/dojo/createClientComponent.ts (1 hunks)
- examples/vanilla/phaser/src/dojo/defineContractComponents.ts (1 hunks)
- examples/vanilla/phaser/src/dojo/defineContractSystems.ts (1 hunks)
- examples/vanilla/phaser/src/dojo/models.ts (1 hunks)
- examples/vanilla/phaser/src/dojo/setup.ts (1 hunks)
- examples/vanilla/phaser/src/dojo/systems.ts (1 hunks)
- examples/vanilla/phaser/src/dojo/utils.ts (1 hunks)
- examples/vanilla/phaser/src/dojo/world.ts (1 hunks)
- examples/vanilla/phaser/src/entities.ts (1 hunks)
- examples/vanilla/phaser/src/main.ts (1 hunks)
- examples/vanilla/phaser/src/scenes/scene-main.ts (1 hunks)
- examples/vanilla/phaser/tsconfig.json (1 hunks)
- examples/vanilla/phaser/vite.config.ts (1 hunks)
- package.json (1 hunks)
- pnpm-workspace.yaml (1 hunks)
- scripts/build-examples.sh (1 hunks)
Files skipped from review due to trivial changes (8)
- examples/vanilla/phaser/.gitignore
- examples/vanilla/phaser/index.html
- examples/vanilla/phaser/package.json
- examples/vanilla/phaser/src/dojo/world.ts
- examples/vanilla/phaser/tsconfig.json
- examples/vanilla/phaser/vite.config.ts
- package.json
- pnpm-workspace.yaml
Additional context used
Biome
examples/vanilla/phaser/src/dojo/utils.ts
[error] 13-13: This is an unexpected use of the debugger statement.
Unsafe fix: Remove debugger statement
(lint/suspicious/noDebugger)
Additional comments not posted (29)
examples/vanilla/phaser/dojoConfig.ts (3)
1-2
: Imports are appropriate and necessary.The imported
manifest
andcreateDojoConfig
function are required for creating the configuration.
4-6
: Configuration creation is correct.The
createDojoConfig
function is used correctly to create the configuration with the providedmanifest
.
7-7
: Export statements are correct.The configuration and its type are correctly exported.
scripts/build-examples.sh (1)
11-13
: Build commands are correctly added and reorganized.The build command for the vanilla Phaser example is correctly added, and the Vue app build command is appropriately moved to follow the new command.
examples/vanilla/phaser/src/dojo/models.ts (3)
1-1
: Import statement is appropriate and necessary.The imported
ContractComponents
is required for the function's parameter.
5-9
: Function definition is correct.The function
models
is correctly defined to takecontractModels
as a parameter.
10-15
: Return statement is correct.The function correctly returns an object with the models, spreading the
contractModels
.examples/vanilla/phaser/src/dojo/createClientComponent.ts (3)
1-2
: Imports look good.The import statements are necessary and correctly specified.
4-5
: Type definition looks good.The type definition for
ClientComponents
is clear and correctly specified.
6-16
: Function definition looks good.The function
createClientComponents
is well-defined and correctly uses theoverridableComponent
function to override components.examples/vanilla/phaser/src/dojo/utils.ts (1)
1-6
: Enum definition looks good.The enum
Direction
is clear and correctly specified.examples/vanilla/phaser/src/main.ts (3)
1-4
: Imports look good.The import statements are necessary and correctly specified.
6-25
: Phaser game configuration looks good.The configuration object is well-defined and correctly specifies the necessary settings for the Phaser game.
27-36
: Game setup and initialization looks good.The setup and initialization process is well-defined and correctly handles the game initialization and error handling.
examples/vanilla/phaser/src/dojo/defineContractSystems.ts (4)
1-4
: LGTM! Imports are correct and necessary.The imported modules are used appropriately in the file.
5-10
: LGTM!NAMESPACE
constant andMoveProps
interface are correct.The
NAMESPACE
constant andMoveProps
interface are defined appropriately.
12-15
: LGTM!handleError
function handles errors correctly.The
handleError
function logs the error and rethrows it, which is appropriate for error handling.
17-55
: LGTM!IWorld
type definition andsetupWorld
function are correct.The
IWorld
type definition andsetupWorld
function are implemented correctly and securely.However, ensure that all function calls to
setupWorld
match the expected usage.examples/vanilla/phaser/src/entities.ts (3)
1-3
: LGTM! Imports are correct and necessary.The imported modules are used appropriately in the file.
5-64
: LGTM!Chunk
class is implemented correctly and efficiently.The
Chunk
class includes necessary properties and methods for loading and unloading tiles, and it is implemented efficiently.
67-74
: LGTM!Tile
class is implemented correctly and efficiently.The
Tile
class extendsGameObjects.Sprite
and includes necessary properties for the scene and coordinates.examples/vanilla/phaser/src/dojo/defineContractComponents.ts (2)
1-3
: LGTM! Imports are correct and necessary.The imported modules are used appropriately in the file.
5-71
: LGTM!ContractComponents
type definition anddefineContractComponents
function are correct.The
ContractComponents
type definition anddefineContractComponents
function are implemented correctly and securely.However, ensure that all function calls to
defineContractComponents
match the expected usage.examples/vanilla/phaser/src/dojo/setup.ts (2)
83-94
: Ensure the return statement includes all necessary components.Verify that the return statement includes all necessary components and configurations.
Verification successful
The return statement includes all necessary components.
The return statement in the
setup.ts
file includes all the required components and configurations, as verified by their usage in the codebase.
client
clientModels
contractComponents
systemCalls
config
world
burnerManager
rpcProvider
sync
account
Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify that the return statement includes all necessary components and configurations. # Test: Search for the usage of the return values. Expect: Each return value should be used at least once. rg --type typescript -A 5 'return {' examples/vanilla/phaser/src/dojo/setup.tsLength of output: 160
Script:
#!/bin/bash # Description: Verify that the return statement includes all necessary components and configurations. # Test: Search for the usage of the return values. Expect: Each return value should be used at least once. rg 'return {' -A 10 examples/vanilla/phaser/src/dojo/setup.tsLength of output: 143
Script:
#!/bin/bash # Description: Verify that the return statement includes all necessary components and configurations. # Test: Search for the return statement and include context lines to verify the return values. grep -A 10 'return {' examples/vanilla/phaser/src/dojo/setup.tsLength of output: 302
Script:
#!/bin/bash # Description: Verify that each return value is used in the codebase. # List of return values to verify values=("client" "clientModels" "contractComponents" "systemCalls" "config" "world" "burnerManager" "rpcProvider" "sync" "account") # Loop through each value and search for its usage in the codebase for value in "${values[@]}"; do echo "Searching for usage of: $value" rg "$value" examples/vanilla/phaser/src/dojo/ doneLength of output: 9904
1-15
: Ensure all imports are necessary and correctly used.Verify that all imported modules are necessary and correctly utilized in the file to avoid any unused imports.
Verification successful
Let's modify the script to search for the usage of each imported module in the file without specifying the file type.
All imports are necessary and correctly used.
The verification confirms that each imported module in
examples/vanilla/phaser/src/dojo/setup.ts
is utilized in the file.
getSyncEntities
is used in thesync
constant.torii
is used to create thetoriiClient
.models
is used to getclientModels
.systems
is used to getactions
.defineContractComponents
is used to definecontractModels
.world
is used indefineContractComponents
.Config
is used as a parameter in thesetup
function.setupWorld
is used to set up theclient
.DojoProvider
is used to create a new provider.BurnerManager
is used to create a new manager.Account
,RpcProvider
, andwallet
are used in therpcProvider
andburnerManager
.createClientComponents
is used to createcontractModels
.Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify that all imported modules are necessary and correctly utilized in the file. # Test: Search for the usage of each imported module. Expect: Each module should be used at least once. rg --type typescript -A 5 'import' examples/vanilla/phaser/src/dojo/setup.tsLength of output: 111
Script:
#!/bin/bash # Description: Verify that all imported modules are necessary and correctly utilized in the file. # Test: Search for the usage of each imported module. Expect: Each module should be used at least once. rg 'getSyncEntities|torii|models|systems|defineContractComponents|world|Config|setupWorld|DojoProvider|BurnerManager|Account|RpcProvider|wallet|createClientComponents' examples/vanilla/phaser/src/dojo/setup.tsLength of output: 1661
examples/vanilla/phaser/src/dojo/systems.ts (1)
16-24
: Ensure proper typing for function parameters.The
systems
function parameters have complex types that should be clearly defined. Ensure that the typesClientComponents
,ContractComponents
, andIWorld
are correctly defined and imported.Verification successful
Ensure proper typing for function parameters.
The
systems
function parameters have complex types that are correctly defined and imported in the codebase. The typesClientComponents
,ContractComponents
, andIWorld
are properly defined and utilized.
IWorld
: Defined inpackages/core/src/types/index.ts
andexamples/react/react-pwa-app/src/dojo/generated/generated.ts
.ContractComponents
: Defined inexamples/react/react-pwa-app/src/dojo/generated/contractComponents.ts
.ClientComponents
: Defined inexamples/react/react-pwa-app/src/dojo/createClientComponents.ts
.Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify the definitions of ClientComponents, ContractComponents, and IWorld. # Test: Search for the type definitions. Expect: Definitions of the types. rg --type typescript 'ClientComponents|ContractComponents|IWorld'Length of output: 102
Script:
#!/bin/bash # Description: Verify the definitions of ClientComponents, ContractComponents, and IWorld. # Test: Search for the type definitions. Expect: Definitions of the types. rg 'ClientComponents|ContractComponents|IWorld'Length of output: 14186
examples/vanilla/phaser/src/scenes/scene-main.ts (3)
30-37
: LGTM!The asset loading logic in the
preload
method looks good.
169-176
: LGTM!The input checking logic in the
checkInputs
method looks good.
77-85
: Optimize chunk retrieval logic.The
getChunk
method can be optimized by using aMap
forthis.chunks
to improve lookup performance.
export function updatePositionWithDirection( | ||
direction: Direction, | ||
value: { vec: { x: number; y: number } } | ||
) { | ||
console.log(value); | ||
debugger; | ||
switch (direction) { | ||
case Direction.Left: | ||
value.vec.x--; | ||
break; | ||
case Direction.Right: | ||
value.vec.x++; | ||
break; | ||
case Direction.Up: | ||
value.vec.y--; | ||
break; | ||
case Direction.Down: | ||
value.vec.y++; | ||
break; | ||
default: | ||
throw new Error("Invalid direction provided"); | ||
} | ||
return value; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove console log and debugger statement.
The console log and debugger statement should be removed for production code.
- console.log(value);
- debugger;
The rest of the function is well-defined and correctly handles all possible directions.
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
export function updatePositionWithDirection( | |
direction: Direction, | |
value: { vec: { x: number; y: number } } | |
) { | |
console.log(value); | |
debugger; | |
switch (direction) { | |
case Direction.Left: | |
value.vec.x--; | |
break; | |
case Direction.Right: | |
value.vec.x++; | |
break; | |
case Direction.Up: | |
value.vec.y--; | |
break; | |
case Direction.Down: | |
value.vec.y++; | |
break; | |
default: | |
throw new Error("Invalid direction provided"); | |
} | |
return value; | |
} | |
export function updatePositionWithDirection( | |
direction: Direction, | |
value: { vec: { x: number; y: number } } | |
) { | |
switch (direction) { | |
case Direction.Left: | |
value.vec.x--; | |
break; | |
case Direction.Right: | |
value.vec.x++; | |
break; | |
case Direction.Up: | |
value.vec.y--; | |
break; | |
case Direction.Down: | |
value.vec.y++; | |
break; | |
default: | |
throw new Error("Invalid direction provided"); | |
} | |
return value; | |
} |
Tools
Biome
[error] 13-13: This is an unexpected use of the debugger statement.
Unsafe fix: Remove debugger statement
(lint/suspicious/noDebugger)
const client = await setupWorld( | ||
new DojoProvider(config.manifest, config.rpcUrl), | ||
config | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ensure proper error handling for setupWorld
.
The setupWorld
call should have error handling to manage potential failures.
- const client = await setupWorld(
- new DojoProvider(config.manifest, config.rpcUrl),
- config
- );
+ let client;
+ try {
+ client = await setupWorld(
+ new DojoProvider(config.manifest, config.rpcUrl),
+ config
+ );
+ } catch (e) {
+ console.error('Failed to setup world:', e);
+ throw e;
+ }
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
const client = await setupWorld( | |
new DojoProvider(config.manifest, config.rpcUrl), | |
config | |
); | |
let client; | |
try { | |
client = await setupWorld( | |
new DojoProvider(config.manifest, config.rpcUrl), | |
config | |
); | |
} catch (e) { | |
console.error('Failed to setup world:', e); | |
throw e; | |
} |
// fetch all existing entities from torii | ||
const sync = await getSyncEntities( | ||
toriiClient, | ||
contractModels as any, | ||
[], | ||
1000 | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ensure proper error handling for getSyncEntities
.
The getSyncEntities
call should have error handling to manage potential failures.
- const sync = await getSyncEntities(
- toriiClient,
- contractModels as any,
- [],
- 1000
- );
+ let sync;
+ try {
+ sync = await getSyncEntities(
+ toriiClient,
+ contractModels as any,
+ [],
+ 1000
+ );
+ } catch (e) {
+ console.error('Failed to sync entities:', e);
+ throw e;
+ }
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
// fetch all existing entities from torii | |
const sync = await getSyncEntities( | |
toriiClient, | |
contractModels as any, | |
[], | |
1000 | |
); | |
// fetch all existing entities from torii | |
let sync; | |
try { | |
sync = await getSyncEntities( | |
toriiClient, | |
contractModels as any, | |
[], | |
1000 | |
); | |
} catch (e) { | |
console.error('Failed to sync entities:', e); | |
throw e; | |
} |
// create contract components | ||
const contractModels = createClientComponents({ | ||
contractComponents: defineContractComponents(world), | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ensure proper error handling for createClientComponents
.
The createClientComponents
call should have error handling to manage potential failures.
- const contractModels = createClientComponents({
+ let contractModels;
+ try {
+ contractModels = createClientComponents({
+ contractComponents: defineContractComponents(world),
+ });
+ } catch (e) {
+ console.error('Failed to create client components:', e);
+ throw e;
+ }
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
// create contract components | |
const contractModels = createClientComponents({ | |
contractComponents: defineContractComponents(world), | |
}); | |
let contractModels; | |
try { | |
contractModels = createClientComponents({ | |
contractComponents: defineContractComponents(world), | |
}); | |
} catch (e) { | |
console.error('Failed to create client components:', e); | |
throw e; | |
} |
export async function setup({ ...config }: Config) { | ||
// torii client | ||
const toriiClient = await torii.createClient({ | ||
rpcUrl: config.rpcUrl, | ||
toriiUrl: config.toriiUrl, | ||
relayUrl: "", | ||
worldAddress: config.manifest.world.address || "", | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ensure proper error handling for torii.createClient
.
The torii.createClient
call should have error handling to manage potential failures.
- const toriiClient = await torii.createClient({
+ let toriiClient;
+ try {
+ toriiClient = await torii.createClient({
+ rpcUrl: config.rpcUrl,
+ toriiUrl: config.toriiUrl,
+ relayUrl: "",
+ worldAddress: config.manifest.world.address || "",
+ });
+ } catch (e) {
+ console.error('Failed to create torii client:', e);
+ throw e;
+ }
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
export async function setup({ ...config }: Config) { | |
// torii client | |
const toriiClient = await torii.createClient({ | |
rpcUrl: config.rpcUrl, | |
toriiUrl: config.toriiUrl, | |
relayUrl: "", | |
worldAddress: config.manifest.world.address || "", | |
}); | |
export async function setup({ ...config }: Config) { | |
// torii client | |
let toriiClient; | |
try { | |
toriiClient = await torii.createClient({ | |
rpcUrl: config.rpcUrl, | |
toriiUrl: config.toriiUrl, | |
relayUrl: "", | |
worldAddress: config.manifest.world.address || "", | |
}); | |
} catch (e) { | |
console.error('Failed to create torii client:', e); | |
throw e; | |
} |
const spawn = async (account: AccountInterface) => { | ||
const entityId = getEntityIdFromKeys([ | ||
BigInt(account.address), | ||
]) as Entity; | ||
|
||
const positionId = uuid(); | ||
Position.addOverride(positionId, { | ||
entity: entityId, | ||
value: { player: BigInt(entityId), vec: { x: 10, y: 10 } }, | ||
}); | ||
|
||
const movesId = uuid(); | ||
Moves.addOverride(movesId, { | ||
entity: entityId, | ||
value: { | ||
player: BigInt(entityId), | ||
remaining: 100, | ||
last_direction: 0, | ||
}, | ||
}); | ||
|
||
try { | ||
const { transaction_hash } = await client.actions.spawn({ | ||
account, | ||
}); | ||
// setComponentsFromEvents( | ||
// contractComponents, | ||
// getEvents( | ||
// await account.waitForTransaction(transaction_hash, { | ||
// retryInterval: 100, | ||
// }) | ||
// ) | ||
// ); | ||
} catch (e) { | ||
console.log(e); | ||
Position.removeOverride(positionId); | ||
Moves.removeOverride(movesId); | ||
} finally { | ||
Position.removeOverride(positionId); | ||
Moves.removeOverride(movesId); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ensure proper cleanup in the finally
block.
The finally
block in the spawn
function removes position and moves overrides regardless of the outcome. This might lead to issues if the transaction is successful. Consider removing these lines from the finally
block and handling them only in the catch
block.
- finally {
- Position.removeOverride(positionId);
- Moves.removeOverride(movesId);
- }
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
const spawn = async (account: AccountInterface) => { | |
const entityId = getEntityIdFromKeys([ | |
BigInt(account.address), | |
]) as Entity; | |
const positionId = uuid(); | |
Position.addOverride(positionId, { | |
entity: entityId, | |
value: { player: BigInt(entityId), vec: { x: 10, y: 10 } }, | |
}); | |
const movesId = uuid(); | |
Moves.addOverride(movesId, { | |
entity: entityId, | |
value: { | |
player: BigInt(entityId), | |
remaining: 100, | |
last_direction: 0, | |
}, | |
}); | |
try { | |
const { transaction_hash } = await client.actions.spawn({ | |
account, | |
}); | |
// setComponentsFromEvents( | |
// contractComponents, | |
// getEvents( | |
// await account.waitForTransaction(transaction_hash, { | |
// retryInterval: 100, | |
// }) | |
// ) | |
// ); | |
} catch (e) { | |
console.log(e); | |
Position.removeOverride(positionId); | |
Moves.removeOverride(movesId); | |
} finally { | |
Position.removeOverride(positionId); | |
Moves.removeOverride(movesId); | |
} | |
const spawn = async (account: AccountInterface) => { | |
const entityId = getEntityIdFromKeys([ | |
BigInt(account.address), | |
]) as Entity; | |
const positionId = uuid(); | |
Position.addOverride(positionId, { | |
entity: entityId, | |
value: { player: BigInt(entityId), vec: { x: 10, y: 10 } }, | |
}); | |
const movesId = uuid(); | |
Moves.addOverride(movesId, { | |
entity: entityId, | |
value: { | |
player: BigInt(entityId), | |
remaining: 100, | |
last_direction: 0, | |
}, | |
}); | |
try { | |
const { transaction_hash } = await client.actions.spawn({ | |
account, | |
}); | |
// setComponentsFromEvents( | |
// contractComponents, | |
// getEvents( | |
// await account.waitForTransaction(transaction_hash, { | |
// retryInterval: 100, | |
// }) | |
// ) | |
// ); | |
} catch (e) { | |
console.log(e); | |
Position.removeOverride(positionId); | |
Moves.removeOverride(movesId); | |
} |
const spawn = async (account: AccountInterface) => { | ||
const entityId = getEntityIdFromKeys([ | ||
BigInt(account.address), | ||
]) as Entity; | ||
|
||
const positionId = uuid(); | ||
Position.addOverride(positionId, { | ||
entity: entityId, | ||
value: { player: BigInt(entityId), vec: { x: 10, y: 10 } }, | ||
}); | ||
|
||
const movesId = uuid(); | ||
Moves.addOverride(movesId, { | ||
entity: entityId, | ||
value: { | ||
player: BigInt(entityId), | ||
remaining: 100, | ||
last_direction: 0, | ||
}, | ||
}); | ||
|
||
try { | ||
const { transaction_hash } = await client.actions.spawn({ | ||
account, | ||
}); | ||
// setComponentsFromEvents( | ||
// contractComponents, | ||
// getEvents( | ||
// await account.waitForTransaction(transaction_hash, { | ||
// retryInterval: 100, | ||
// }) | ||
// ) | ||
// ); | ||
} catch (e) { | ||
console.log(e); | ||
Position.removeOverride(positionId); | ||
Moves.removeOverride(movesId); | ||
} finally { | ||
Position.removeOverride(positionId); | ||
Moves.removeOverride(movesId); | ||
} | ||
}; | ||
|
||
const move = async ( | ||
account: AccountInterface, | ||
direction: Direction | ||
) => { | ||
const entityId = getEntityIdFromKeys([ | ||
BigInt(account.address), | ||
]) as Entity; | ||
|
||
console.log(direction, getComponentValue(Position, entityId)); | ||
const positionId = uuid(); | ||
Position.addOverride(positionId, { | ||
entity: entityId, | ||
value: { | ||
player: BigInt(entityId), | ||
vec: updatePositionWithDirection( | ||
direction, | ||
getComponentValue(Position, entityId) as any | ||
).vec, | ||
}, | ||
}); | ||
|
||
const movesId = uuid(); | ||
Moves.addOverride(movesId, { | ||
entity: entityId, | ||
value: { | ||
player: BigInt(entityId), | ||
remaining: | ||
(getComponentValue(Moves, entityId)?.remaining || 0) - | ||
1, | ||
}, | ||
}); | ||
|
||
try { | ||
const { transaction_hash } = await client.actions.move({ | ||
account, | ||
direction, | ||
}); | ||
|
||
setComponentsFromEvents( | ||
contractComponents, | ||
getEvents( | ||
await account.waitForTransaction(transaction_hash, { | ||
retryInterval: 100, | ||
}) | ||
) | ||
); | ||
} catch (e) { | ||
console.error(e); | ||
Position.removeOverride(positionId); | ||
Moves.removeOverride(movesId); | ||
} finally { | ||
Position.removeOverride(positionId); | ||
Moves.removeOverride(movesId); | ||
} | ||
}; | ||
return { spawn, move }; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ensure error handling and logging consistency.
The actions
function contains two asynchronous functions: spawn
and move
. Both functions have try-catch blocks for error handling, but the error logging is inconsistent. Use console.error
for both to maintain consistency.
- console.log(e);
+ console.error(e);
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
function actions() { | |
const spawn = async (account: AccountInterface) => { | |
const entityId = getEntityIdFromKeys([ | |
BigInt(account.address), | |
]) as Entity; | |
const positionId = uuid(); | |
Position.addOverride(positionId, { | |
entity: entityId, | |
value: { player: BigInt(entityId), vec: { x: 10, y: 10 } }, | |
}); | |
const movesId = uuid(); | |
Moves.addOverride(movesId, { | |
entity: entityId, | |
value: { | |
player: BigInt(entityId), | |
remaining: 100, | |
last_direction: 0, | |
}, | |
}); | |
try { | |
const { transaction_hash } = await client.actions.spawn({ | |
account, | |
}); | |
// setComponentsFromEvents( | |
// contractComponents, | |
// getEvents( | |
// await account.waitForTransaction(transaction_hash, { | |
// retryInterval: 100, | |
// }) | |
// ) | |
// ); | |
} catch (e) { | |
console.log(e); | |
Position.removeOverride(positionId); | |
Moves.removeOverride(movesId); | |
} finally { | |
Position.removeOverride(positionId); | |
Moves.removeOverride(movesId); | |
} | |
}; | |
const move = async ( | |
account: AccountInterface, | |
direction: Direction | |
) => { | |
const entityId = getEntityIdFromKeys([ | |
BigInt(account.address), | |
]) as Entity; | |
console.log(direction, getComponentValue(Position, entityId)); | |
const positionId = uuid(); | |
Position.addOverride(positionId, { | |
entity: entityId, | |
value: { | |
player: BigInt(entityId), | |
vec: updatePositionWithDirection( | |
direction, | |
getComponentValue(Position, entityId) as any | |
).vec, | |
}, | |
}); | |
const movesId = uuid(); | |
Moves.addOverride(movesId, { | |
entity: entityId, | |
value: { | |
player: BigInt(entityId), | |
remaining: | |
(getComponentValue(Moves, entityId)?.remaining || 0) - | |
1, | |
}, | |
}); | |
try { | |
const { transaction_hash } = await client.actions.move({ | |
account, | |
direction, | |
}); | |
setComponentsFromEvents( | |
contractComponents, | |
getEvents( | |
await account.waitForTransaction(transaction_hash, { | |
retryInterval: 100, | |
}) | |
) | |
); | |
} catch (e) { | |
console.error(e); | |
Position.removeOverride(positionId); | |
Moves.removeOverride(movesId); | |
} finally { | |
Position.removeOverride(positionId); | |
Moves.removeOverride(movesId); | |
} | |
}; | |
return { spawn, move }; | |
} | |
function actions() { | |
const spawn = async (account: AccountInterface) => { | |
const entityId = getEntityIdFromKeys([ | |
BigInt(account.address), | |
]) as Entity; | |
const positionId = uuid(); | |
Position.addOverride(positionId, { | |
entity: entityId, | |
value: { player: BigInt(entityId), vec: { x: 10, y: 10 } }, | |
}); | |
const movesId = uuid(); | |
Moves.addOverride(movesId, { | |
entity: entityId, | |
value: { | |
player: BigInt(entityId), | |
remaining: 100, | |
last_direction: 0, | |
}, | |
}); | |
try { | |
const { transaction_hash } = await client.actions.spawn({ | |
account, | |
}); | |
// setComponentsFromEvents( | |
// contractComponents, | |
// getEvents( | |
// await account.waitForTransaction(transaction_hash, { | |
// retryInterval: 100, | |
// }) | |
// ) | |
// ); | |
} catch (e) { | |
console.error(e); | |
Position.removeOverride(positionId); | |
Moves.removeOverride(movesId); | |
} finally { | |
Position.removeOverride(positionId); | |
Moves.removeOverride(movesId); | |
} | |
}; | |
const move = async ( | |
account: AccountInterface, | |
direction: Direction | |
) => { | |
const entityId = getEntityIdFromKeys([ | |
BigInt(account.address), | |
]) as Entity; | |
console.log(direction, getComponentValue(Position, entityId)); | |
const positionId = uuid(); | |
Position.addOverride(positionId, { | |
entity: entityId, | |
value: { | |
player: BigInt(entityId), | |
vec: updatePositionWithDirection( | |
direction, | |
getComponentValue(Position, entityId) as any | |
).vec, | |
}, | |
}); | |
const movesId = uuid(); | |
Moves.addOverride(movesId, { | |
entity: entityId, | |
value: { | |
player: BigInt(entityId), | |
remaining: | |
(getComponentValue(Moves, entityId)?.remaining || 0) - | |
1, | |
}, | |
}); | |
try { | |
const { transaction_hash } = await client.actions.move({ | |
account, | |
direction, | |
}); | |
setComponentsFromEvents( | |
contractComponents, | |
getEvents( | |
await account.waitForTransaction(transaction_hash, { | |
retryInterval: 100, | |
}) | |
) | |
); | |
} catch (e) { | |
console.error(e); | |
Position.removeOverride(positionId); | |
Moves.removeOverride(movesId); | |
} finally { | |
Position.removeOverride(positionId); | |
Moves.removeOverride(movesId); | |
} | |
}; | |
return { spawn, move }; | |
} |
constructor(dojo: IDojo) { | ||
super({ key: "MainScene" }); | ||
this.dojo = dojo; | ||
|
||
this.chunkSize = 16; | ||
this.tileSize = 16; | ||
this.cameraSpeed = 2; | ||
this.chunks = []; | ||
this.keyW = null; | ||
this.keyS = null; | ||
this.keyA = null; | ||
this.keyD = null; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ensure proper initialization of properties.
The constructor
initializes several properties, but followPoint
is not initialized. Consider initializing followPoint
to avoid potential undefined errors.
+ this.followPoint = new Phaser.Math.Vector2(0, 0);
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
constructor(dojo: IDojo) { | |
super({ key: "MainScene" }); | |
this.dojo = dojo; | |
this.chunkSize = 16; | |
this.tileSize = 16; | |
this.cameraSpeed = 2; | |
this.chunks = []; | |
this.keyW = null; | |
this.keyS = null; | |
this.keyA = null; | |
this.keyD = null; | |
} | |
constructor(dojo: IDojo) { | |
super({ key: "MainScene" }); | |
this.dojo = dojo; | |
this.chunkSize = 16; | |
this.tileSize = 16; | |
this.cameraSpeed = 2; | |
this.chunks = []; | |
this.keyW = null; | |
this.keyS = null; | |
this.keyA = null; | |
this.keyD = null; | |
this.followPoint = new Phaser.Math.Vector2(0, 0); | |
} |
create() { | ||
this.anims.create({ | ||
key: "sprWater", | ||
frames: this.anims.generateFrameNumbers("sprWater", { | ||
start: 0, | ||
end: 1, | ||
}), | ||
frameRate: 5, | ||
repeat: -1, | ||
}); | ||
|
||
this.cameras.main.setZoom(2); | ||
this.followPoint = new Phaser.Math.Vector2( | ||
this.cameras.main.worldView.x + | ||
this.cameras.main.worldView.width * 2, | ||
this.cameras.main.worldView.y + | ||
this.cameras.main.worldView.height * 2 | ||
); | ||
|
||
if (this.input.keyboard === null) { | ||
throw new Error("plugin input.keyboard is not loaded"); | ||
} | ||
|
||
this.keyW = this.input.keyboard.addKey( | ||
Phaser.Input.Keyboard.KeyCodes.W | ||
); | ||
this.keyS = this.input.keyboard.addKey( | ||
Phaser.Input.Keyboard.KeyCodes.S | ||
); | ||
this.keyA = this.input.keyboard.addKey( | ||
Phaser.Input.Keyboard.KeyCodes.A | ||
); | ||
this.keyD = this.input.keyboard.addKey( | ||
Phaser.Input.Keyboard.KeyCodes.D | ||
); | ||
|
||
this.dojo.systemCalls.spawn(this.dojo.account); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ensure proper error handling for keyboard input.
The create
method throws an error if the keyboard input plugin is not loaded. Consider providing a more descriptive error message or handling the error gracefully.
- throw new Error("plugin input.keyboard is not loaded");
+ throw new Error("Keyboard input plugin is not loaded. Ensure the plugin is included in the Phaser configuration.");
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
create() { | |
this.anims.create({ | |
key: "sprWater", | |
frames: this.anims.generateFrameNumbers("sprWater", { | |
start: 0, | |
end: 1, | |
}), | |
frameRate: 5, | |
repeat: -1, | |
}); | |
this.cameras.main.setZoom(2); | |
this.followPoint = new Phaser.Math.Vector2( | |
this.cameras.main.worldView.x + | |
this.cameras.main.worldView.width * 2, | |
this.cameras.main.worldView.y + | |
this.cameras.main.worldView.height * 2 | |
); | |
if (this.input.keyboard === null) { | |
throw new Error("plugin input.keyboard is not loaded"); | |
} | |
this.keyW = this.input.keyboard.addKey( | |
Phaser.Input.Keyboard.KeyCodes.W | |
); | |
this.keyS = this.input.keyboard.addKey( | |
Phaser.Input.Keyboard.KeyCodes.S | |
); | |
this.keyA = this.input.keyboard.addKey( | |
Phaser.Input.Keyboard.KeyCodes.A | |
); | |
this.keyD = this.input.keyboard.addKey( | |
Phaser.Input.Keyboard.KeyCodes.D | |
); | |
this.dojo.systemCalls.spawn(this.dojo.account); | |
create() { | |
this.anims.create({ | |
key: "sprWater", | |
frames: this.anims.generateFrameNumbers("sprWater", { | |
start: 0, | |
end: 1, | |
}), | |
frameRate: 5, | |
repeat: -1, | |
}); | |
this.cameras.main.setZoom(2); | |
this.followPoint = new Phaser.Math.Vector2( | |
this.cameras.main.worldView.x + | |
this.cameras.main.worldView.width * 2, | |
this.cameras.main.worldView.y + | |
this.cameras.main.worldView.height * 2 | |
); | |
if (this.input.keyboard === null) { | |
throw new Error("Keyboard input plugin is not loaded. Ensure the plugin is included in the Phaser configuration."); | |
} | |
this.keyW = this.input.keyboard.addKey( | |
Phaser.Input.Keyboard.KeyCodes.W | |
); | |
this.keyS = this.input.keyboard.addKey( | |
Phaser.Input.Keyboard.KeyCodes.S | |
); | |
this.keyA = this.input.keyboard.addKey( | |
Phaser.Input.Keyboard.KeyCodes.A | |
); | |
this.keyD = this.input.keyboard.addKey( | |
Phaser.Input.Keyboard.KeyCodes.D | |
); | |
this.dojo.systemCalls.spawn(this.dojo.account); |
update() { | ||
if (this.followPoint === null || this.followPoint === undefined) { | ||
throw new Error("failed to initialize followPoint"); | ||
} | ||
if ( | ||
this.checkInputs([this.keyA, this.keyD, this.keyW, this.keyS]) === | ||
false | ||
) { | ||
throw new Error("failed to initialize inputs"); | ||
} | ||
var snappedChunkX = | ||
this.chunkSize * | ||
this.tileSize * | ||
Math.round(this.followPoint.x / (this.chunkSize * this.tileSize)); | ||
var snappedChunkY = | ||
this.chunkSize * | ||
this.tileSize * | ||
Math.round(this.followPoint.y / (this.chunkSize * this.tileSize)); | ||
|
||
snappedChunkX = snappedChunkX / this.chunkSize / this.tileSize; | ||
snappedChunkY = snappedChunkY / this.chunkSize / this.tileSize; | ||
|
||
for (var x = snappedChunkX - 2; x < snappedChunkX + 2; x++) { | ||
for (var y = snappedChunkY - 2; y < snappedChunkY + 2; y++) { | ||
var existingChunk = this.getChunk(x, y); | ||
|
||
if (existingChunk == null) { | ||
var newChunk = new Chunk(this, x, y); | ||
this.chunks.push(newChunk); | ||
} | ||
} | ||
} | ||
|
||
for (var i = 0; i < this.chunks.length; i++) { | ||
var chunk = this.chunks[i]; | ||
|
||
if ( | ||
Phaser.Math.Distance.Between( | ||
snappedChunkX, | ||
snappedChunkY, | ||
chunk.x, | ||
chunk.y | ||
) < 3 | ||
) { | ||
if (chunk !== null) { | ||
chunk.load(); | ||
} | ||
} else { | ||
if (chunk !== null) { | ||
chunk.unload(); | ||
} | ||
} | ||
} | ||
|
||
if (null !== this.keyW && this.keyW.isDown) { | ||
this.followPoint.y -= this.cameraSpeed; | ||
this.cameras.main.centerOn(this.followPoint.x, this.followPoint.y); | ||
this.dojo.systemCalls.move(this.dojo.account, Direction.Up); | ||
return; | ||
} | ||
if (null !== this.keyS && this.keyS.isDown) { | ||
this.followPoint.y += this.cameraSpeed; | ||
this.cameras.main.centerOn(this.followPoint.x, this.followPoint.y); | ||
this.dojo.systemCalls.move(this.dojo.account, Direction.Down); | ||
return; | ||
} | ||
if (null !== this.keyA && this.keyA.isDown) { | ||
this.followPoint.x -= this.cameraSpeed; | ||
this.cameras.main.centerOn(this.followPoint.x, this.followPoint.y); | ||
this.dojo.systemCalls.move(this.dojo.account, Direction.Left); | ||
return; | ||
} | ||
if (null !== this.keyD && this.keyD.isDown) { | ||
this.followPoint.x += this.cameraSpeed; | ||
this.cameras.main.centerOn(this.followPoint.x, this.followPoint.y); | ||
this.dojo.systemCalls.move(this.dojo.account, Direction.Right); | ||
return; | ||
} | ||
|
||
this.cameras.main.centerOn(this.followPoint.x, this.followPoint.y); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optimize chunk management logic.
The update
method's chunk management logic can be optimized by using a Map
for this.chunks
to improve lookup performance.
- this.chunks = [];
+ this.chunks = new Map();
- var chunk = null;
- for (var i = 0; i < this.chunks.length; i++) {
- if (this.chunks[i].x == x && this.chunks[i].y == y) {
- chunk = this.chunks[i];
- }
- }
- return chunk;
+ return this.chunks.get(`${x},${y}`);
- var newChunk = new Chunk(this, x, y);
- this.chunks.push(newChunk);
+ var newChunk = new Chunk(this, x, y);
+ this.chunks.set(`${x},${y}`, newChunk);
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
update() { | |
if (this.followPoint === null || this.followPoint === undefined) { | |
throw new Error("failed to initialize followPoint"); | |
} | |
if ( | |
this.checkInputs([this.keyA, this.keyD, this.keyW, this.keyS]) === | |
false | |
) { | |
throw new Error("failed to initialize inputs"); | |
} | |
var snappedChunkX = | |
this.chunkSize * | |
this.tileSize * | |
Math.round(this.followPoint.x / (this.chunkSize * this.tileSize)); | |
var snappedChunkY = | |
this.chunkSize * | |
this.tileSize * | |
Math.round(this.followPoint.y / (this.chunkSize * this.tileSize)); | |
snappedChunkX = snappedChunkX / this.chunkSize / this.tileSize; | |
snappedChunkY = snappedChunkY / this.chunkSize / this.tileSize; | |
for (var x = snappedChunkX - 2; x < snappedChunkX + 2; x++) { | |
for (var y = snappedChunkY - 2; y < snappedChunkY + 2; y++) { | |
var existingChunk = this.getChunk(x, y); | |
if (existingChunk == null) { | |
var newChunk = new Chunk(this, x, y); | |
this.chunks.push(newChunk); | |
} | |
} | |
} | |
for (var i = 0; i < this.chunks.length; i++) { | |
var chunk = this.chunks[i]; | |
if ( | |
Phaser.Math.Distance.Between( | |
snappedChunkX, | |
snappedChunkY, | |
chunk.x, | |
chunk.y | |
) < 3 | |
) { | |
if (chunk !== null) { | |
chunk.load(); | |
} | |
} else { | |
if (chunk !== null) { | |
chunk.unload(); | |
} | |
} | |
} | |
if (null !== this.keyW && this.keyW.isDown) { | |
this.followPoint.y -= this.cameraSpeed; | |
this.cameras.main.centerOn(this.followPoint.x, this.followPoint.y); | |
this.dojo.systemCalls.move(this.dojo.account, Direction.Up); | |
return; | |
} | |
if (null !== this.keyS && this.keyS.isDown) { | |
this.followPoint.y += this.cameraSpeed; | |
this.cameras.main.centerOn(this.followPoint.x, this.followPoint.y); | |
this.dojo.systemCalls.move(this.dojo.account, Direction.Down); | |
return; | |
} | |
if (null !== this.keyA && this.keyA.isDown) { | |
this.followPoint.x -= this.cameraSpeed; | |
this.cameras.main.centerOn(this.followPoint.x, this.followPoint.y); | |
this.dojo.systemCalls.move(this.dojo.account, Direction.Left); | |
return; | |
} | |
if (null !== this.keyD && this.keyD.isDown) { | |
this.followPoint.x += this.cameraSpeed; | |
this.cameras.main.centerOn(this.followPoint.x, this.followPoint.y); | |
this.dojo.systemCalls.move(this.dojo.account, Direction.Right); | |
return; | |
} | |
this.cameras.main.centerOn(this.followPoint.x, this.followPoint.y); | |
} | |
update() { | |
if (this.followPoint === null || this.followPoint === undefined) { | |
throw new Error("failed to initialize followPoint"); | |
} | |
if ( | |
this.checkInputs([this.keyA, this.keyD, this.keyW, this.keyS]) === | |
false | |
) { | |
throw new Error("failed to initialize inputs"); | |
} | |
var snappedChunkX = | |
this.chunkSize * | |
this.tileSize * | |
Math.round(this.followPoint.x / (this.chunkSize * this.tileSize)); | |
var snappedChunkY = | |
this.chunkSize * | |
this.tileSize * | |
Math.round(this.followPoint.y / (this.chunkSize * this.tileSize)); | |
snappedChunkX = snappedChunkX / this.chunkSize / this.tileSize; | |
snappedChunkY = snappedChunkY / this.chunkSize / this.tileSize; | |
for (var x = snappedChunkX - 2; x < snappedChunkX + 2; x++) { | |
for (var y = snappedChunkY - 2; y < snappedChunkY + 2; y++) { | |
var existingChunk = this.getChunk(x, y); | |
if (existingChunk == null) { | |
var newChunk = new Chunk(this, x, y); | |
this.chunks.set(`${x},${y}`, newChunk); | |
} | |
} | |
} | |
for (var i = 0; i < this.chunks.length; i++) { | |
var chunk = this.chunks[i]; | |
if ( | |
Phaser.Math.Distance.Between( | |
snappedChunkX, | |
snappedChunkY, | |
chunk.x, | |
chunk.y | |
) < 3 | |
) { | |
if (chunk !== null) { | |
chunk.load(); | |
} | |
} else { | |
if (chunk !== null) { | |
chunk.unload(); | |
} | |
} | |
} | |
if (null !== this.keyW && this.keyW.isDown) { | |
this.followPoint.y -= this.cameraSpeed; | |
this.cameras.main.centerOn(this.followPoint.x, this.followPoint.y); | |
this.dojo.systemCalls.move(this.dojo.account, Direction.Up); | |
return; | |
} | |
if (null !== this.keyS && this.keyS.isDown) { | |
this.followPoint.y += this.cameraSpeed; | |
this.cameras.main.centerOn(this.followPoint.x, this.followPoint.y); | |
this.dojo.systemCalls.move(this.dojo.account, Direction.Down); | |
return; | |
} | |
if (null !== this.keyA && this.keyA.isDown) { | |
this.followPoint.x -= this.cameraSpeed; | |
this.cameras.main.centerOn(this.followPoint.x, this.followPoint.y); | |
this.dojo.systemCalls.move(this.dojo.account, Direction.Left); | |
return; | |
} | |
if (null !== this.keyD && this.keyD.isDown) { | |
this.followPoint.x += this.cameraSpeed; | |
this.cameras.main.centerOn(this.followPoint.x, this.followPoint.y); | |
this.dojo.systemCalls.move(this.dojo.account, Direction.Right); | |
return; | |
} | |
this.cameras.main.centerOn(this.followPoint.x, this.followPoint.y); | |
} |
ead6aab
to
95e373c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files ignored due to path filters (5)
examples/vanilla/phaser/assets/sprGrass.png
is excluded by!**/*.png
examples/vanilla/phaser/assets/sprSand.png
is excluded by!**/*.png
examples/vanilla/phaser/assets/sprWater.png
is excluded by!**/*.png
examples/vanilla/phaser/public/favicon.ico
is excluded by!**/*.ico
pnpm-lock.yaml
is excluded by!**/pnpm-lock.yaml
Files selected for processing (20)
- examples/vanilla/phaser/.gitignore (1 hunks)
- examples/vanilla/phaser/dojoConfig.ts (1 hunks)
- examples/vanilla/phaser/index.html (1 hunks)
- examples/vanilla/phaser/package.json (1 hunks)
- examples/vanilla/phaser/src/dojo/createClientComponent.ts (1 hunks)
- examples/vanilla/phaser/src/dojo/defineContractComponents.ts (1 hunks)
- examples/vanilla/phaser/src/dojo/defineContractSystems.ts (1 hunks)
- examples/vanilla/phaser/src/dojo/models.ts (1 hunks)
- examples/vanilla/phaser/src/dojo/setup.ts (1 hunks)
- examples/vanilla/phaser/src/dojo/systems.ts (1 hunks)
- examples/vanilla/phaser/src/dojo/utils.ts (1 hunks)
- examples/vanilla/phaser/src/dojo/world.ts (1 hunks)
- examples/vanilla/phaser/src/entities.ts (1 hunks)
- examples/vanilla/phaser/src/main.ts (1 hunks)
- examples/vanilla/phaser/src/scenes/scene-main.ts (1 hunks)
- examples/vanilla/phaser/tsconfig.json (1 hunks)
- examples/vanilla/phaser/vite.config.ts (1 hunks)
- package.json (1 hunks)
- pnpm-workspace.yaml (1 hunks)
- scripts/build-examples.sh (1 hunks)
Files skipped from review due to trivial changes (6)
- examples/vanilla/phaser/index.html
- examples/vanilla/phaser/package.json
- examples/vanilla/phaser/src/dojo/world.ts
- examples/vanilla/phaser/src/entities.ts
- examples/vanilla/phaser/tsconfig.json
- examples/vanilla/phaser/vite.config.ts
Files skipped from review as they are similar to previous changes (13)
- examples/vanilla/phaser/.gitignore
- examples/vanilla/phaser/dojoConfig.ts
- examples/vanilla/phaser/src/dojo/createClientComponent.ts
- examples/vanilla/phaser/src/dojo/defineContractComponents.ts
- examples/vanilla/phaser/src/dojo/defineContractSystems.ts
- examples/vanilla/phaser/src/dojo/models.ts
- examples/vanilla/phaser/src/dojo/setup.ts
- examples/vanilla/phaser/src/dojo/systems.ts
- examples/vanilla/phaser/src/dojo/utils.ts
- examples/vanilla/phaser/src/main.ts
- package.json
- pnpm-workspace.yaml
- scripts/build-examples.sh
Additional context used
Biome
examples/vanilla/phaser/src/scenes/scene-main.ts
[error] 11-11: Don't use 'String' as a type.
Use lowercase primitives for consistency.
Safe fix: Use 'string' instead(lint/complexity/noBannedTypes)
Additional comments not posted (6)
examples/vanilla/phaser/src/scenes/scene-main.ts (6)
31-38
: LGTM!The
preload
method correctly loads the assets.
58-59
: Ensure proper error handling for keyboard input.The
create
method throws an error if the keyboard input plugin is not loaded. Consider providing a more descriptive error message.- throw new Error("plugin input.keyboard is not loaded"); + throw new Error("Keyboard input plugin is not loaded. Ensure the plugin is included in the Phaser configuration.");
78-83
: LGTM!The
getChunk
method correctly retrieves the chunk from thechunks
map.
105-113
: Optimize chunk management logic.The chunk management logic can be optimized by using a
Map
forthis.chunks
to improve lookup performance.- var chunk = null; - for (var i = 0; i < this.chunks.length; i++) { - if (this.chunks[i].x == x && this.chunks[i].y == y) { - chunk = this.chunks[i]; - } - } - return chunk; + return this.chunks.get(`${x},${y}`); - var newChunk = new Chunk(this, x, y); - this.chunks.push(newChunk); + var newChunk = new Chunk(this, x, y); + this.chunks.set(`${x},${y}`, newChunk);
133-155
: LGTM!The character movement logic correctly updates the
followPoint
and camera position based on key presses.
161-167
: LGTM!The
checkInputs
method correctly verifies that all input keys are initialized.
tileSize: number; | ||
cameraSpeed: number; | ||
followPoint?: Phaser.Math.Vector2; | ||
chunks: Map<String, Chunk>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use lowercase primitive for type consistency.
Change the type of chunks
map key from 'String' to 'string' for consistency.
- chunks: Map<String, Chunk>;
+ chunks: Map<string, Chunk>;
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
chunks: Map<String, Chunk>; | |
chunks: Map<string, Chunk>; |
Tools
Biome
[error] 11-11: Don't use 'String' as a type.
Use lowercase primitives for consistency.
Safe fix: Use 'string' instead(lint/complexity/noBannedTypes)
95e373c
to
7c6d94e
Compare
Closes #242
Introduced changes
Adds a new example using vanilla js and phaser
Checklist
Summary by CodeRabbit