buesingniklas

Untitled

Mar 3rd, 2019
2,813
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const aClientSystem = client.registerSystem(0, 0);
  2.  
  3. // Global variables
  4. let globalVars = {};
  5.  
  6. // A query to control fighters that need to be animated
  7. globalVars.animationQuery = {};
  8.  
  9. // Setup which events to listen for
  10. aClientSystem.initialize = function () {
  11.     this.listenForEvent("minecraft:pick_hit_result_changed", (eventData) => this.onPick(eventData));
  12.  
  13.     // Setup callback for when the player enters the world
  14.     this.listenForEvent("minecraft:client_entered_world", (eventData) => this.onClientEnteredWorld(eventData));
  15.  
  16.     // Setup callback for UI events from the custom screens
  17.     this.listenForEvent("minecraft:ui_event", (eventData) => this.onUIMessage(eventData));
  18.  
  19.     // Setup callback for the victory event from the server script
  20.     this.listenForEvent("rpg_game:victory", (eventData) => this.onVictory(eventData));
  21.  
  22.     // Setup callback for the loss event from the server script
  23.     this.listenForEvent("rpg_game:loss", (eventData) => this.onLoss(eventData));
  24.  
  25.     // Setup callback for the turn order event from the server script
  26.     this.listenForEvent("rpg_game:update_turn_order", (eventData) => this.onUpdateTurnOrder(eventData));
  27.  
  28.     // Setup callback to handle the on-hit animation from the server script
  29.     this.listenForEvent("rpg_game:execute_on_hit_animation", (eventData) => this.onExecuteOnHitAnimation(eventData));  
  30.  
  31.     // Register a script only component to help control animations
  32.     this.registerComponent("rpg_game:animation_controller", { counter: 0 });
  33.  
  34.     // Register a query to get all fighters that have the custom component that helps control animations
  35.     globalVars.animationQuery = this.registerQuery();
  36.     this.addFilterToQuery(globalVars.animationQuery, "rpg_game:animation_controller");
  37.  
  38. };
  39.  
  40. aClientSystem.update = function () {
  41.     // Update all animating fighters
  42.     this.UpdateAnimationState();
  43. };
  44.  
  45. aClientSystem.onPick = function (eventData) {
  46.     // Relay pick event to the server script to update the newest hovered target
  47.     this.broadcastEvent("rpg_game:update_hovered_target", eventData);
  48. };
  49.  
  50. aClientSystem.onUpdateTurnOrder = function (turnOrderData) {
  51.     // Package up the turn order data to send to the custom screen for display.
  52.     // The data property requires a string. Turn an object into a string by using JSON.stringify()
  53.     let uiEventData = {
  54.         eventName: "UpdateTurnOrder",
  55.         data: JSON.stringify(turnOrderData)
  56.     };
  57.  
  58.     // Send the event to the custom screen
  59.     this.broadcastEvent("minecraft:send_ui_event", uiEventData);
  60. };
  61.  
  62. aClientSystem.onClientEnteredWorld = function (eventData) {
  63.     // Client has entered the world, show the starting screen
  64.     aClientSystem.broadcastEvent("minecraft:load_ui", {path:"rpg_game_start.html"});
  65.     // Notify the server script that the player has finished loading in.
  66.     aClientSystem.broadcastEvent("rpg_game:client_entered_world", eventData);
  67. };
  68.  
  69. aClientSystem.onVictory = function (eventData) {
  70.     // Server told us that the player won, remove the game screen and show the victory screen
  71.     this.broadcastEvent("minecraft:unload_ui", {path:"rpg_game.html"});
  72.     this.broadcastEvent("minecraft:load_ui", {path:"rpg_game_victory.html"});
  73. };
  74.  
  75. aClientSystem.onLoss = function (eventData) {
  76.     // Server told us that the player lost, remove the game screen and show the loss screen
  77.     this.broadcastEvent("minecraft:unload_ui", {path:"rpg_game.html"});
  78.     this.broadcastEvent("minecraft:load_ui", {path:"rpg_game_loss.html"});
  79. };
  80.  
  81. aClientSystem.onUIMessage = function (eventData) {
  82.     // UI engine sent us an event.
  83.     if (eventData === "damageSingleTargetAbilityClicked" ||
  84.         eventData === "damageWholeTeamAbilityClicked" ||
  85.         eventData === "healSingleTargetAbilityClicked") {
  86.         // An ability button was clicked. Send an ability clicked event to the server script
  87.         this.broadcastEvent("rpg_game:ability_clicked", eventData);
  88.     }
  89.     else if (eventData === "rpg_game:click") {
  90.         this.broadcastEvent("rpg_game:click", "");
  91.     }
  92.     else if (eventData === "startPressed" || eventData === "restartPressed") {
  93.         // Start or restart button was pressed on a screen. Start up the game.
  94.         this.startGame();
  95.     }
  96.     else if (eventData === "leavePressed") {
  97.         // Leave button was pressed. Exit all custom screens and stop turn-based game.
  98.         this.leaveGame();
  99.     }
  100. };
  101.  
  102. aClientSystem.startGame = function () {
  103.     // Remove any screens we might have loaded
  104.     this.broadcastEvent("minecraft:unload_ui", {path:"rpg_game_start.html"});
  105.     this.broadcastEvent("minecraft:unload_ui", {path:"rpg_game.html"});
  106.     this.broadcastEvent("minecraft:unload_ui", {path:"rpg_game_loss.html"});
  107.     this.broadcastEvent("minecraft:unload_ui", {path:"rpg_game_victory.html"});
  108.  
  109.     // Load and show the game screen
  110.     this.broadcastEvent("minecraft:load_ui", {path:"rpg_game.html"});
  111.     // Tell the server to start the game
  112.     this.broadcastEvent("rpg_game:start", "");
  113. };
  114.  
  115. aClientSystem.leaveGame = function () {
  116.     // Remove any screens we might have loaded
  117.     this.broadcastEvent("minecraft:unload_ui", {path:"rpg_game_start.html"});
  118.     this.broadcastEvent("minecraft:unload_ui", {path:"rpg_game.html"});
  119.     this.broadcastEvent("minecraft:unload_ui", {path:"rpg_game_loss.html"});
  120.     this.broadcastEvent("minecraft:unload_ui", {path:"rpg_game_victory.html"});
  121.    
  122.     // Tell the server to leave the game
  123.     this.broadcastEvent("rpg_game:leave", "");
  124. };
  125.  
  126. aClientSystem.onExecuteOnHitAnimation = function(fighter) {
  127.     // Add a animation controller component to the fighter that was just hit
  128.     // Used to track animation counters
  129.     this.createComponent(fighter, "rpg_game:animation_controller");
  130. };
  131.  
  132. aClientSystem.UpdateAnimationState = function () {
  133.     // Get all currently animating fighters
  134.     let animatedFighters = this.getEntitiesFromQuery(globalVars.animationQuery);
  135.  
  136.     // Update animation
  137.     for (let index in animatedFighters) {
  138.         let fighter = animatedFighters[index];
  139.         let molangComponent = this.getComponent(fighter, "minecraft:molang");
  140.         // Get the animation controller component that was added when the fighter was hit
  141.         let animationComponent = this.getComponent(fighter, "rpg_game:animation_controller");
  142.         if (animationComponent.data.counter < 1) {
  143.             // Increment counter and base the fighters animation on it
  144.             animationComponent.data.counter += 0.03;
  145.             molangComponent.data["variable.script_attacktime"] = animationComponent.data.counter;
  146.             this.applyComponentChanges(fighter, animationComponent);
  147.         }
  148.         else {
  149.             // Animation has finished. Reset the molang component. Remove the animation controller component from the fighter.
  150.             molangComponent.data["variable.script_attacktime"] = 0;
  151.             this.destroyComponent(fighter, "rpg_game:animation_controller");
  152.         }
  153.         this.applyComponentChanges(fighter, molangComponent);
  154.     }
  155. };
Advertisement
Add Comment
Please, Sign In to add comment