code refactor part 1: advance_tic is now two parts
seems to work so far
This commit is contained in:
parent
c8d80dfc63
commit
2e1a87199a
58
js/game.js
58
js/game.js
@ -187,7 +187,6 @@ export class Level {
|
|||||||
this.time_remaining = this.stored_level.time_limit * 20;
|
this.time_remaining = this.stored_level.time_limit * 20;
|
||||||
}
|
}
|
||||||
this.timer_paused = false
|
this.timer_paused = false
|
||||||
this.waiting_for_input = false
|
|
||||||
// Note that this clock counts *up*, even on untimed levels, and is unaffected by CC2's
|
// Note that this clock counts *up*, even on untimed levels, and is unaffected by CC2's
|
||||||
// clock alteration shenanigans
|
// clock alteration shenanigans
|
||||||
this.tic_counter = 0;
|
this.tic_counter = 0;
|
||||||
@ -331,15 +330,32 @@ export class Level {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
player_awaiting_input() {
|
||||||
|
//todo: the tic_counter part feels kludgey. maybe there's some other way a wait/wall nudge can wait a certain amount of time per tap?
|
||||||
|
return this.player.movement_cooldown === 0 && this.player.slide_mode === null && this.tic_counter % 2 == 0
|
||||||
|
}
|
||||||
|
|
||||||
// Move the game state forwards by one tic
|
// Move the game state forwards by one tic
|
||||||
advance_tic(p1_primary_direction, p1_secondary_direction, force_wait) {
|
// split into two parts for turn-based mode: first part is the consequences of the previous tick, second part depends on the player's input
|
||||||
|
advance_tic(p1_primary_direction, p1_secondary_direction, pass) {
|
||||||
if (this.state !== 'playing') {
|
if (this.state !== 'playing') {
|
||||||
console.warn(`Level.advance_tic() called when state is ${this.state}`);
|
console.warn(`Level.advance_tic() called when state is ${this.state}`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
this._advance_tic(p1_primary_direction, p1_secondary_direction, force_wait);
|
if (pass == 1)
|
||||||
|
{
|
||||||
|
this._advance_tic_part1(p1_primary_direction, p1_secondary_direction);
|
||||||
|
}
|
||||||
|
else if (pass == 2)
|
||||||
|
{
|
||||||
|
this._advance_tic_part2(p1_primary_direction, p1_secondary_direction);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
console.warn(`What pass is this?`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
if (e instanceof GameEnded) {
|
if (e instanceof GameEnded) {
|
||||||
@ -350,26 +366,13 @@ export class Level {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Commit the undo state at the end of each tic
|
// Commit the undo state at the end of each tic (pass 2)
|
||||||
if (!this.waiting_for_input) {
|
if (pass == 2) {
|
||||||
this.commit();
|
this.commit();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_advance_tic(p1_primary_direction, p1_secondary_direction, force_wait) {
|
_advance_tic_part1(p1_primary_direction, p1_secondary_direction) {
|
||||||
var skip_to_third_pass = false;
|
|
||||||
|
|
||||||
//if we're waiting for input, then we want to skip straight to phase 3 with a player decision filled out when they have one ready
|
|
||||||
if (this.waiting_for_input) {
|
|
||||||
this.actor_decision(this.player, p1_primary_direction);
|
|
||||||
if (this.player.decision != null || force_wait) {
|
|
||||||
skip_to_third_pass = true;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Player's secondary direction is set immediately; it applies on arrival to cells even if
|
// Player's secondary direction is set immediately; it applies on arrival to cells even if
|
||||||
// it wasn't held the last time the player started moving
|
// it wasn't held the last time the player started moving
|
||||||
this._set_prop(this.player, 'secondary_direction', p1_secondary_direction);
|
this._set_prop(this.player, 'secondary_direction', p1_secondary_direction);
|
||||||
@ -387,7 +390,6 @@ export class Level {
|
|||||||
// arrival as its own mini pass, for one reason: if the player dies (which will end the game
|
// arrival as its own mini pass, for one reason: if the player dies (which will end the game
|
||||||
// immediately), we still want every time's animation to finish, or it'll look like some
|
// immediately), we still want every time's animation to finish, or it'll look like some
|
||||||
// objects move backwards when the death screen appears!
|
// objects move backwards when the death screen appears!
|
||||||
if (!skip_to_third_pass) {
|
|
||||||
let cell_steppers = [];
|
let cell_steppers = [];
|
||||||
for (let actor of this.actors) {
|
for (let actor of this.actors) {
|
||||||
// Actors with no cell were destroyed
|
// Actors with no cell were destroyed
|
||||||
@ -441,16 +443,10 @@ export class Level {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//in Turn-Based mode, wait for input if the player can voluntarily move on tic_counter % 4 == 0 and isn't
|
|
||||||
if (this.turn_based && this.player.movement_cooldown == 0 && this.player.decision == null && (this.tic_counter % 4 == 0) && !force_wait)
|
_advance_tic_part2(p1_primary_direction, p1_secondary_direction) {
|
||||||
{
|
//player now makes a decision based on input
|
||||||
this.waiting_for_input = true;
|
this.actor_decision(this.player, p1_primary_direction);
|
||||||
return;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
this.waiting_for_input = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Third pass: everyone actually moves
|
// Third pass: everyone actually moves
|
||||||
for (let actor of this.actors) {
|
for (let actor of this.actors) {
|
||||||
@ -981,8 +977,6 @@ export class Level {
|
|||||||
}
|
}
|
||||||
|
|
||||||
undo() {
|
undo() {
|
||||||
//ok, actually we're not doing an in-progress move after all.
|
|
||||||
this.waiting_for_input = false;
|
|
||||||
this.pending_undo = [];
|
this.pending_undo = [];
|
||||||
this.aid = Math.max(1, this.aid);
|
this.aid = Math.max(1, this.aid);
|
||||||
|
|
||||||
|
|||||||
40
js/main.js
40
js/main.js
@ -703,6 +703,8 @@ class Player extends PrimaryView {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
waiting_for_input = false;
|
||||||
|
|
||||||
advance_by(tics) {
|
advance_by(tics) {
|
||||||
for (let i = 0; i < tics; i++) {
|
for (let i = 0; i < tics; i++) {
|
||||||
let input = this.get_input();
|
let input = this.get_input();
|
||||||
@ -765,11 +767,41 @@ class Player extends PrimaryView {
|
|||||||
this.previous_input = input;
|
this.previous_input = input;
|
||||||
|
|
||||||
this.sfx_player.advance_tic();
|
this.sfx_player.advance_tic();
|
||||||
|
|
||||||
|
var primary_dir = this.primary_action ? ACTION_DIRECTIONS[this.primary_action] : null;
|
||||||
|
var secondary_dir = this.secondary_action ? ACTION_DIRECTIONS[this.secondary_action] : null;
|
||||||
|
|
||||||
|
//turn based logic
|
||||||
|
//first, handle a part 2 we just got input for
|
||||||
|
if (this.waiting_for_input)
|
||||||
|
{
|
||||||
|
if (!this.turn_based || primary_dir != null || input.has('wait'))
|
||||||
|
{
|
||||||
this.level.advance_tic(
|
this.level.advance_tic(
|
||||||
this.primary_action ? ACTION_DIRECTIONS[this.primary_action] : null,
|
primary_dir,
|
||||||
this.secondary_action ? ACTION_DIRECTIONS[this.secondary_action] : null,
|
secondary_dir,
|
||||||
input.has('wait')
|
2);
|
||||||
);
|
}
|
||||||
|
}
|
||||||
|
else //TODO: or `if (!this.waiting_for_input)` to be snappier
|
||||||
|
{
|
||||||
|
this.level.advance_tic(
|
||||||
|
primary_dir,
|
||||||
|
secondary_dir,
|
||||||
|
1);
|
||||||
|
//then if we should wait for input, the player needs input and we don't have input, we set waiting_for_input, else we run part 2
|
||||||
|
if (this.turn_based && this.level.player_awaiting_input() && !(primary_dir != null || input.has('wait')))
|
||||||
|
{
|
||||||
|
this.waiting_for_input = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
this.level.advance_tic(
|
||||||
|
primary_dir,
|
||||||
|
secondary_dir,
|
||||||
|
2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (this.level.state !== 'playing') {
|
if (this.level.state !== 'playing') {
|
||||||
// We either won or lost!
|
// We either won or lost!
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user