Skip to content

Instantly share code, notes, and snippets.

@botrosg
botrosg / WorldSpaceUIDocument.cs
Created December 1, 2024 08:53 — forked from katas94/WorldSpaceUIDocument.cs
Custom Unity component to create a world-space UIToolkit panel
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
using UnityEngine.EventSystems;
using UnityEngine.Rendering;
namespace Katas.Experimental
{
public class WorldSpaceUIDocument : MonoBehaviour, IPointerMoveHandler, IPointerUpHandler, IPointerDownHandler,
ISubmitHandler, ICancelHandler, IMoveHandler, IScrollHandler, ISelectHandler, IDeselectHandler, IDragHandler
@botrosg
botrosg / Pong.js
Last active August 23, 2017 01:43
var animate = window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
function(callback) { window.setTimeout(callback, 1000/60) };
var CANVAS_WIDTH = 400;
var CANVAS_HEIGHT = 600;
var PADDLE_HEIGHT = 10;
/////////////////////////////////////////////////////////
// COMPUTER
/////////////////////////////////////////////////////////
Computer.prototype.update = function(ball) {
var x_pos = ball.x;
var diff = -((this.paddle.x + (this.paddle.width / 2)) - x_pos);
if(diff < 0 && diff < -PADDLE_MAX_SPEED) { // max speed left
diff = -PADDLE_MAX_SPEED;
} else if(diff > 0 && diff > PADDLE_MAX_SPEED) { // max speed right
/////////////////////////////////////////////////////////
// CONTROLS
/////////////////////////////////////////////////////////
var keysDown = {};
window.addEventListener("keydown", function(event) {
keysDown[event.keyCode] = true;
});
/////////////////////////////////////////////////////////
// PLAYER
/////////////////////////////////////////////////////////
function Player() {
this.paddle = new Paddle((canvas.width - PADDLE_WIDTH) / 2, canvas.height - 10 - PADDLE_HEIGHT, PADDLE_WIDTH, PADDLE_HEIGHT, "#0000FF");
}
Player.prototype.render = function() {
this.paddle.render();
/////////////////////////////////////////////////////////
// PADDLES
/////////////////////////////////////////////////////////
function Paddle(x, y, width, height, color) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.x_speed = 0;
if(top_y > canvas.height / 2) {
if(top_y < (paddle1.y + paddle1.height) && bottom_y > paddle1.y && top_x < (paddle1.x + paddle1.width) && bottom_x > paddle1.x) {
// hit the player's paddle
this.y_speed = -3;
this.x_speed += (paddle1.x_speed / 2);
this.y += this.y_speed;
this.color = paddle1.color;
}
} else {
if(top_y < (paddle2.y + paddle2.height) && bottom_y > paddle2.y && top_x < (paddle2.x + paddle2.width) && bottom_x > paddle2.x) {
if(this.x - 5 < 0) { // hitting the left wall
this.x = 5;
this.x_speed = -this.x_speed;
} else if(this.x + 5 > canvas.width) { // hitting the right wall
this.x = canvas.width - this.radius;
this.x_speed = -this.x_speed;
}
if(this.y < 0 || this.y > canvas.height) { // a point was scored
this.x_speed = this.x_initial_speed;
this.y_speed = this.y_initial_speed;
this.x = canvas.width / 2;
this.y = canvas.height / 2;
this.color = "#FFFFFF";
}
/////////////////////////////////////////////////////////
// BALL
/////////////////////////////////////////////////////////
function Ball(x,y) {
this.x = x;
this.y = y;
this.x_initial_speed = 0;
this.y_initial_speed = 3;
this.x_speed = this.x_initial_speed;