Skip to content

Commit

Permalink
adding hooks to snake
Browse files Browse the repository at this point in the history
  • Loading branch information
thiscaspar committed Dec 4, 2024
1 parent bd3f806 commit 0eba57a
Showing 1 changed file with 46 additions and 1 deletion.
47 changes: 46 additions & 1 deletion contracts/src/apps/snake/app.cairo
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use pixelaw::core::models::{pixel::{PixelUpdate}, registry::{App}};
use pixelaw::core::utils::{Direction, DefaultParameters};
use starknet::{ContractAddress};

Expand Down Expand Up @@ -91,6 +92,15 @@ pub const APP_ICON: felt252 = 'U+1F40D';
/// Interface for Snake actions.
#[starknet::interface]
pub trait ISnakeActions<T> {
fn on_pre_update(
ref self: T, pixel_update: PixelUpdate, app_caller: App, player_caller: ContractAddress
) -> Option<PixelUpdate>;

fn on_post_update(
ref self: T, pixel_update: PixelUpdate, app_caller: App, player_caller: ContractAddress
);


/// Initializes the Snake App.
///
/// # Arguments
Expand Down Expand Up @@ -131,7 +141,8 @@ pub mod snake_actions {
IActionsDispatcherTrait as ICoreActionsDispatcherTrait,
};
use pixelaw::core::models::pixel::{Pixel, PixelUpdate, PixelUpdateResultTrait};
//use pixelaw::core::models::registry::App;

use pixelaw::core::models::registry::App;
use pixelaw::core::utils::{
MOVE_SELECTOR, get_callers, get_core_actions, Direction, Position, DefaultParameters
};
Expand Down Expand Up @@ -168,6 +179,40 @@ pub mod snake_actions {
/// Implementation of the Snake actions.
#[abi(embed_v0)]
impl ActionsImpl of ISnakeActions<ContractState> {
/// Hook called before a pixel update.
///
/// # Arguments
///
/// * `world` - A reference to the world dispatcher.
/// * `pixel_update` - The proposed update to the pixel.
/// * `app_caller` - The app initiating the update.
/// * `player_caller` - The player initiating the update.
fn on_pre_update(
ref self: ContractState,
pixel_update: PixelUpdate,
app_caller: App,
player_caller: ContractAddress,
) -> Option<PixelUpdate> {
//Default is to not allow anything
Option::None
}

/// Hook called after a pixel update.
///
/// # Arguments
///
/// * `world` - A reference to the world dispatcher.
/// * `pixel_update` - The update that was applied to the pixel.
/// * `app_caller` - The app that performed the update.
/// * `player_caller` - The player that performed the update.
fn on_post_update(
ref self: ContractState,
pixel_update: PixelUpdate,
app_caller: App,
player_caller: ContractAddress,
) { // No action
}

/// Initializes the Snake App.
///
/// Registers the app with core actions
Expand Down

0 comments on commit 0eba57a

Please sign in to comment.