Code Monkey home page Code Monkey logo

wasm4-as's Introduction

WASM-4 AssemblyScript Extras

WASM-4 AssemblyScript Extras (wasm4-as) is a drop-in replacement to WASM-4's wasm4.ts to ease development and make using WASM-4 with AssemblyScript feel more at home.

Features

  • Set the palette using hex strings
  • Rapidly change drawing colors directly through the drawing methods
  • Input API to check if buttons were pressed once
  • Write to persistent memory like an integer array
  • Clean API to avoid having to reference the WASM-4 memory address banks directly

Example

import * as w4 from "./wasm4"

export function start() : void {
    // Palette
    // https://lospec.com/palette-list/mononoke-san
    w4.palette("03324e", "c74148", "dbb9a0", "ffffff")
}

export function update (): void {
    // Clear the screen.
    w4.cls()

    // Draw some Rectangles
    w4.rect(0, 0, w4.SCREEN_SIZE / 2, w4.SCREEN_SIZE / 2, 1, 1)
    w4.rect(w4.SCREEN_SIZE / 2, 0, w4.SCREEN_SIZE / 2, w4.SCREEN_SIZE / 2, 2, 2)
    w4.rect(0, w4.SCREEN_SIZE / 2, w4.SCREEN_SIZE / 2, w4.SCREEN_SIZE / 2, 3, 3)
    w4.rect(w4.SCREEN_SIZE / 2, w4.SCREEN_SIZE / 2, w4.SCREEN_SIZE / 2, w4.SCREEN_SIZE / 2, 4, 4)

    // Shapes
    w4.circ(10, 20, 10, 3, 2)
    w4.ellipse(40, 20, 10, 5, 3, 2)
    w4.line(80, 120, 50, 80, 2)
    w4.pixel(w4.mousex(), w4.mousey(), 2)

    // Text
    w4.text("Hello World!", w4.SCREEN_SIZE / 2, w4.SCREEN_SIZE / 2, 4, 2, 1, 1)

    // Input
    if (w4.mouse(w4.MOUSE_LEFT)) {
        w4.text("Random Number: " + w4.rand(0, 100).toString(), 0, w4.SCREEN_SIZE, 1, 0, 0, 2)
    }
}

Quick Start

npm i
npm start

Usage

  1. Replace your project's wasm4.ts this wasm4.ts
  2. Import all the methods by using either of the following:
    // WASM-4
    import * as w4 from "./wasm4"
    
    // WASM-4
    import { color, palette, cls, line, pixel, hline, vline, ellipse, oval, rect, text, textWidth, textHeight, circ, btn, btnp, mouse, mousep, mousex, mousey, rand, diskw, diskr } from "./wasm4"

API

The API builds ontop of the base WASM-4 API to be a bit more user-friendly.

color([color1], [color2], [color3], [color4])
palette(color1, color2, color3, color4)
cls([color])
line(x1, y1, x2, y2, [color])
pixel(x, y, [color])
hline(x, y, length, [color])
vline(x, y, length, [color])
ellipse(x, y, radiusH, radiusV, [color], [fill])
rect(x, y, width, height, [color], [fill])
text(text, x, y, [color], [background], [horizontalAlign], [verticalAlign])
circ(x, y, radius, [color], [fill])
btn(button, [player])
btnp(button, [player])
mouse(button)
mousep(button)
mousex()
mousey()
rand(min, max)
diskw(position, value)
diskr(position)

License

wasm4-as is licensed under an unmodified zlib/libpng license, which is an OSI-certified, BSD-like license that allows static linking with closed source software. Check LICENSE for further details.

wasm4-as's People

Contributors

robloach avatar

Stargazers

 avatar

Watchers

 avatar  avatar

wasm4-as's Issues

palette() - Allow mixing strings and u32

export function palette<T>(col1:T, col2:T, col3:T, col4:T) :void {
    if (isString(col1)) {
        store<u32>(PALETTE, Number.parseInt(col1, 16) as u32);
        store<u32>(PALETTE, Number.parseInt(col2, 16) as u32, 4);
        store<u32>(PALETTE, Number.parseInt(col3, 16) as u32, 8);
        store<u32>(PALETTE, Number.parseInt(col4, 16) as u32, 12);
    }
    else {
        store<u32>(PALETTE, col1 as u32);
        store<u32>(PALETTE, col2 as u32, 4);
        store<u32>(PALETTE, col3 as u32, 8);
        store<u32>(PALETTE, col4 as u32, 12);
    }
}

Enums

Use enums for the horizontal/vertical alignments

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.