Skip to content

Instantly share code, notes, and snippets.

@EtienneSchmitz
Created November 11, 2020 12:15
Show Gist options
  • Save EtienneSchmitz/f96320c99c7f793097b04713ecb52e12 to your computer and use it in GitHub Desktop.
Save EtienneSchmitz/f96320c99c7f793097b04713ecb52e12 to your computer and use it in GitHub Desktop.
Shoot - TP
/* eslint-disable prefer-const */
/* eslint-disable no-case-declarations */
import { ActionSchema, Context, ServiceBroker } from 'moleculer'
import { MoveToPacket } from '@ssl/types/internal/control/packet'
import { Vector2D } from '@ssl/types/utils/math'
import Strategies from '@ssl/types/internal/task-manager/tasks/strategies'
import { state } from '../../models/GameState'
export default class Shoot extends Strategies {
name = 'shoot';
public constructor(public id: number) {
super()
}
public static declaration: ActionSchema = {
params: {
id: {
type: 'number', min: 0, max: 15,
},
},
handler(ctx: Context<{ id: number }>): void {
state.assign.register([ctx.params.id], new Shoot(ctx.params.id))
},
}
public step = 1;
compute(broker: ServiceBroker): boolean {
const robot = state.data.robots.allies[this.id]
const { ball } = state.data
let target = ball.position
let orientation = 0
const goalCenter: Vector2D = { x: -(state.data.field.length / 2.0), y: 0 }
const target2Ball: Vector2D = { x: ball.position.x - goalCenter.x, y: ball.position.y - goalCenter.y }
const norm = Math.sqrt(target2Ball.x ** 2 + target2Ball.y ** 2)
target2Ball.x /= norm
target2Ball.y /= norm
switch (this.step) {
case 1:
target.x += target2Ball.x * 0.5
target.y += target2Ball.y * 0.5
orientation = Math.atan2(-target2Ball.y, -target2Ball.x)
const dist = Math.sqrt(((target.x - robot.position.x) ** 2 - (target.y - robot.position.y) ** 2))
if (dist < 0.01) {
this.step += 1
}
break
case 2:
orientation = Math.atan2(-target2Ball.y, -target2Ball.x)
target.x -= 0.1 * target2Ball.x
target.y -= 0.1 * target2Ball.y
if (robot.status.infrared) {
this.step += 1
}
break
default:
broker.logger.error('Not implemented')
}
void broker.call('bots-control.moveTo', {
id: this.id,
target,
orientation,
kick: this.step === 2,
expectedReachTime: 1,
} as MoveToPacket)
return this.step === 3
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment