Skip to content

MaxPrehoda/refactory

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

2 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿญ ReFactory

A factory automation programming game where you write Python code to control robots and optimize production chains.

Python SvelteKit TypeScript

๐ŸŽฎ About

ReFactory is an idle programming game that teaches Python through interactive gameplay. Instead of traditional farming mechanics, you're programming a swarm of robots to automate an ever-expanding factory floor. Your code runs in real-time using Pyodide (Python in the browser), and you can see your functions execute as robots carry out your instructions.

Key Features

  • ๐Ÿ Python Programming - Write real Python code that executes in your browser
  • ๐Ÿ“š Interactive Tutorial - Learn through hands-on coding challenges with immediate feedback
  • โšก Energy System - Optimize your code for efficiency - wasteful code drains power!
  • ๐Ÿค– Real-time Execution - Watch your robots execute your code live on the factory floor
  • ๐ŸŽจ Isometric Factory View - Beautiful canvas-rendered factory with robots, resources, and machines
  • ๐Ÿ’พ Auto-save - Your progress is automatically saved to local storage
  • ๐ŸŽฏ Progressive Gameplay - Start simple, unlock complex production chains and departments

๐Ÿš€ Getting Started

Not looking to contribute? Just want to play and optimize to the top of the leaderboard? https://refactory-swart.vercel.app/

Prerequisites

  • Node.js (v18 or higher)
  • npm or pnpm

Installation

# Clone the repository
git clone <repository-url>
cd refactory

# Install dependencies
npm install

# Start the development server
npm run dev

Visit http://localhost:5173 to start playing!

๐ŸŽ“ How to Play

Tutorial

When you first launch the game, you'll go through an interactive tutorial that teaches you:

  1. Moving Robots - Use robot.move_to(x, y) to position your robots
  2. Finding Resources - Scan for nearby resources with robot.get_nearby_resources(radius)
  3. Collecting Resources - Pick up resources with robot.pickup(resource)
  4. Automation - Build loops and conditionals to automate production

Python API Reference

Each robot has access to these functions:

# Movement
robot.move_to(x, y)                    # Move to coordinates (x, y)
robot.get_position()                    # Get current position

# Resources
robot.get_nearby_resources(radius)      # Find resources within radius
robot.pickup(resource)                  # Pick up a resource
robot.get_inventory()                   # Check what you're carrying

# Machines
robot.get_nearby_machines(radius)       # Find machines within radius
robot.dropoff(machine)                  # Drop resources at a machine

# Status
robot.get_status()                      # Returns: 'idle', 'moving', 'working', or 'error'
robot.get_energy()                      # Get robot's energy level

Example Code

Here's a basic automation script:

# Get robot status
status = robot.get_status()

# Only act when idle
if status == "idle":
    # Find nearby resources
    resources = robot.get_nearby_resources(200)

    # Check inventory
    inventory = robot.get_inventory()

    # If we found resources and aren't carrying anything
    if len(resources) > 0 and inventory["type"] is None:
        # Get the first resource
        resource = resources[0]

        # Move to it
        robot.move_to(resource["position"]["x"], resource["position"]["y"])

        # Pick it up
        robot.pickup(resource)

๐Ÿ› ๏ธ Technical Stack

  • Frontend: SvelteKit 5 (Runes mode)
  • Python Runtime: Pyodide (Python in WebAssembly)
  • Rendering: HTML5 Canvas
  • State Management: Svelte 5 reactive stores
  • Styling: Scoped CSS

๐Ÿ“ Project Structure

refactory/
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ lib/
โ”‚   โ”‚   โ”œโ”€โ”€ components/        # Svelte components
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ CodeEditor.svelte
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ FactoryCanvas.svelte
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ GameHUD.svelte
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ GameControls.svelte
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ InteractiveTutorial.svelte
โ”‚   โ”‚   โ”œโ”€โ”€ engine/           # Game logic
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ gameLoop.svelte.ts
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ pythonExecutor.ts
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ robotController.ts
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ machineController.ts
โ”‚   โ”‚   โ”œโ”€โ”€ stores/           # State management
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ gameState.svelte.ts
โ”‚   โ”‚   โ””โ”€โ”€ types/            # TypeScript types
โ”‚   โ”‚       โ””โ”€โ”€ game.ts
โ”‚   โ””โ”€โ”€ routes/               # SvelteKit routes
โ”‚       โ””โ”€โ”€ +page.svelte
โ”œโ”€โ”€ static/                   # Static assets
โ””โ”€โ”€ package.json

๐ŸŽฏ Game Mechanics

Energy System

Every action costs energy:

  • Function calls: 0.01 energy
  • Movement: 0.1 energy per move command
  • Picking up resources: 0.5 energy
  • Dropping off resources: 0.5 energy

Energy regenerates slowly over time. Write efficient code to maximize productivity!

Resources

  • Iron Ore โš™๏ธ - Basic metal resource
  • Copper Ore ๐ŸŸ  - Electrical component resource
  • Circuit Boards ๐Ÿ’พ - Advanced manufactured goods

Robot States

  • idle - Ready for new commands
  • moving - Traveling to target position
  • working - Executing a task
  • error - Something went wrong (check error message)

๐Ÿ”ง Development

Build for Production

npm run build

Preview Production Build

npm run preview

Type Checking

npm run check

๐Ÿ—บ๏ธ Roadmap

Future features planned:

  • โœ… Python programming interface
  • โœ… Interactive tutorial system
  • โฌœ Multiple robot support
  • โฌœ Machine crafting system
  • โฌœ Department unlocks (Electronics, Textiles, Food Processing)
  • โฌœ Profiler/heat map for code optimization
  • โฌœ Multiplayer leaderboards
  • โฌœ Code sharing via URL
  • โฌœ Version control features (git-like)
  • โฌœ Worker threads for parallel processing
  • โฌœ PWA support for offline play

๐Ÿ“ License

MIT

๐Ÿค Contributing

Contributions welcome! Please feel free to submit a Pull Request.


Built with โค๏ธ using SvelteKit and Python

About

A factory automation programming game where you write Python code to control robots and optimize production chains.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors